Disabled checkboxes in flowLayoutPanel - c#

I'm trying to create some checkboxes dynamically in the flowLayoutPanel control.
I have managed to create it but they seem to be disabled inside the panel. I can't check/uncheck them. They are grey and not active.
I suppose there is some flowLayoutPanel property that prevents these checkboxes to be enabled that I'm missing.
Here is my code for flowLayoutPanel:
//
// flowLayoutPanelForCheckBoxes
//
this.flowLayoutPanelForCheckBoxes.Controls.Add(this.SomeCheckBox);
this.flowLayoutPanelForCheckBoxes.AutoScroll = true;
this.flowLayoutPanelForCheckBoxes.Location = new System.Drawing.Point(132, 8);
this.flowLayoutPanelForCheckBoxes.Name = "flowLayoutPanelForCheckBoxes";
this.flowLayoutPanelForCheckBoxes.Size = new System.Drawing.Size(546, 38);
this.flowLayoutPanelForCheckBoxes.TabIndex = 28;
this.flowLayoutPanelForCheckBoxes.WrapContents = false;
For generating checkboxes:
private List<CheckBox> GetGeneratedCheckboxes()
{
var generatedCheckboxes = new List<CheckBox>();
var valuesForCheckboxes = GetCheckboxValuesFromDb(); //it returns dictionary<int, string>
// with some numbers and text. I think this method is not as important
if (valuesForCheckboxes != null && valuesForCheckboxes.Count != 0)
{
int index = 0;
foreach (var chbx in valuesForCheckboxes)
{
var checkboxToAdd = new System.Windows.Forms.CheckBox();
checkboxToAdd.AutoSize = true;
checkboxToAdd.Enabled = true;
checkboxToAdd.Checked = true;
checkboxToAdd.CheckState = System.Windows.Forms.CheckState.Checked;
checkboxToAdd.Size = new System.Drawing.Size(84, 21);
checkboxToAdd.UseVisualStyleBackColor = true;
checkboxToAdd.Name = "chboxCountry" + chbx.Key;
checkboxToAdd.Text = chbx.Value;
checkboxToAdd.TabIndex = index + 1;
index++;
generatedCheckboxes.Add(checkboxToAdd);
}
}
return generatedCheckboxes;
}
And it's used in the FormLoad method:
var checkboxesForPanel = GetGeneratedCheckboxes();
foreach (var checkbox in checkboxesForPanel)
{
this.flowLayoutPanelForCheckBoxes.Controls.Add(checkbox);
}

Code looks fine for me. It has no issue. Check if your flow layout panel is in disabled state some how. Check in designer or check your code if its getting in disabled state.

Related

Check all items in a specified checked list box c#

I have created a small kitchen display program that display food orders. So I created dynamically a panel that contains a table layout panel that contains a checked list box and a check all button . My problem is... I have a check all button in each table layout panel created dynamically and every time I click it, it checks all items in the last created CheckedListBox not the clicked one.
This is my code:
p = new Panel();
p.Size = new System.Drawing.Size(360, 500);
p.BorderStyle = BorderStyle.FixedSingle;
p.Name = "panel";
tpanel = new TableLayoutPanel();
tpanel.Name = "tablepanel";
clb = new CheckedListBox();
tpanel.Controls.Add(b1 = new Button() { Text = "CheckAll" }, 1, 4);
b1.Name = "b1";
b1.Click += new EventHandler(CheckAll_Click);
b1.AutoSize = true;
private void CheckAll_Click(object sender, EventArgs e)
{
var buttonClicked = (Button)sender;
var c = GetAll(this, typeof(CheckedListBox));
for (int i = 0; i < c.Count(); i++)
{
\\any help
}
}
public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type)).Concat(controls).Where(c =>
c.GetType() == type);
}
First I will describe the struct
Order = TableLayoutPanel
TableLayoutPanel has 1 CheckAll Button and CheckListBox
And you want when you click to CheckAll Button it will checks exactly all items in current TableLayoutPanel.
So try this code
class XForm : Form {
// create Dictionary to store Button and CheckListBox
IDictionary<Button, CheckListBox> map = new Dictionary<Button, CheckListBox> ();
// when you create new order (new TableLayoutPanel)
// just add map Button and CheckListBox to map
private void CreateOrder () {
var panel = new Panel ();
panel.Size = new System.Drawing.Size (360, 500);
panel.BorderStyle = BorderStyle.FixedSingle;
panel.Name = "panel";
var table = new TableLayoutPanel ();
var checklistBox = new CheckedListBox ();
var button = new Button () { Text = "CheckAll" };
table.Controls.Add (button, 1, 4);
button.Name = "b1";
button.Click += new EventHandler (CheckAll_Click);
button.AutoSize = true;
map[button] = checklistBox;
}
// and on event handle
private void CheckAll_Click (object sender, EventArgs e) {
var buttonClicked = (Button) sender;
var c = map[buttonClicked];
if (c == null) return;
for (int i = 0; i < c.Items.Count; i++)
{
c.SetItemChecked(i, true);
}
}
}
And dont for get remove it from map when remove the order.
Hope it helps

