Looping through tabpages of tabcontrol in c# - c#

I am trying to Loop through the tabpages of a tabcontrol in c#. Each tabpage has multiple textboxes. The purpose is to sum up all of the values of these textboxes.
double sum = 0;
foreach(TabPage tbp in tabControl1.TabPages)
{
foreach(System.Windows.Forms.Control c in tbp.Controls)
{
if (c.ToString() == "TextBox")
{
sum += Convert.ToDouble(c.Text);
}
}
}
When I execute this code, the first foreach loop is entered three times even though I have one TabPage in my TabControl. Furthermore, the if statement is not entered, so there seems to be something wrong with that as well.

This code (C#7) allows you to safety sum all text boxes values in a tab control, include text boxes nested in children container controls (example: a text box in panel, and the panel in a tab page)
private double SumTextBoxesValues()
{
double sum = 0;
foreach (TabPage tbp in tabControl1.TabPages)
{
var textBoxes = GetAllTextBoxes(tbp);
foreach (var textBox in textBoxes)
{
if(double.TryParse(textBox.Text, out double value))
sum += value;
}
}
return sum;
}
private static IEnumerable<TextBox> GetAllTextBoxes(Control container)
{
var controlList = new List<TextBox>();
foreach (Control c in container.Controls)
{
controlList.AddRange(GetAllTextBoxes(c));
if(c is TextBox box)
controlList.Add(box);
}
return controlList;
}

Related

Find a ListView control in tab page

I have 2 listviews in tab page. however I am looking for a function to find the right control by name:
I have
foreach (Control c in form.Controls) // loop through form controls
{
if (c is TabControl)
{
TabControl f = (TabControl)c;
foreach (Control tab in f.Controls)
{
TabPage tabPage = (TabPage)tab;
foreach (Control control in tabPage.Controls)
{
MessageBox.Show(control.Name);
// code to go here
}
}
}
}
The Controls collection has a Find function that returns an array:
Control[] ctrls = this.Controls.Find("listView1", true);
if (ctrls.Length == 1) {
MessageBox.Show("Found " + ctrls[0].Name);
}
This will search for a control with the specified name in the controls of the specified control and all his sons.
public Control findControlbyName(String name, Control parent){
foreach (Control ctr in parent.Controls)
{
if (ctr.Name.Equals(name)) return ctr;
else return findControlbyName(name, ctr);
}
return null;
}
You just need to do:
findControlbyName("NameOfTheListView",this);

How can I loop through the selected listview item

When I double click on my selected listview item I can easily get each value and set them in a textbox by using the following code.
ListViewItem item = listView1.SelectedItems[0];
entry_txtBox.Text = item.Text;
name_txtBox.Text = item.SubItems[1].Text;
But I'd rather loop through the selected item and compare each column name to each textbox tag. If they match set the column value to the textbox.
This is what I have so far.
foreach (Control c in this.Controls)
{
foreach (Control childc in c.Controls)
{
foreach (ColumnHeader header in listView1.Columns) // This should be the selected item.
{
if (childc is TextBox && header == childc.Tag)
{
// Fill Textboxes
}
}
}
}
Again my question is how can I loop through each column header and compare them to a textbox tag.
First you can use good old recursion to build a flat list of all your controls up by traversing the control tree:
var controls = GetControls(parent);
public IEnumerable<Control> GetControls(Control parent)
{
foreach(var control in parent.Controls)
{
yield return control;
var childControls = GetControls(control);
foreach(var child in childControls)
{
yield return child;
}
}
}
Then just do a LINQ to filter:
var textBoxControls = controls.Select(c => c is TextBox && header == TextBox.ID); //Or whatever condition you want to use.

Check duplicate index value in listveiw

I have a listview in C# with columns(ProdcutId,ProductName,UnitPirce,Quantity). I am inserting data in it from text boxes(txtproid,txtproname,txtunitprice,txtquantity). All these txtboxes are inserted once on a button click. What I want here is when i click the button,it should check the ProductId column in listview. If it contain the value coming from 'txtproid', It should not add all txtboxes in listview instead should show a messaage "This Product is Already Included". It should not check the whole item in listv.
I have tried many things but invain like:
int c = 0;
if (listView1.Items.Count != 0)
{
foreach (ListViewItem lv in listView1.Items)
{
c = 0;
if (lv.SubItems[0].Text == cmbpid.SelectedItem.ToString())
{
Validations.ErrorMessage("Item already exists in the Cart");
c = 1;
}
}
}
else if (c == 0)
{
ListViewItem lvi = new ListViewItem(cmbpid.Text);
lvi.SubItems.Add(cmbpname.Text);
lvi.SubItems.Add(txtunitprice.Text);
lvi.SubItems.Add(txtproquantity.Text);
lvi.SubItems.Add(txtunittotal.Text);
lvi.SubItems.Add(txtbatch.Text);
listView1.Items.Add(lvi);
//clear fields
quvar = 0;
totalvar = 0;
txtproquantity.Clear();
txtunitprice.Clear();
txtunittotal.Clear();
GetListviewTotal();
txtbatch.Clear();
cmbpid.SelectedIndex = -1;
cmbpname.SelectedIndex = -1;
}
Your task is divided two small functions.
First, check duplicate product id.(Let's say CheckSameProductId())
Second, insert data to ListView.(Let's say InsertProductData())
So, below form can be.
if (CheckDuplicateId())
{
//show error text
}
else
{
//insert text boxes's data to List View
InsertProductData();
}
And then, each function can write, for example.
//check whether it's already inserted
bool CheckDuplicateId()
{
foreach (ListViewItem lv in listView1.Items)
{
if (lv.SubItems[0].Text == cmbpid.SelectedItem.ToString())
{
//there is a duplicate data
return true;
}
}
//there is no duplicate data
return false;
}
//insert data using text boxes to listview ctrl
void InsertProductData()
{
foreach (Control x in this.Controls)
{
if (x is TextBox)
{
//I'm not sure your control z-order
lvi.SubItems.Add(((TextBox)x).Text);
}
}
}
I've not compiled above code, so you have to check that.

Removing a TextBox from the Controls collection has no effect

Basically what I do is: Add (n) textboxes via a button click. When I again click on it, this code runs:
foreach (Control c in this.Controls)
{
TextBox tb = c as TextBox;
if (tb != null)
{
this.Controls.Remove(tb);
tb.Dispose();
}
}
I than again add (n) textboxes, but every second item from the old textboxes remains. Any ideas?
Removing items from the collection you are iterating on is a bad idea. Try this:
List<Control> toBeRemoved = new List<Control>();
foreach (Control c in this.Controls)
{
if (c instanceof TextBox)
{
toBeRemoved.Add(c);
}
}
foreach (Control c in toBeRemoved)
{
this.Controls.Remove(c);
c.Dispose();
}

Can't loop all controls in c#

I have a C# form with some types of controls, i throught this code for loop all Labels and re-parent:
private void MakeAllLabelTrans(Control frm, Control parent)
{
foreach (Control ctl in frm.Controls)
{
if (ctl is Label)
{
ctl.Parent = parent;
// ctl.BackColor = Color.Transparent;
}
else if (ctl.HasChildren)
{
MakeAllLabelTrans(ctl, parent);
}
}
}
and call as: MakeAllLabelTrans(this, picBackground); in Form_Load event, but some label was missed (i have puted a messagebox in the loop body - it really not in the loop), but i don't know why?
You're changing the collection while enumerating it. This leads to some items being skipped.
You should rewrite this to first build a list of controls that will need to be reparented, and then reparent them in a second pass.
You should not modify the collection on which you are iterating.
private static IEnumerable<Labels> FindAllLabels(Control container)
{
foreach (Control ctl in container.Controls)
{
var lbl = ctl as Label;
if (lbl != null)
{
yield return ctl;
}
else if (ctl.HasChildren)
{
foreach (var innerResult in FindAllLabels(ctl))
yield return innerResult;
}
}
}
// now call the method like this if you want to change a property on the label
foreach (var label in FindAllLabels(theForm))
label.BackgroundColor = Color.White;
// or like this (note the .ToList()) if you want to move the labels around:
foreach (var label in FindAllLabels(theForm).ToList())
label.Parent = someOtherControl;

Categories

Resources