当用户从组框中的单选按钮中选择要出现在标签中时,有没有办法?
在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 + "/";
}
答案 0 :(得分: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
答案 1 :(得分: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);
}