How to add radio buttons dynamically in Windows form?

I need to add radio buttons dynamically in my windows form and in horizontal mode.
for (int i = 0; i <= r.Count; i++)
{
RadioButton rdo = new RadioButton();
rdo.Name = "id";
rdo.Text = "Name";
rdo.ForeColor = Color.Red;
rdo.Location = new Point(5, 30 );
this.Controls.Add(rdo);
}
You could do something like this:
FlowLayoutPanel pnl = new FlowLayoutPanel();
pnl.Dock = DockStyle.Fill;
for (int i = 0; i < 4; i++)
{
pnl.Controls.Add(new RadioButton() { Text = "RadioButton" + i });
}
this.Controls.Add(pnl);
You could also add the FlowLayoutPanel in the designer and leave that part out in the code.
To get the selected RadioButton use a construct like this:
RadioButton rbSelected = pnl.Controls
.OfType<RadioButton>()
.FirstOrDefault(r => r.Checked);
To use this the FlowLayoutPanel needs to be known in the calling method. So either add it to the Form in the designer (Thats what I would prefer) or create it as an instance member of the form and add it at runtime (this has no benefit).
You can do something like this
//This is my dynamic data list
List<ItemType> itemTypeData = new List<ItemType>()
RadioButton[] itemTypes = new RadioButton[ItemType.Count];
int locationX = 0;
for (int i = 0; i < ItemType.Count; i++)
{
var type = ItemType[i];
itemTypes[i] = new RadioButton
{
Name = type.Code,
Text = type.Code,
AutoSize = true,
Font = new System.Drawing.Font("Calibri", 11F, FontStyle.Regular),
Location = new Point(156 + locationX, 88),
};
this.Controls.Add(itemTypes[i]);
locationX += 80;
}
This works fine for me

asp.net c# multiple PlaceHolders on page with multiple dynamic buttons

Im creating dynamic buttons and adding unique ones to 2 different placeholders. The click event is added correctly to each button but the buttons click event is not firing on the 2nd placeholder. If i change the buttons to be just add to the 1 placeholder they all fire correctly. I can alter things around to get round this but im curious as to why the 2nd placeholder is an issue. Code below:
code behind:
//Create Colour Button
RadButton buttonColour = new RadButton();
buttonColour.EnableEmbeddedBaseStylesheet = false;
buttonColour.EnableEmbeddedSkins = false;
buttonColour.CssClass = "VariationButtonsColour";
buttonColour.BackColor = System.Drawing.Color.White;
buttonColour.Height = new Unit(45);
buttonColour.Click += new EventHandler(ColourClick);
buttonColour.Text = Colour;
buttonColour.ButtonType = RadButtonType.SkinnedButton;
buttonColour.ID = dr["Colour"].ToString() ;
buttonColour.PressedCssClass = "VariationButtonsColourActive";
foreach (Control ctrl in phColourButtons.Controls)
{
if (ctrl is RadButton)
{
RadButton button = (RadButton)ctrl;
if (buttonColour.ID == button.ID)
{
ColourExists = ColourExists + 1;
}
}
}
if (ColourExists == 0)
{
phColourButtons.Controls.Add(buttonColour);
Label spacer = new Label();
spacer.Width = new Unit(15);
spacer.Text = " ";
phColourButtons.Controls.Add(spacer);
}
//Create Size Button
RadButton buttonSize = new RadButton();
buttonSize.EnableEmbeddedBaseStylesheet = false;
buttonSize.EnableEmbeddedSkins = false;
buttonSize.CssClass = "VariationButtonsSize";
buttonSize.BackColor = System.Drawing.Color.White;
buttonSize.Height = new Unit(45);
buttonSize.Click += new EventHandler(SizeClick);
buttonSize.Text = Size;
buttonSize.ButtonType = RadButtonType.SkinnedButton;
buttonSize.ID = dr["Size"].ToString();
buttonSize.PressedCssClass = "VariationButtonsSizeActive";
foreach (Control ctrl in phSizeButtons.Controls)
{
if (ctrl is RadButton)
{
RadButton button = (RadButton)ctrl;
if (buttonSize.ID == button.ID)
{
SizeExists = SizeExists + 1;
}
}
}
if (SizeExists == 0)
{
phSizeButtons.Controls.Add(buttonSize);
Label spacer = new Label();
spacer.Width = new Unit(15);
spacer.Text = " ";
phSizeButtons.Controls.Add(spacer);
}
If someone could explain why the 2nd placeholder is not working that would be great.

