开发者

C#: Simple question regarding radio and checkbox controls. Getting the wrong return type?

开发者 https://www.devze.com 2023-03-16 21:14 出处:网络
Getting an error of : Error1\'string RadioGroupTester.Form1.chkReceipt_CheckedChanged(object, System.EventArgs)\' has the wrong return type

Getting an error of :

Error   1   'string RadioGroupTester.Form1.chkReceipt_CheckedChanged(object, System.EventArgs)' has the wrong return type

When I compile a very simple program to understand radio and check box controls.

Here's the code for the form is as follows:

    private string rdoMastercard_CheckedChanged(object sender, EventArgs e)
    {
        if (rdoMastercard.Checked)
            return "You have selected Mastercard";
        else
            return "You have selected Visa";
    }

    private string chkReceipt_CheckedChanged(object sender, EventArgs e)
    {
        if (chkReceipt.Checked)
            return "You will receive a receipt";
        else
            return "You will not receive a receipt";
    }

    private string chkRecurring_CheckedChanged(object sender, EventArgs e)
    {
        if (chkRecurring.Checked)
        {
            return "You will be charged monthly";
        }
        else
            return "This is a one time payment";
  开发者_JS百科  }

And here's what the form looks like:

C#: Simple question regarding radio and checkbox controls. Getting the wrong return type?

What am I doing wrong here? I apologize, but I'm still pretty new to C# and VS 2010.

Thanks, Ray


You can't return things from an event handler. Do you mean to show a message box instead? Or give feedback through a label? It would be something like:

private string void rdoMastercard_CheckedChanged(object sender, EventArgs e)
{
    if (rdoMastercard.Checked)
        return MessageBox.Show("You have selected Mastercard");
    else
        return MessageBox.Show("You have selected Visa");
}

and the same for your other examples. This example uses a message box but you can set a Label's text or whatever.

The reason? How do you expect C# to know what you want to do when you're returning something from an event handler? An event handler handles events, nothing more.

Edit: So for what you want to do, it would be something like:

private void btnConfirmChoices_Click(object sender, EventArgs e) {
    string str = string.Empty;

    if(rdoMastercard.Checked)
        str += "You have selected Mastercard\n";
    else
        str += "You have selected Visa\n";

    if (chkReceipt.Checked)
        str += "You will receive a receipt\n";
    else
        str += "You will not receive a receipt\n";

    if (chkRecurring.Checked)
        str += "You will be charged monthly";
    else
        str += "This is a one time payment";

    MessageBox.Show(str);
}


Not sure how you got in this pickle, you are seemingly guessing that returning a string from the CheckedChanged event handler is useful. No such use has been intended in the design, the declaration for the RadioButton.CheckedChanged event handler is:

public event EventHandler CheckedChanged;

Which makes the declaration of the EventHandler delegate type relevant:

public delegate void EventHandler(object sender, EventArgs e);

Which defines exactly what your event handler should look like. It has to be a method that returns void and takes two argument, one of type object and one of type EventArgs. Which is what you get when you double-click the radio button in the designer:

    private void radioButton1_CheckedChanged(object sender, EventArgs e) {

    }

Note how the auto-generated method exactly matches the signature of the delegate type. The compiler is happy. What it won't be happy about is you changing the signature of your event handler. The Winforms code was pre-programmed to call your CheckedChanged event handler and it is hoping that you implemented the way it was pre-programmed. What it cannot do is deal with a different return type. It doesn't know what to do with it. What is it supposed to do with it? Do you want to log it? Do you want to change the Text property of the radio button? Do you want to send it across the Internet to another machine?

These things are certainly possible. But you have to write that code, Winforms cannot guess for you what you want to use it for.

The buck stops there, your time to take charge.

0

精彩评论

暂无评论...
验证码 换一张
取 消