C# default checked RadioButton in a GroupBox
Solution 1
Just select the RadioButtons in the form designer and set the 'checked' property to true for the elements you want to be checked by default.
Solution 2
I had to set the TabStop
property of all radio buttons to 'false' to prevent the radio button with lowest TabIndex
from being automatically checked in the group box.
daiyue
Updated on January 05, 2020Comments
-
daiyue over 4 years
I have two
GroupBoxes
(groupBox1 and groupBox2) and each of which contains a set ofRadioButtons
.The problem is that in groupBox1, there is a default
RadioButton
(1stRadioButton
in the group) been checked when running the project, meanwhile groupBox2 doesn't have a default checkedRadioButton
,I am wondering why is that and how to set a default checked
RadioButton
for groupBox2, and deselect the defaultRadioButton
for groupBox1.ps. When I run the project, the
Checked
property of the firstRadioButton
in groupBox1 was set to 'False' though.namespace RadioButtonsTest { partial class RadioButtonsTestForm { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.buttonTypeGroupBox = new System.Windows.Forms.GroupBox(); this.retryCancelButton = new System.Windows.Forms.RadioButton(); this.yesNoRadioButton = new System.Windows.Forms.RadioButton(); this.yesNoCancelRadioButton = new System.Windows.Forms.RadioButton(); this.abortRetryIgnoreRadioButton = new System.Windows.Forms.RadioButton(); this.okCancelRadioButton = new System.Windows.Forms.RadioButton(); this.okRadioButton = new System.Windows.Forms.RadioButton(); this.displayButton = new System.Windows.Forms.Button(); this.iconGroupBox = new System.Windows.Forms.GroupBox(); this.warningRadioButton = new System.Windows.Forms.RadioButton(); this.stopRadioButton = new System.Windows.Forms.RadioButton(); this.questionRadioButton = new System.Windows.Forms.RadioButton(); this.informationRadioButton = new System.Windows.Forms.RadioButton(); this.handRadioButton = new System.Windows.Forms.RadioButton(); this.exclamationRadioButton = new System.Windows.Forms.RadioButton(); this.errorRadioButton = new System.Windows.Forms.RadioButton(); this.asteriskRadioButton = new System.Windows.Forms.RadioButton(); this.displayLabel = new System.Windows.Forms.Label(); this.buttonTypeGroupBox.SuspendLayout(); this.iconGroupBox.SuspendLayout(); this.SuspendLayout(); // // buttonTypeGroupBox // this.buttonTypeGroupBox.Controls.Add(this.retryCancelButton); this.buttonTypeGroupBox.Controls.Add(this.yesNoRadioButton); this.buttonTypeGroupBox.Controls.Add(this.yesNoCancelRadioButton); this.buttonTypeGroupBox.Controls.Add(this.abortRetryIgnoreRadioButton); this.buttonTypeGroupBox.Controls.Add(this.okCancelRadioButton); this.buttonTypeGroupBox.Controls.Add(this.okRadioButton); this.buttonTypeGroupBox.Location = new System.Drawing.Point(13, 13); this.buttonTypeGroupBox.Name = "buttonTypeGroupBox"; this.buttonTypeGroupBox.Size = new System.Drawing.Size(120, 160); this.buttonTypeGroupBox.TabIndex = 0; this.buttonTypeGroupBox.TabStop = false; this.buttonTypeGroupBox.Text = "Button Type"; // // retryCancelButton // this.retryCancelButton.AutoSize = true; this.retryCancelButton.Location = new System.Drawing.Point(7, 140); this.retryCancelButton.Name = "retryCancelButton"; this.retryCancelButton.Size = new System.Drawing.Size(83, 17); this.retryCancelButton.TabIndex = 5; this.retryCancelButton.TabStop = true; this.retryCancelButton.Text = "RetryCancel"; this.retryCancelButton.UseVisualStyleBackColor = true; // // yesNoRadioButton // this.yesNoRadioButton.AutoSize = true; this.yesNoRadioButton.Location = new System.Drawing.Point(7, 116); this.yesNoRadioButton.Name = "yesNoRadioButton"; this.yesNoRadioButton.Size = new System.Drawing.Size(57, 17); this.yesNoRadioButton.TabIndex = 4; this.yesNoRadioButton.TabStop = true; this.yesNoRadioButton.Text = "YesNo"; this.yesNoRadioButton.UseVisualStyleBackColor = true; // // yesNoCancelRadioButton // this.yesNoCancelRadioButton.AutoSize = true; this.yesNoCancelRadioButton.Location = new System.Drawing.Point(7, 92); this.yesNoCancelRadioButton.Name = "yesNoCancelRadioButton"; this.yesNoCancelRadioButton.Size = new System.Drawing.Size(90, 17); this.yesNoCancelRadioButton.TabIndex = 3; this.yesNoCancelRadioButton.TabStop = true; this.yesNoCancelRadioButton.Text = "YesNoCancel"; this.yesNoCancelRadioButton.UseVisualStyleBackColor = true; // // abortRetryIgnoreRadioButton // this.abortRetryIgnoreRadioButton.AutoSize = true; this.abortRetryIgnoreRadioButton.Location = new System.Drawing.Point(7, 68); this.abortRetryIgnoreRadioButton.Name = "abortRetryIgnoreRadioButton"; this.abortRetryIgnoreRadioButton.Size = new System.Drawing.Size(105, 17); this.abortRetryIgnoreRadioButton.TabIndex = 2; this.abortRetryIgnoreRadioButton.TabStop = true; this.abortRetryIgnoreRadioButton.Text = "AbortRetryIgnore"; this.abortRetryIgnoreRadioButton.UseVisualStyleBackColor = true; // // okCancelRadioButton // this.okCancelRadioButton.AutoSize = true; this.okCancelRadioButton.Location = new System.Drawing.Point(7, 44); this.okCancelRadioButton.Name = "okCancelRadioButton"; this.okCancelRadioButton.Size = new System.Drawing.Size(73, 17); this.okCancelRadioButton.TabIndex = 1; this.okCancelRadioButton.TabStop = true; this.okCancelRadioButton.Text = "OKCancel"; this.okCancelRadioButton.UseVisualStyleBackColor = true; // // okRadioButton // this.okRadioButton.AutoSize = true; this.okRadioButton.Location = new System.Drawing.Point(7, 20); this.okRadioButton.Name = "okRadioButton"; this.okRadioButton.Size = new System.Drawing.Size(40, 17); this.okRadioButton.TabIndex = 0; this.okRadioButton.TabStop = true; this.okRadioButton.Text = "OK"; this.okRadioButton.UseVisualStyleBackColor = true; // // displayButton // this.displayButton.Location = new System.Drawing.Point(13, 180); this.displayButton.Name = "displayButton"; this.displayButton.Size = new System.Drawing.Size(120, 40); this.displayButton.TabIndex = 1; this.displayButton.Text = "Display"; this.displayButton.UseVisualStyleBackColor = true; this.displayButton.Click += new System.EventHandler(this.displayButton_Click); // // iconGroupBox // this.iconGroupBox.Controls.Add(this.warningRadioButton); this.iconGroupBox.Controls.Add(this.stopRadioButton); this.iconGroupBox.Controls.Add(this.questionRadioButton); this.iconGroupBox.Controls.Add(this.informationRadioButton); this.iconGroupBox.Controls.Add(this.handRadioButton); this.iconGroupBox.Controls.Add(this.exclamationRadioButton); this.iconGroupBox.Controls.Add(this.errorRadioButton); this.iconGroupBox.Controls.Add(this.asteriskRadioButton); this.iconGroupBox.Location = new System.Drawing.Point(140, 13); this.iconGroupBox.Name = "iconGroupBox"; this.iconGroupBox.Size = new System.Drawing.Size(120, 207); this.iconGroupBox.TabIndex = 2; this.iconGroupBox.TabStop = false; this.iconGroupBox.Text = "Icon"; // // warningRadioButton // this.warningRadioButton.AutoSize = true; this.warningRadioButton.Location = new System.Drawing.Point(7, 188); this.warningRadioButton.Name = "warningRadioButton"; this.warningRadioButton.Size = new System.Drawing.Size(65, 17); this.warningRadioButton.TabIndex = 7; this.warningRadioButton.TabStop = true; this.warningRadioButton.Text = "Warning"; this.warningRadioButton.UseVisualStyleBackColor = true; // // stopRadioButton // this.stopRadioButton.AutoSize = true; this.stopRadioButton.Location = new System.Drawing.Point(7, 164); this.stopRadioButton.Name = "stopRadioButton"; this.stopRadioButton.Size = new System.Drawing.Size(47, 17); this.stopRadioButton.TabIndex = 6; this.stopRadioButton.TabStop = true; this.stopRadioButton.Text = "Stop"; this.stopRadioButton.UseVisualStyleBackColor = true; // // questionRadioButton // this.questionRadioButton.AutoSize = true; this.questionRadioButton.Location = new System.Drawing.Point(7, 140); this.questionRadioButton.Name = "questionRadioButton"; this.questionRadioButton.Size = new System.Drawing.Size(67, 17); this.questionRadioButton.TabIndex = 5; this.questionRadioButton.TabStop = true; this.questionRadioButton.Text = "Question"; this.questionRadioButton.UseVisualStyleBackColor = true; // // informationRadioButton // this.informationRadioButton.AutoSize = true; this.informationRadioButton.Location = new System.Drawing.Point(7, 116); this.informationRadioButton.Name = "informationRadioButton"; this.informationRadioButton.Size = new System.Drawing.Size(77, 17); this.informationRadioButton.TabIndex = 4; this.informationRadioButton.TabStop = true; this.informationRadioButton.Text = "Information"; this.informationRadioButton.UseVisualStyleBackColor = true; // // handRadioButton // this.handRadioButton.AutoSize = true; this.handRadioButton.Location = new System.Drawing.Point(7, 92); this.handRadioButton.Name = "handRadioButton"; this.handRadioButton.Size = new System.Drawing.Size(51, 17); this.handRadioButton.TabIndex = 3; this.handRadioButton.TabStop = true; this.handRadioButton.Text = "Hand"; this.handRadioButton.UseVisualStyleBackColor = true; // // exclamationRadioButton // this.exclamationRadioButton.AutoSize = true; this.exclamationRadioButton.Location = new System.Drawing.Point(7, 68); this.exclamationRadioButton.Name = "exclamationRadioButton"; this.exclamationRadioButton.Size = new System.Drawing.Size(82, 17); this.exclamationRadioButton.TabIndex = 2; this.exclamationRadioButton.TabStop = true; this.exclamationRadioButton.Text = "Exclamation"; this.exclamationRadioButton.UseVisualStyleBackColor = true; // // errorRadioButton // this.errorRadioButton.AutoSize = true; this.errorRadioButton.Location = new System.Drawing.Point(7, 44); this.errorRadioButton.Name = "errorRadioButton"; this.errorRadioButton.Size = new System.Drawing.Size(47, 17); this.errorRadioButton.TabIndex = 1; this.errorRadioButton.TabStop = true; this.errorRadioButton.Text = "Error"; this.errorRadioButton.UseVisualStyleBackColor = true; // // asteriskRadioButton // this.asteriskRadioButton.AutoSize = true; this.asteriskRadioButton.Location = new System.Drawing.Point(7, 20); this.asteriskRadioButton.Name = "asteriskRadioButton"; this.asteriskRadioButton.Size = new System.Drawing.Size(62, 17); this.asteriskRadioButton.TabIndex = 0; this.asteriskRadioButton.TabStop = true; this.asteriskRadioButton.Text = "Asterisk"; this.asteriskRadioButton.UseVisualStyleBackColor = true; // // displayLabel // this.displayLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.displayLabel.Location = new System.Drawing.Point(13, 227); this.displayLabel.Name = "displayLabel"; this.displayLabel.Size = new System.Drawing.Size(247, 15); this.displayLabel.TabIndex = 3; // // RadioButtonsTestForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284, 262); this.Controls.Add(this.displayLabel); this.Controls.Add(this.iconGroupBox); this.Controls.Add(this.displayButton); this.Controls.Add(this.buttonTypeGroupBox); this.Name = "RadioButtonsTestForm"; this.Text = "Demonstrating RadioButtons"; this.buttonTypeGroupBox.ResumeLayout(false); this.buttonTypeGroupBox.PerformLayout(); this.iconGroupBox.ResumeLayout(false); this.iconGroupBox.PerformLayout(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.GroupBox buttonTypeGroupBox; private System.Windows.Forms.RadioButton retryCancelButton; private System.Windows.Forms.RadioButton yesNoRadioButton; private System.Windows.Forms.RadioButton yesNoCancelRadioButton; private System.Windows.Forms.RadioButton abortRetryIgnoreRadioButton; private System.Windows.Forms.RadioButton okCancelRadioButton; private System.Windows.Forms.RadioButton okRadioButton; private System.Windows.Forms.Button displayButton; private System.Windows.Forms.GroupBox iconGroupBox; private System.Windows.Forms.RadioButton warningRadioButton; private System.Windows.Forms.RadioButton stopRadioButton; private System.Windows.Forms.RadioButton questionRadioButton; private System.Windows.Forms.RadioButton informationRadioButton; private System.Windows.Forms.RadioButton handRadioButton; private System.Windows.Forms.RadioButton exclamationRadioButton; private System.Windows.Forms.RadioButton errorRadioButton; private System.Windows.Forms.RadioButton asteriskRadioButton; private System.Windows.Forms.Label displayLabel; } }