0

ユーザーがグループボックスのラジオボタンから選択肢を選択してラベルに表示する方法はありますか?

直後に数量/電話タイプの行になりnumberPhoneTextBox.Textます。

ユーザーが選択できるラジオボタンは全部で3つあります。

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 + "/";
}
4

2 に答える 2

0

残念ながら、手作業で行う必要があります。次のように、コードの繰り返しを避けるために、タスクを実行するメソッドまたはプロパティを定義できます。

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

アップデート:

どうやら、OPの割り当ては「if / elseステートメントのユーザーを許可していません」-これは非常に現実的ではありませんが、?:演算子を使用するなど、いくつかの方法で回避できます。

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

別のオプションは、イベントを使用することです。

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
于 2012-09-23T22:22:13.280 に答える
0

ところで、文字列の連結を使用する習慣から抜け出す必要があります。それは非常に非効率的です。代わりに、次のようなものを試してください。

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);
}
于 2012-09-24T00:33:16.093 に答える