c# How to change the visible property of a label created at runtime

//Here I create the labels at runtime in one click
Label[] labels = new Label[countresult];
for (int i = 1; i < countresult; i++)
{
labels[i] = new Label();
labels[i].Font = new Font("Arial Rounded MT Bold", 30);
labels[i].ForeColor = System.Drawing.Color.Red;
labels[i].AutoSize = true;
labels[i].Text = "";
//Here I try to assign the value visible = true
labels[i].Visible = true;
labels[i].TabIndex = i;
}
//In a private void of a timer tick I assign the name of label to var "a" and I do the 3 methods
string a = string.Format("labels[{0}]", labelscount);
//1st method
if (this.Controls.ContainsKey(a))
{
this.Controls[a].Visible=false;
}
//2nd method
foreach (Control control in Controls)
{
if (control.Name == a)
{
control.Visible = false;
}
}
//3rd method
if (this.Controls[a] is Label) this.Controls[a].Visible=false;
labelscount++;
Unfortunately none works.
Someone know What's happened?
You are not adding the labels to the owning control. So they will never be displayed. So in your loop you need to add the following as the last line...
this.Controls.Add(labels[i]);

How Can I Implement the Grid-like Control Using C# 2010 Express

I need a control like this:
This is from the Microsoft Word: Insert => Symbols.
This dialog has a grid-like control with a list of Unicode Characters;
You can select any character;
The selected character's Unicode is displayed;
User cannot edit the character;
Besides, I need to extend it's feature:
1. User can delete the cell with the selected character;
2. User can add a list of characters (from a file or whatever).
I'm asking what built-in controls I should use to implement this specific control.
Thanks.
Peter
I was able to create a simple mock up using the standard DataGridView control.
private void InitilizeDataGridView(DataGridView view)
{
var defaultCellStyle = new DataGridViewCellStyle();
defaultCellStyle.ForeColor = SystemColors.ControlText;
defaultCellStyle.WrapMode = DataGridViewTriState.False;
defaultCellStyle.SelectionBackColor = SystemColors.Highlight;
defaultCellStyle.BackColor = System.Drawing.SystemColors.Window;
defaultCellStyle.SelectionForeColor = SystemColors.HighlightText;
defaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
defaultCellStyle.Font = new Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((0)));
view.DefaultCellStyle = defaultCellStyle;
view.MultiSelect = false;
view.RowHeadersVisible = false;
view.AllowUserToAddRows = false;
view.ColumnHeadersVisible = false;
view.AllowUserToResizeRows = false;
view.AllowUserToDeleteRows = false;
view.AllowUserToOrderColumns = true;
view.AllowUserToResizeColumns = false;
view.BackgroundColor = SystemColors.Control;
for(var i = 0; i < 16; i++)
{
view.Columns.Add(new DataGridViewTextBoxColumn { AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, Resizable = DataGridViewTriState.False });
}
DataGridViewRow row = null;
for (int index = 32, cell = 0; index < 255; index++, cell++)
{
if(cell % 16 == 0)
{
if(row != null)
{
view.Rows.Add(row);
}
row = new DataGridViewRow { Height = 40 };
row.CreateCells(view);
cell = 0;
}
if (row != null)
{
row.Cells[cell].Value = Char.ConvertFromUtf32(index);
}
}
}
In WinForms, you can add fixed-size Label controls to a FlowLayoutPanel at runtime.
Note that this will not scale well; do not make thousands of Label controls.
If you want large numbers of characters, you can make a single screenfull of labels, then add a ScrollBar control and handle the Scroll event to change the label captions.

Categories