How to name checked list box in C# dynamically? - c#

The names shouldn't change even when checked list boxes are sorted.
I tried to name them by using just concatenating strings but that didn't work at last. I need to name them like a id like "Polygon 1 , 2 etc " and display them accordingly.

This code set name of all CheckBoxes dynamically (WinForms):
public Form1()
{
InitializeComponent();
CheckBox[] chk = new CheckBox[10];
for (int i = 0; i < 10; i++)
{
chk[i] = new CheckBox();
chk[i].Name = "Polygon " + i + 1;
//Rest of your code
}
}
for CheckBoxList something like this (Web Application):
CheckBoxList chkList = new CheckBoxList();
CheckBox chk = new CheckBox();
chk.ID = "Polygon1";
chkList.Items.Add("chk");
myDiv.Controls.Add(chkList);

Related

CheckedListBox , Populating a row of Checkboxes

My application is running unstable because I have to many checkboxes in one form. I heard from some source, that I should try to populate the checkboxes in a CheckedListBox. But I dont know how i can realize that. Among I wanna show you my solution with CheckBox.
Can anyone show me how I can populate this with a CheckedListBox?
for (int i = 1; i <= maxTask; i++)
{
Label labelTasks = new Label();
labelTasks.Text = "A" + i;
labelTasks.Width = 28;
labelTasks.Height = 13;
labelTasks.Location = new Point(boundX_Label, boundY_Label);
boundX_Label += 26;
panel.Controls.Add(labelTasks);
CheckBox checkBox = new CheckBox();
checkBox.Name = String.Format("checkboxTask{0}", i);
checkBox.GotFocus += checkBox_GotFocus;
checkBox.LostFocus += checkBox_LostFocus;
checkBox.Text = "";
checkBox.Width = 20;
checkBox.Height = 15;
checkBox.Location = new Point(boundX_ChkBox, boundY_ChkBox);
boundX_ChkBox += 26;
checkBox.CheckedChanged += new EventHandler(checkBox_CheckedChanged);
panel.Controls.Add(checkBox);
}
This code will populate a row of checkboxes like showing among. My application have around 200 of this checkbox rows.
You can use a CheckedListBox for this, you can add items to
CheckedListBox by simply doing- CheckedListBox
CheckedListBox1.Items.Add("A1");

Dynamically creating checkboxes from datatable values

I am making a DTO creator. I am able get the table names and put them into datatable. The problem is, I must create checkboxes dynamically using these table names, and then be able to get whatever items checked. This is what I have been able to come up with so far:
for (int i = 0; i < dtable.Rows.Count; i++)
{
string cbName = dtable.Rows[i][0].ToString();
//Console.WriteLine(dtable.Rows[i][0]);
CheckBox box = new CheckBox();
box.Tag = i.ToString();
box.Text = cbName;
box.AutoSize = true;
box.Location = new Point(10, i * 50); //vertical
//box.Location = new Point(i * 50, 10); //horizontal
this.Controls.Add(box);
}
The dtable already has names and I create the checkboxes. However they are out the rendered area of the form, can see at most 10 of them. Also, how can I register which boxes are checked during runtime?
You can either put your CheckBoxes in a List and then count the checked ones.
List<CheckBox> lstBoxes = new List<CheckBox>();
// create box
...
lstBoxes.Add(box);
// Checking for checked boxes (eg. on form exit)
var checkedBoxes = lstBoxes.Where(b => b.Checked);
Or when you're creating your checkboxes, add an event on checked changed :
box.CheckedChanged += (sender, e) =>
{
var senderAsBox = sender as CheckBox;
if (senderAsBox == null) return;
var state = senderAsBox.Checked;
// Do you stuff then...
};

Remove a checkbox that is being created dynamically in a loop

