How to count checked checkbox values in C# - c#

I'm new to C# and trying to learn a few things. For example, I can't get the process beneath together. What am I doing wrong?
The issue I get is that when all 4 checkboxes are selected I only get 5 as result as they are all 5 selected...? and must be 20 (4x5)
//CheckCheckedCheckboxes
int Totalchecked = 0;
if (CheckBox1.Checked)
{
Totalchecked = 5;
}
if (CheckBox2.Checked)
{
Totalchecked = 5;
}
if (CheckBox3.Checked)
{
Totalchecked = 5;
}
if (CheckBox4.Checked)
{
Totalchecked = 5;
}
var totalselected = Totalchecked ;
labelHeader.Text = Convert.ToString(totalselected);

private void button1_Click(object sender, EventArgs e)
{
int Totalchecked = 0;
int TotalValue = 0;
foreach (Control item in this.Controls)
{
if(item is CheckBox)
{
if (((CheckBox)item).Checked)
Totalchecked++;
}
}
//If you consider, each check box value as a 5, just multiply Totalchecked * 5
TotalValue = Totalchecked * 5;
MessageBox.Show(TotalValue.ToString());
}
But the above approach is not good when forms contain more than these checkboxes, So just add your checkboxes into the Group box and, you can get a count of the checked checkboxes as follows,
int Totalchecked = groupBox1.Controls.OfType<CheckBox>().Where(c => c.Checked).Count();
Updated: Based on your comment, you can set Tag value for a each Check box as,
checkBox1.Tag = 5;
checkBox2.Tag = 10;
int totalValueOfeachCheckboxes = 0;
foreach (Control item in this.Controls)
{
if(item is CheckBox)
{
if (((CheckBox)item).Checked)
{
totalValueOfeachCheckboxes = totalValueOfeachCheckboxes + Convert.ToInt32(item.Tag);
}
}
}
MessageBox.Show(totalValueOfeachCheckboxes.ToString());

You can loop over them and iterate on the controls on the form.
foreach(Control c in this.Controls)
{
if(c is CheckBox)
{
// Do stuff here/logic
}
}
Or do a more LINQ/Lamba-ish type approach of
var checkList = YourForm.Controls.OfType<CheckBox>().Where(x => x.Checked).ToList();
checkList.ForEach(x => {
//Do stuff here
});

Related

WinForms - Dynamically create textbox based on ComboBox selection

I am trying to create a TextBox based on the selection on ComboBox dynamically based on the following steps:
First step (Select a source from ComboBox):
Second step (Textbox should appear based on ComboBox.SelectedValue):
Last step (A new ComboBox should appear below):
I have created a createTextBox function using the following code:
public void createTextBox(int numPassenger)
{
TextBox[] passengerBoxes = new TextBox[numPassenger];
for (int u = 0; u < passengerBoxes.Count(); u++)
{
passengerBoxes[u] = new TextBox();
}
int i = 0;
foreach (TextBox txt in passengerBoxes)
{
string name = "passenger" + i.ToString();
txt.Name = name;
txt.Text = name;
txt.Location = new Point(244, 32 + (i * 28));
txt.Visible = true;
this.Controls.Add(txt);
i++;
}
}
Is there a way that I can modify my current function to adapt to the mentioned steps? Additionally, how can I find the dynamically created TextBox?
You can try the following code:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
createTextBox(sender as ComboBox);
}
private void createTextBox(ComboBox cmb)
{
TextBox passengerBoxes = new TextBox();
string name = cmb.Text;
if (Controls.Find(name, true).Length == 0)
{
passengerBoxes.Name = name;
passengerBoxes.Text = name;
int textBoxCount = GetTextBoxCount();
passengerBoxes.Location = new Point(244, 32 + (textBoxCount * 28));
passengerBoxes.Visible = true;
this.Controls.Add(passengerBoxes);
if (cmb.Items.Count != 1)//last item remaining then we should not create new combo box
{
ComboBox newCombo = new ComboBox
{
Location = new Point(cmb.Location.X, 32 + ((textBoxCount + 1) * 28))
};
foreach (string str in cmb.Items)
if (cmb.Text != str)
newCombo.Items.Add(str);
newCombo.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
this.Controls.Add(newCombo);
}
}
else
MessageBox.Show("Textbox Already for the selected source " + name);
}
private int GetTextBoxCount()
{
int i = 0;
foreach (Control ctl in this.Controls)
{
if (ctl is TextBox) i++;
}
return i;
}

