我似乎無法阻止我的形式在我Group Box
檢查單選按鈕之一:如何防止在表單加載時檢查RadioButton?
正如設計師表示,沒有單選按鈕檢查那裏。
下面是關於這個簡單表單的所有代碼。沒有任何要求在此處或在表單的設計人員中檢查單選按鈕。
問:有沒有一種方法,以防止任何單選按鈕時從被檢查的形式加載?
public ValueTypeSelector() {
InitializeComponent();
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
radioButton5.Checked = false;
radioButton6.Checked = false;
button1.Enabled = false;
button1.Click += clickEvent;
button2.Click += clickEvent;
radioButton1.Click += clickEvent;
radioButton2.Click += clickEvent;
radioButton3.Click += clickEvent;
radioButton4.Click += clickEvent;
radioButton5.Click += clickEvent;
radioButton6.Click += clickEvent;
}
void OnShow(object sender, EventArgs e) {
foreach (RadioButton rad in Controls) {
if (rad.Checked) {
Console.WriteLine("WTF?");
}
}
}
void clickEvent(object sender, EventArgs e) {
RadioButton rad = sender as RadioButton;
if (rad != null) {
if (rad.Checked) {
if (rad == radioButton1) {
DataType = TableDataType.Boolean; // <= HERE IS THE PROBLEM! FIRES ON FORM LOAD
} else if (rad == radioButton2) {
DataType = TableDataType.Character;
} else if (rad == radioButton3) {
DataType = TableDataType.DateTime;
} else if (rad == radioButton4) {
DataType = TableDataType.Decimal;
} else if (rad == radioButton5) {
DataType = TableDataType.Integer;
} else if (rad == radioButton6) {
DataType = TableDataType.String;
} else {
return;
}
button1.Enabled = true;
}
} else if (sender == button1) {
DialogResult = DialogResult.OK;
Close();
} else if (sender == button2) {
DialogResult = DialogResult.Cancel;
Close();
}
}
UPDATE:問題是顯示在表單時radioButton1
被檢查:
if (rad == radioButton1) {
DataType = TableDataType.Boolean; // <= HERE IS THE PROBLEM! FIRES ON FORM LOAD
} else if (rad == radioButton2) {
添加一些單選按鈕,並使其不可見:) –
LOL。我寧願控制我的代碼! – jp2code