CheckedListBox , Populating a row of Checkboxes - c#

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");

Related

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...
};

accessing dynamically created controls C#

I have been reading for a bit and found some things close, but not anything works in my case. The user has a settings file that they use and I read it into the Windows form. One tab has 2 standard columns but then there can be any number more after those two which have different labels and listbox names that are based on the label (i.e. if the label is "portland" the corresponding listbox is "lstportland" but these names WILL vary). There are created in the is part of the settings file import method, on the fly:
for (int i = 3; i < (lastColumn); i++)
{
//creates new list box and names it the column name with a "lst" infront of it
var cellVal = squidSheet.Cells[1, i].Value;
string convertString = cellVal.ToString();
string listBoxName = "lst" + convertString;
int lbLocation = new int();
/*where to place the next label/listbox on the sheet based on a placement point if its the first one
*it is placed in a specific spot, then each subsequent one is placed equidistant from the last.*/
if (i==3)
{ lbLocation = 382; }
else
{ lbLocation = 382 + (115*(i-3)); }
//create the properties for the new listbox and label in its proper place on the "Run Tab"
ListBox listBox1 = new ListBox();
Label label1 = new Label();
listBox1.Name = listBoxName;
listBox1.Height = 316;
listBox1.Width = 94;
listBox1.Location = new Point(lbLocation, 30);
label1.Location = new Point(lbLocation, 14);
label1.Text = convertString;
label1.Name = "lbl" + convertString;
label1.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
//add the new listbox and label to the Form
tabPage4.Controls.Add(listBox1);
tabPage4.Controls.Add(label1);
//fill the new list box
string colIdString = TestCase((i-1).ToString());
fillListBox(listBox1, lastRowRunList, squidSheet, colIdString);
}
In a Later method, I need to read the items of each listbox created on the fly, into its own array or somehow access the items in the listbox itself. I have sketched the following but it doesn't work. Any ideas?
for (int l = 2; l < (listBoxNames.Count); l++)
{
string variableNameString = labelText[l].ToString();
string variableNames = "#" + variableNameString + "#";
ListBox listboxTest = Controls[("lst" + variableNameString)] as ListBox;
string variableValues = listboxTest.Items[(l-1)].ToString();
readText = readText.Replace(variableNames, variableValues);
}
The control isn't being found because you are searching the Form's Controls() collection, instead of its actual container, tabPage4.
Change:
ListBox listboxTest = Controls[("lst" + variableNameString)] as ListBox;
To:
ListBox listboxTest = tabPage4.Controls[("lst" + variableNameString)] as ListBox;
Or search for the control as in the link provided by drzounds in the comments.

How to name checked list box in C# dynamically?

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);

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.

Categories

Resources