C# Create Tracks Parents which Depend on Children

I'm trying to create tracks of Parent that have more than a child and put theme in dynamic ListBox
i have this ParentActivity table"
tblParentActivity
and I want to make tracks depend on ParentActivity table to be in ListBox like this:
Tracks in ListBox
the code so far:
private void TrackingActivity(long ParentActivityID)
{
DataTable dtActiveChild = objA.SelectActivityChild(ParentActivityID);
ListBox lstBox = new ListBox();
lstBox.ID = "lstTrack" + ParentActivityID.ToString();
lstBox.Width = 200;
pnlTrack.Controls.Add(lstBox);
for (int i = 0; i < dtActiveChild.Rows.Count; i++)
{
long ActivityChildID = Convert.ToInt64(dtActiveChild.Rows[i]["ActivityID"].ToString());
string ActivityChildName = dtActiveChild.Rows[i]["ActivityName"].ToString();
lstBox.Items.Add(new ListItem (ActivityChildName.ToString(),ActivityChildID.ToString()));
DataTable dtBrotherActivity = objA.selectBrotherActivity(ActivityChildID);
if (dtBrotherActivity.Rows.Count > 0)
{
TrackingActivity(ActivityChildID);
}
}
}
for example ParentActivityID=1;
selectBrotherActivity is query to get another child of it parent
it gave me distribution like this:
|1|2 3|4 5|7|9|10|7|9|10|6|8|9|10|
which || means ListBox
Ok thanks to everyone I found it:
protected void btnCreateTrack_Click(object sender, EventArgs e)
{
ListBox lstBoxParent = new ListBox();
lstBoxParent.ID = "lstTrack1";
lstBoxParent.Width = 200;
lstBoxParent.Height = 200;
pnlTrack.Controls.Add(lstBoxParent);
TrackingActivity(1, lstBoxParent );
}
private void TrackingActivity(long ParentActivityID, ListBox lstBoxParent)
{
chk:
DataTable dtActiveChild = objWFAI.SelectActivityChild(ParentActivityID);
string ActivityName = objWFA.CurrentActivityName(ParentActivityID);
lstBoxParent.Items.Add(new ListItem(ActivityName, ParentActivityID.ToString()));
for (int i = dtActiveChild .Rows.Count - 1; i >= 0; i--)
{
long ActivityChildID = Convert.ToInt64(dtActiveChild .Rows[i][" ActivityID"].ToString());
if (i != 0)
{
ListBox lstBoxChild = new ListBox();
lstBoxChild.ID = "lstTrack" + ActivityChildID.ToString();
lstBoxChild.Width = 200;
lstBoxChild.Height = 200;
pnlTrack.Controls.Add(lstBoxChild);
for (int p = 0; p < lstBoxParent.Items.Count; p++)
{
lstBoxChild.Items.Add(new ListItem(lstBoxParent.Items[p].Text, lstBoxParent.Items[p].Value));
}
TrackingActivity(ActivityChildID, lstBoxChild);
}
else
{
ParentActivityID = ActivityChildID ;
goto chk;
}
}

dynamically delete controls from table

Am trying to delete textboxes and labels while their names which have equal value from the selected items from the listbox. If i run this code only the first if statement is executed and removing only the label controls inside the table.
I must also mention that controls of the table are dynamically created.
private void pictureBox2_Click(object sender, EventArgs e)
{
for (int i = 0; i < listBox2.SelectedItems.Count; i++)
{
foreach (Control t in table2.Controls)
{
if (t is Label && t.Text==listBox2.SelectedItem.ToString())
{
table2.Controls.Remove(t);
continue;
}
if (t is TextBox && t.Name.Contains(listBox2.SelectedItem.ToString()))
{
table2.Controls.Remove(t); continue;
}
}
listBox2.Items.Remove(listBox2.SelectedItems[i]); i--;
}
}
This is how controls are created indide the table.
private void pictureBox1_Click(object sender, EventArgs e)
{
listBox2.Items.Clear();
this.table2.Controls.Clear();
foreach (var item in listBox1.SelectedItems)
{
table2.Controls.Add(new Label() { Name = item.ToString(), Text = item.ToString(), AutoSize = true });
table2.Controls.Add(new TextBox() { Name = item.ToString(), AutoSize = true });
}
}
}
When you remove an item from a collection (suppose the item at position 0), the item at the next position (postion 1) shifts in position zero. But your for loop execute the next iteration and your indexer becomes 1 and so it terminate the loop.
The first approach to avoid this is to loop in reverse order, from the end of the collection toward the begin of it
But you could also simplify a lot your code with
private void pictureBox2_Click(object sender, EventArgs e)
{
for (int i = listBox2.SelectedItems.Count - 1 ; i >= 0 ; i--)
{
// This is our search term...
string curItem = listBox2.SelectedItems[i].ToString();
// Get only the controls of type Label with Text property equal to the current item
var labels = table2.Controls
.OfType<Label>()
.Where (c => c.Text == curItem)
.ToList();
if(labels != null)
{
for(int x = labels.Count()-1; x >= 0; x--)
table2.Remove(labels[x]);
}
// Get only the controls of type TextBox with Name property containing the current item
var boxes = table2.Controls
.OfType<TextBox>()
.Where (c => c.Name.Contains(curItem)
.ToList();
if(boxes != null)
{
for(int x = boxes.Count()-1; x >= 0; x--)
table2.Remove(boxes[x]);
}
listBox2.Items.Remove(curItem);
}
}
Why are you decrementing your iterator at the end of your for loop? It looks like you're stuck in the loop, buddy.

GridView missing data between postbacks

So basically I am doing a manage packaging item system. The scenario is, when I select from gvSPU, gvFinalised should display the item in database. After that, when I add new item into the gvFinalised, it supposed to keep stacking up the items instead of wiping out the previous record and display the added latest one. Here is the code:
private List<DistributionStandardPackingUnitItems> tempDistSPUI
{
get
{
if (ViewState["tempDistSPUI"] == null)
{
return new List<DistributionStandardPackingUnitItems>();
}
else
{
return (List<DistributionStandardPackingUnitItems>)ViewState["tempDistSPUI"];
}
}
set
{
ViewState["tempDistSPUI"] = value;
}
}
protected void gvSPU_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
int rowNo = int.Parse(e.CommandArgument.ToString());
SPUname = this.gvSPU.DataKeys[rowNo].Value.ToString();
lblSPUname.Text = SPUname;
List<DistributionStandardPackingUnitItems> templist = new List<DistributionStandardPackingUnitItems>();
templist = tempDistSPUI;
templist = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);
gvFinalised.DataSource = templist;
gvFinalised.DataBind();
this.tempDistSPUI = templist;
}
When gvSPU row is selected, it should store all the records into templist and display in gvFinalised.
List<string> prodVariantIDList = new List<string>();
private List<string> SelectedVariantDetailIDs
{
get
{
if (ViewState["SelectedVariantDetailIDs"] == null)
{
return new List<string>();
}
else
{
return (List<string>)ViewState["SelectedVariantDetailIDs"];
}
}
set
{
ViewState["SelectedVariantDetailIDs"] = value;
}
}
protected void lbnAdd_Click(object sender, EventArgs e)
{
List<ProductPacking> prodVariantDetail = new List<ProductPacking>();
int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
// get the last product variant IDs from ViewState
prodVariantIDList = this.SelectedVariantDetailIDs;
foreach (RepeaterItem ri in Repeater1.Items)
{
GridView gvProduct = (GridView)ri.FindControl("gvProduct");
foreach (GridViewRow gr in gvProduct.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
//Prevent gvFinalised to store duplicate products
if (cb.Checked && !prodVariantIDList.Any(i => i == gvProduct.DataKeys[gr.RowIndex].Value.ToString()))
{
// add the corresponding DataKey to idList
prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
}
}
}
for (int i = 0; i < prodVariantIDList.Count; i++)
{
prodVariantDetail.Add(prodPackBLL.getProdVariantDetailByID(prodVariantIDList[i]));
}
gvFinalised.DataSource = prodVariantDetail;
gvFinalised.DataBind();
// save prodVariantIDList to ViewState
this.SelectedVariantDetailIDs = prodVariantIDList;
}
When add button is on click, it should append with the record in gridview instead of wiping them off and insert the checked one.
I am using the viewState to store the record between postbacks. However, when I try to add new item into gvFinalised, the previous record which in the templist will disappear and the gvFinalised will be populated with the result from prodVariantDetail list.
Any guide? Thanks in advance.
EDIT
List<DistributionStandardPackingUnitItems> itemList = new List<DistributionStandardPackingUnitItems>();
protected void gvSPU_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
int rowNo = int.Parse(e.CommandArgument.ToString());
SPUname = this.gvSPU.DataKeys[rowNo].Value.ToString();
lblSPUname.Text = SPUname;
itemList = tempDistSPUI;
itemList = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);
gvFinalised.DataSource = itemList;
gvFinalised.DataBind();
this.tempDistSPUI = itemList;
}
protected void lbnAdd_Click(object sender, EventArgs e)
{
List<DistributionStandardPackingUnitItems> prodVariantDetail = new List<DistributionStandardPackingUnitItems>();
int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
// get the last product variant IDs from ViewState
prodVariantIDList = this.SelectedVariantDetailIDs;
foreach (RepeaterItem ri in Repeater1.Items)
{
GridView gvProduct = (GridView)ri.FindControl("gvProduct");
foreach (GridViewRow gr in gvProduct.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
//Prevent gvFinalised to store duplicate products
if (cb.Checked && !prodVariantIDList.Any(i => i == gvProduct.DataKeys[gr.RowIndex].Value.ToString()))
{
// add the corresponding DataKey to idList
prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
}
}
}
for (int i = 0; i < prodVariantIDList.Count; i++)
{
prodVariantDetail.Add(packBLL.getProdVariantDetailByID(prodVariantIDList[i]));
}
for (int j = 0; j < itemList.Count; j++)
{
prodVariantDetail.Add(itemList[j]);
}
gvFinalised.DataSource = prodVariantDetail;
gvFinalised.DataBind();
}
// save prodVariantIDList to ViewState
this.SelectedVariantDetailIDs = prodVariantIDList;
}
You have to add the elements of tempDistSPUI into prodVariantDetail before binding prodVariantDetail to gvFinalised.
Here's what I would do using List.AddRange Method:
protected void lbnAdd_Click(object sender, EventArgs e)
{
List<DistributionStandardPackingUnitItems> prodVariantDetail = new List<DistributionStandardPackingUnitItems>();
int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
// get the last product variant IDs from ViewState
prodVariantIDList = this.SelectedVariantDetailIDs;
foreach (RepeaterItem ri in Repeater1.Items)
{
GridView gvProduct = (GridView)ri.FindControl("gvProduct");
foreach (GridViewRow gr in gvProduct.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
//Prevent gvFinalised to store duplicate products
if (cb.Checked && !prodVariantIDList.Any(i => i == gvProduct.DataKeys[gr.RowIndex].Value.ToString()))
{
// add the corresponding DataKey to idList
prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
}
}
}
for (int i = 0; i < prodVariantIDList.Count; i++)
{
prodVariantDetail.Add(packBLL.getProdVariantDetailByID(prodVariantIDList[i]));
}
// get the content of tempDistSPUI from ViewState
itemList = this.tempDistSPUI;
// add all elements of itemList to prodVariantDetail
prodVariantDetail.AddRange(itemList);
gvFinalised.DataSource = prodVariantDetail;
gvFinalised.DataBind();
// save prodVariantIDList to ViewState
this.SelectedVariantDetailIDs = prodVariantIDList;
}

