I want to save some selected controls in a list and then compare, i can only select two so it's okay.
Each of the controls have a boolean property Selected, and i need the : Selected == True.
How do i create a method that returns the Selected controls?
Something like:
foreach (Control c in Controls)
{
if ( this.Selected == true )
{
// how to add to a list
}
}
Using LINQ you can do it in one line, this will put all the selected controls into a list and return:
return Controls.Where(c => c.Selected).ToList();
If you want to use foreach just create a list to store controls and populate your list with selected controls:
var selectedControls = new List<Control>();
foreach(var c in Controls)
{
if(c.Selected) selectedControls.Add(c);
}
return selectedControls;
Related
I'm looking for an effective way of clearing textboxes, ideally in a function.
I have tried using:
{
Action<Control.ControlCollection> func = null;
func = (controls) =>
{
foreach (Control control in controls)
if (control is TextBox)
(control as TextBox).Clear();
else
func(control.Controls);
};
func(Controls);
}
The problem with the above solution is that I could not choose which text boxes were to be deleted.
TextBoxName.Text = "";
The above works however the only problem is that it takes up 200 lines
I have 61 boxes, i need a clear all button, that only clears 60 boxes (all except one)
i need clear row buttons, since the boxes are arranged in rows
there are 15 clear row buttons, each with 4 boxes, is there a for loop i can use that will only clear the ones i need (by name if possible)?
You could create a collection of TextBoxes to exempted and filter based on it. For example,
var exceptionList = new[] { textBox1 };
foreach(var textBox in Controls.OfType<TextBox>().Where(x=> !exceptionList.Contains(x)))
{
textBox.Clear();
}
If you want to filter based on the Control Name, you would use
foreach(var textBox in Controls.OfType<TextBox>().Where(x=> !exceptionList.Contains(x.Name)))
{
textBox.Clear();
}
Where exceptionList is collection of names of TextBoxes that needs to be exempted.
I have a question regarding use of "Tag" :
I have a ListBox, or ListView, in which I have the name of my objects, I addes a "Tag" property to find its corresponding object :
foreach(Operation op_ass in ListOpAss1)
{
op_ass.getNom(Properties.Settings.Default.Langue);
ListViewItem item = new ListViewItem(op_ass.Nom);
item.Tag = op_ass;
listBoxAss1.Items.Add(op_ass.Nom);
}
Now what I would like, is when I select an item in my list(or several), make an action on corresponding objects. But how can I find them back?
For example I want to remove selected objects from a List, or get the list of Operation ID (without displaying ID in my list).
Looks like you are adding the property, op_ass.Nom into the listbox instead of the ListViewItem, item. Modify your code as follows:
foreach (Operation op_ass in ListOpAss1)
{
op_ass.getNom(Properties.Settings.Default.Langue);
ListViewItem item = new ListViewItem(op_ass.Nom);
item.Tag = op_ass;
// Add the list view item instead of op_ass.Nom
listBoxAss1.Items.Add(item);
}
Now you should be able to retrieve the tag from selected item/items as follows:
var operation = ((listBox1.SelectedItem as ListViewItem).Tag) as Operation;
Alternatively, you could think of using data binding as follows:
foreach (Operation op_ass in ListOpAss1)
{
op_ass.getNom(Properties.Settings.Default.Langue);
}
listBoxAss1.DataSource = ListOpAss1;
listBoxAss1.DisplayMember = "Nom";
And access the data bound object as follows:
var operation = listBox1.SelectedItem as Operation;
using foreach is kind of deprecated you can look into implemented functions in list of objects
ListOpAss1.ForEach(x=>
{
x.getNom(Properties.Settings.Default.Langue);
var item = new ListViewItem(x.Nom);
item.Tag = x;
listBoxAss1.Items.Add(x.Nom);
});
in order to select an item in a list you can use SingleOrDefalt() or Skip(count) take (count) for multiple files or you can run native querys with conditions to search the list like this
var items = collection.Where(x=> x.City == "Burgas").ToList(); //You can use select if you want only certain properties of the object to be selected
///then you can use that new item list to remove the objects from the collection list like this
items.ForEach(x=>
{
collection.Remove(x);
});
I have a tab control that has listboxes on it some of which are created and named dynamically so I can't statically program their name. Is there a way to create an array of all the list box names on a give tabPage? I have been going nuts trying to figure out a way to do it.
it would look something like this (based on a winforms example)
List<string> listBoxNames = new List<string>();
foreach (Control control in tabPage1.Controls)
{
if (control.GetType() == typeof(ListBox))
{
listBoxNames.Add(control.Name);
}
}
Or the same thing in linq syntax
List<string> listBoxNames = (from Control control in tabPage1.Controls
where control.GetType() == typeof (ListBox)
select control.Name).ToList();
if you want to find all the listbox's in the tabpage again then see below
foreach (var listBoxName in listBoxNames)
{
ListBox listBox = (ListBox) tabPage1.Controls.Find(listBoxName, true)[0];
}
I have more than 50 comboboxes on my form that have as items currencies (USD, EUR). Every currency has its own price TextBox.
What I want to do is sum price values based on currencies.
For example, if I have 20 USD I want to get a sum of those 20 price values.
How do I loop through the comboboxes?
You could use the .Text property of the comboboxes, as #AYETY suggested, but I suppose it depends on how you've populated them and if you'd rather use the value rather than the text, etc.
Anyway, you didn't say where the comboboxes are on the form, so if they are placed in containers, you need to recursively search through the controls on your form, e.g.
public IEnumerable<Control> GetChildControls(Control parentControl)
{
List<Control> controls = new List<Control>();
foreach (Control child in parentControl.Controls)
{
controls.AddRange(GetChildControls(child));
}
controls.Add(parentControl);
return controls;
}
and then interrogate the comboboxes. Based off using the Text property of the comboboxes, you could do something like this:
private void SumCurrencies()
{
var controls = GetChildControls(this);
foreach (var control in controls.Where(c => c is ComboBox))
{
if (control.Text == "USD")
{
// do something
}
else if (control.Text == "GBP")
{
// do something
}
else if (control.Text == "EUR")
{
// do something
}
}
}
int i = 0;
foreach (Control c in this.Controls)
{
if (c is ComboBox)
{
if (c.Text == 'USD')
i++;
}
}
I have a list box representing the contents of a direcory. I want to allow dropping only on those items that represent directories themselves. I've tried two approaches:
First i set the itemsource of the listbox to a compositeCollection of all directory contents and tried to iterate through those with:
foreach (ListBoxItem lbItem in directoryExplorer.Items)
{
MessageBox.Show(lbItem.DataContext.GetType().ToString());
}
The messagebox comes up with 'Directory' or 'UserFile' for each item. I was hoping to have access to the items, check what they represent, and set AllowDrop as necessary.
My second approach was to add individual items like so:
ListBoxItem nxt;
foreach (Directory d in dir.childdirs)
{
MessageBox.Show(d.name);
nxt = new ListBoxItem();
nxt.DataContext = d;
nxt.AllowDrop = true;
nxt.Name = d.name;
directoryExplorer.Items.Add(nxt);
}
foreach (UserFile f in dir.childfiles)
{
MessageBox.Show(f.name);
nxt = new ListBoxItem();
nxt.AllowDrop = false;
nxt.DataContext = f;
nxt.Name = f.name;
directoryExplorer.Items.Add(nxt);
}
but then it just comes up blank.
look at mdm20's answer wpf treeview blues. I want to select an item
Just make an allowDrop property and bind it appropriately.