Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

c# - How can I prevent a RadioButton from being checked when the Form loads?

I can't seem to prevent my form from checking one of the Radio Buttons in my Group Box:

enter image description here

As shown in the designer, no Radio Buttons are checked there.

Below is just about all of the code for this simple form. Nothing calls for a Radio Buttons to be checked here or in the form's designer.

Q: Is there a way to prevent any Radio Button from being checked when the form loads?

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: The problem is that radioButton1 gets checked when the form is shown:

      if (rad == radioButton1) {
        DataType = TableDataType.Boolean; // <= HERE IS THE PROBLEM! FIRES ON FORM LOAD
      } else if (rad == radioButton2) {
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make sure your radiobuttons are NOT the first tabindex = 0 controls. Make the OK button tabindex=0, followed by the radiobuttons.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...