0

Is there a way when a user selects a choice from a radio button from a group-box to appear in a label?

It would be on the line with Quantity/Phone Type right after numberPhoneTextBox.Text.

There are a total of 3 radio-buttons for the user to choose from.

private void displayButton_Click(object sender, EventArgs e)
{
    summaryLabel.Text = "Receipt Summary\n" +
        "--------------\n" +
        "Name: " + nameTextBox.Text +
        "\nAddress: " + streetTextBox.Text +
        "\nCity: " + cityTextBox.Text +
        "\nState: " + stateTextBox.Text +
        "\nZip Code: " + zipTextBox.Text +
        "\nPhone Number: " + phoneNumberTextBox.Text +
        "\nDate: " + dateMaskedBox.Text +
        "\n-------------------------" +
        "\nQuantity/Phone Type: " + numberPhoneTextBox.Text + "/";
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1422051
  • 37
  • 1
  • 7
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Sep 24 '12 at 00:25

2 Answers2

0

You would have to do it by hand, unfortunately. You could define a method or property that performs the task for you to avoid repetitive code, like so:

String GetRadioButtonValue() {
         if( radioButton1.Checked ) return radioButton1.Text;
    else if( radioButton2.Checked ) return radioButton2.Text;
    else                            return radioButton3.Text;
}

UPDATE:

Apparently the OP's assignment "doesn't allow the user of if/else statements" - that's quite surreal, but you can skirt it several ways, such as using the ?: operator:

String GetRadioButtonValue() {
    return radioButton1.Checked ? radioButton1.Text
         : radioButton2.Checked ? radioButton2.Text
                                : radioButton3.Text;
}

Another option is to use events:

private String _selectedRadioText;

public MyForm() { // your form's constructor
    InitializeComponent();
    radioButton1.CheckedChanged += RadioButtonCheckedChanged;
    radioButton2.CheckedChanged += RadioButtonCheckedChanged;
    radioButton3.CheckedChanged += RadioButtonCheckedChanged;
    // or even:
    // foreach(Control c in this.groupBox.Controls)
    //     if( c is RadioButton )
    //         ((RadioButton)c).CheckedChanged += RadioButtonCheckedChanged;

    // Initialize the field
    _selectedRadioText = radioButton1.Text;
}

private void RadioButtonCheckedChanged(Object sender, EventArgs e) {
    _selectedRadioText = ((RadioButton)sender).Text;
}

// then just concatenate the _selectedRadioText field into your string
Dai
  • 141,631
  • 28
  • 261
  • 374
  • Tested it and your way works. However I just checked project assignment and it doesn't allow the use of if/else statements.. which is a shame. What's the long way of making this work? Thanks for the help! – user1422051 Sep 23 '12 at 22:35
0

BTW, you should get out of the habit of using string concatenation. It's very inefficient. Instead, try something like this:

private void displayButton_Click(object sender, EventArgs e)
{
    summaryLabel.Text =
        string.Format(
            "Receipt Summary\n--------------\nName: {0}\nAddress: {1}\nCity: {2}\nState: {3}\nZip Code: {4}\nPhone Number: {5}\nDate: {6}\n-------------------------\nQuantity/Phone Type: {7}/",
            nameTextBox.Text,
            streetTextBox.Text,
            cityTextBox.Text,
            stateTextBox.Text,
            zipTextBox.Text,
            phoneNumberTextBox.Text,
            dateMaskedBox.Text,
            numberPhoneTextBox.Text);
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397