Under usual circumstances, only one RadioButton can be checked at a time in a Panel.
Now i have a Panel (mainly to achieve vertical scrolling), containing a two-column TableLayoutPanel. New rows can be added infinitely to the TLP by the click on a "Add row"-button (this is why i need the Panel for scrolling down).
Each row in the TLP looks the same:
First column contains another TLP with one col and two rows. First row contains a RadioButton only, second row contains another button only.
Second column contains a CheckedListBox.
So let's say, i have added 5 rows to the TLP. So i have 5 RadioButtons. Don't know why, but the RadioButtons can be checked all at once. How can i avoid that?
Here is the code for adding the rows the the TLP:
void add_newbox()
{
var new_chklistbox = new CheckedListBox{
Dock=DockStyle.Fill,
Margin=new Padding(0,0,0,3),
Location=new Point(20,0),
Size=new Size(238,94),
HorizontalScrollbar=true,
CheckOnClick=true
};
var new_radiobutton = new RadioButton{
Text="",
Dock=DockStyle.Fill,
Location=new Point(3,3),
Size=new Size(14,90),
MaximumSize=new Size(0,90)
};
new_radiobutton.Click += (sender, e) => this.focus=new_chklistbox;
var new_rembutton = new Button{
Text="-",
Dock=DockStyle.Fill,
AutoSize=true,
AutoSizeMode=AutoSizeMode.GrowAndShrink,
Margin=new Padding(0)
};
new_rembutton.Click += (sender, e) => rem_items();
var new_tbl = new TableLayoutPanel{
RowCount=2,
ColumnCount=1,
Dock=DockStyle.Fill,
Margin=new Padding(0)
};
new_tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 70F));
new_tbl.RowStyles.Add(new RowStyle(SizeType.AutoSize));
new_tbl.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
new_tbl.Controls.Add(new_radiobutton,0,0);
new_tbl.Controls.Add(new_rembutton,0,1);
tbl_groups.Controls.Add(new_tbl,0,tbl_groups.RowCount);
tbl_groups.Controls.Add(new_chklistbox,1,tbl_groups.RowCount);
}
Greeting, xola
To have some radio buttons act as a group you should host them all in the same container. Different table layout panels are different container. It describes the behavior.
If for any reason you want to keep the layout as is, you need to set
AutoCheck
property of those radio buttons to true and handleClick
event of them to check just clicked one and uncheck rest of them.Example