I have a bunch of code that dynamicly creates some controls. It looks in a folder and lists the filenames in it. For each file in the folder it creates a checklistbox item, listbox item and two checkboxes. This is working great and as intended:
private void getAllFiles(string type)
{
try
{
string listPath = "not_defined";
if (type == "internal_mod")
{
int first_line = 76;
int next_line = 0;
int i = 0;
CheckBox[] chkMod = new CheckBox[100];
CheckBox[] chkTool = new CheckBox[100];
listPath = this.internalModsPath.Text;
string[] filesToList = System.IO.Directory.GetFiles(listPath);
foreach (string file in filesToList)
{
if (!internalModsChkList.Items.Contains(file))
{
internalModsChkList.Items.Add(file, false);
string fileName = Path.GetFileName(file);
internalModNameList.Items.Add(fileName);
//-----------------
// Draw Checkboxes
//-----------------
chkMod[i] = new CheckBox(); chkTool[i] = new CheckBox();
chkMod[i].Name = "modChk" + i.ToString(); chkTool[i].Name = "modChk" + i.ToString();
//chkMod[i].TabIndex = i; //chkTool[i].TabIndex = i;
chkMod[i].Anchor = (AnchorStyles.Left | AnchorStyles.Top); chkTool[i].Anchor = (AnchorStyles.Left | AnchorStyles.Top);
chkMod[i].Checked = true; chkTool[i].Checked = false;
chkMod[i].AutoCheck = true; chkTool[i].AutoCheck = true;
chkMod[i].Bounds = new Rectangle(549, first_line + next_line, 15, 15); chkTool[i].Bounds = new Rectangle(606, first_line + next_line, 15, 15);
groupBox7.Controls.Add(chkMod[i]); groupBox7.Controls.Add(chkTool[i]);
//-----------------
next_line += 15;
i++;
}
}
}
Now my problem is that I also want the user to be able to delete all these thing again based on the checklistbox' checked items.. I have no problems deleting the items in the checklistbox or the items in the listbox, but I want to remove the two checkboxes I create too ..
This is what I got to remove the items in the checklistbox, and the listbox
private void internalModListDel_btn_Click(object sender, EventArgs e)
{
int count = internalModsChkList.Items.Count;
for (int index = count; index > 0; index--)
{
if (internalModsChkList.CheckedItems.Contains(internalModsChkList.Items[index - 1]))
{
internalModsChkList.Items.RemoveAt(index - 1);
internalModNameList.Items.RemoveAt(index - 1);
groupBox7.Controls.Remove(modChk[index - 1]);
}
}
}
As you can see I have also tried to write something to remove the checkbox but it doesn't work and I have no idea how to make it work
Can you assist ?
Try using UserControls.
Use the ListBox controller to show those UserControls,
The user control can be built with those checkboxes, and the labels you want .
Another suggestion is to bind this list to an ObservableCollection which will contain the UserContorols you have created.
This way, it will be much more simlpe to add/remove/change the items inside.

get value of dynamically created textboxes

ASP.NET C#.
Inside UpdatePanel we have TextBox with OnTextChanged="text_changed" method and Panel.
if number 3 was typed at textbox, 3 textboxes below will appear inside Panel with different IDs.
However when button outside updatepanel clicks, dynamically created textboxes not found error occured.
How to get values of dynamically created textboxes?
Creating textbox:
protected void text_changed(Object sender, EventArgs e)
{
int n = Int32.Parse(TextBox6.Text);
Table table = new Table();
for (int i = 0; i < n; i++)
{
TableRow trow = new TableRow();
table.Rows.Add(trow);
TableCell tcell = new TableCell();
tcell.Text = (i + 1).ToString();
TextBox tb = new TextBox();
tb.ID = "TB" + i.ToString();
tcell.Controls.Add(tb);
trow.Cells.Add(tcell);
}
Panel1.Controls.Add(table);
ButtonClick //get values from created textboxes:
int n = Int32.Parse(TextBox6.Text);
for (int i = 0; i < n; i++)
{
string title = ((TextBox)UpdatePanel1.FindControl("Panel1").FindControl("TB" + i.ToString())).Text; //here null pointer exception..
}
where are you generating your textboxes? if you're creating them in text_changed event, then on the next post back your going to run into pagelife cycle issues. you'd need to cache the fact that you created them, and recreate them in the OnInit phase of the page.

How to write a list of array in a for loop?

Hi I neeed to do this one in a loop, but I don't have any idea how to do. I can't do this by just simply incrementing it.
CheckBox[] checkboxarray;
checkboxarray = new CheckBox[] {
txtChckBx0, txtChckBx1, txtChckBx2, txtChckBx3, txtChckBx4, txtChckBx5,
txtChckBx6, txtChckBx7, txtChckBx8, txtChckBx9, txtChckBx10, txtChckBx11,
txtChckBx12, txtChckBx13, txtChckBx14, txtChckBx15, txtChckBx16, txtChckBx17,
txtChckBx18, txtChckBx19, txtChckBx20, txtChckBx21, txtChckBx22, txtChckBx23,
txtChckBx24, txtChckBx25, txtChckBx26, txtChckBx27, txtChckBx28, txtChckBx29,
txtChckBx30, txtChckBx31, txtChckBx32, txtChckBx33, txtChckBx34, txtChckBx35,
txtChckBx36, txtChckBx37, txtChckBx38, txtChckBx39, txtChckBx40, txtChckBx41,
txtChckBx42, txtChckBx43, txtChckBx44, txtChckBx45, txtChckBx46, txtChckBx47,
txtChckBx48, txtChckBx49, txtChckBx50, txtChckBx51, txtChckBx52, txtChckBx53,
txtChckBx54, txtChckBx55, txtChckBx56, txtChckBx57, txtChckBx58, txtChckBx59,
txtChckBx60, txtChckBx61, txtChckBx62, txtChckBx63, txtChckBx64, txtChckBx65,
txtChckBx66, txtChckBx67, txtChckBx68, txtChckBx69, txtChckBx70, txtChckBx71,
txtChckBx72, txtChckBx73, txtChckBx74, txtChckBx75, txtChckBx76, txtChckBx77,
txtChckBx78, txtChckBx79, txtChckBx80
};
If you know that the checkboxes are all on a form:
var list = new List<CheckBox>();
foreach(var control in this.Controls)
{
var checkBox = control as CheckBox;
if(checkBox != null)
{
list.Add(checkBox);
}
}
var checkBoxArray = list.ToArray();
If you don't know where the controls are then you will have to search for them.
BTW: The code above uses WinForms. If you are using WPF, Silverlight, Metro,... the container will be named differently.
You can't do new and then
checkboxarray = new CheckBox[] { txtChckBx0, ....}
it's two different ways to define an array.
you need to do:
CheckBox[] checkboxarray = { txtChckBx0, ....};
If you want it to work.
Good luck.
In WinForm
List<CheckBox> checkBox = new List<CheckBox>();
// Adding checkboxes for testing...
for (int i = 0; i <= 80; i++)
{
var cbox = new CheckBox();
cbox.Name = "txtChckBx"+ i.ToString();
checkBox.Add(cbox);
Controls.Add(cbox);
}
List<CheckBox> checkBoxfound = new List<CheckBox>();
// loop though all the controls
foreach (var item in Controls)
{
// filter for checkboxes and name should start with "txtChckBx"
if (item is CheckBox && ((CheckBox)item).Name.StartsWith("txtChckBx", StringComparison.OrdinalIgnoreCase))
{
checkBoxfound.Add((CheckBox)item);
}
}

Categories

Resources