How to store name of dynamic checkbox in an String array

How can store name of dynamically created checkbox in a String array when I don't know how many checkbox will user select at runtime.
Say I have 10 dynamic checkboxes and out of 10 user select 6 checkboxes randomly now how can get the name of those selected checkboxes and store them in a String array.
I know how to use event handler on dynamic check box but confused how to declare Straing array when I don't know what will be be size of an array.
Here what I have done till now -
private void CheckBoxCheckedChanged(object sender, EventArgs e)
{
CheckBox c = (CheckBox)sender;
//Label myLabel;
String str = null;
if (c.Checked == true)
{
str = c.Text;
gpBox[gpcount] = new GroupBox();
gpBox[gpcount].Name = "gpBox" + Convert.ToString(count);
gpBox[gpcount].Text = str;
gpBox[gpcount].Location = new Point(5, gpposition);
gpBox[gpcount].AutoSize = true;
this.Controls.Add(gpBox[gpcount]);
aCommand3 = new OleDbCommand("select * from batch_tbl where batch_branch LIKE '" + str + "'", main_connection);
aAdapter3 = new OleDbDataAdapter(aCommand3);
ds3 = new DataSet();
aAdapter3.Fill(ds3, "app_info");
ds3.Tables[0].Constraints.Add("pk_bno", ds3.Tables[0].Columns[0], true);
int batch_count = ds3.Tables[0].Rows.Count;
batchCheckBox = new CheckBox[batch_count];
//filling the groupbox with batch code by generating dynamic checkboxes
for (int j=0; j < batch_count; ++j)
{
batchCheckBox[j] = new CheckBox();
batchCheckBox[j].Name = "batch" + Convert.ToString(k);
batchCheckBox[j].Text = ds3.Tables[0].Rows[j][1].ToString();
Console.WriteLine(batchCheckBox[j].Text);
batchCheckBox[j].Location = new System.Drawing.Point(104 * position, 30);
gpBox[gpcount].Controls.Add(batchCheckBox[j]);
batchCheckBox[j].CheckStateChanged += new System.EventHandler(BatchBoxCheckedChanged);
position++;
count++;
Console.WriteLine(batchCheckBox[j].Name);
k++;
}
position = 1;
gpposition += 100;
}
else
{
count--;
this.Controls.RemoveByKey("lbl" + c.Name);
this.Update();
}
}
int total_batch = 1;
string[] batchname;
private void BatchBoxCheckedChanged(object sender, EventArgs e)
{
CheckBox batchBox = (CheckBox)sender;
//Here I want to store name of checkbox in array
if (batchBox.Checked == true)
{
batchname = new String[total_batch];
total_batch++;
}
else
{
}
}
You can try this:
//Gets all checkbox's on the form
List<CheckBox> chks = Controls.OfType<CheckBox>().ToList();
//take only those who is checked, and select only their name property
List<string> names = chks.Where(c => c.Checked).Select(c => c.Name).ToList();
UPDATE
For testing you could print a list of the selected names:
string txt = "";
foreach(string name in names)
{
txt += name+" \n\r";
}
MessageBox.Show(txt);
Thank you everbody
}
list = new List<string>();
}
private void BatchBoxCheckedChanged(object sender, EventArgs e)
{
CheckBox batchBox = (CheckBox)sender;
//Here I want to store name of checkbox in array
if (batchBox.Checked == true)
{
list.Add(batchBox.Text);
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach(string prime in list) // Loop through List with foreach
{
Console.WriteLine(prime);
}
}
This is Done

Categories