I have a list
List<Control> inputBoxes = new List<Control>();
where I have added comboboxes and textboxes.
I can set the text property with inputBoxes[0].GetType().GetProperty("Text").SetValue(inputBoxes[0], "ABC", null);
but how can I add items to the comboboxes and select them?
Can I use inputBoxes[0].GetType().GetMethod() somehow?
Why do you use reflection to simply set a property?
You can use this which is much more efficient and less error-prone:
inputBoxes.OfType<TextBox>().ElementAt(0).Text = "ABC";
If you want to add items to one (or multiple) ComboBoxes:
var combos = inputBoxes.OfType<ComboBox>();
foreach(ComboBox combo in combos)
{
// add items here or set their DataDource, for example:
string[] installs = new string[]{"Typical", "Compact", "Custom"};
combo.Items.AddRange(installs);
}
Note that you need to add using System.Linq for OfType
Related
Hello I have set of check boxes that are generated from source code and not in XAML. Now, I wanted to set x:Name during creation of it so that I can loop through each element using the FindByName. I planned on setting name like checkbox1, checkbox2, checkbox3...
So that I can use this FindByName
foreach (var qaItem in question.Entity.QuestionAnswer)
{
(FindByName($"checkbox{qaItem.OrderNum}") as CheckBox).IsChecked = false;
}
Is there a way to set "x:Name" in source? since the number of checkboxes is not definite.
x:Name is not actually a property, it is just a XAML helper that is used to create a C# variable name. If you are creating the elements at runtime, you need to maintain an array or list in order to keep track of them
you could do something like this
Dictionary<string,Checkbox> myCheckboxList = new Dictionary<string,Checkbox>();
then as you create each checbox
var cb = new Checkbox();
... set properties, etc ...
myCheckboxList.Add(qaItem.OrderNum, cb);
I have a windows form with many comboboxes. They all have to display the same items and I want to be able to remove items from their list of values. So I decided to try and make a List variable so I could easily remove and insert values into it. So what I did was
List<string> Ranks = new List<string>(new string[] { "values here" });
Then in my Form1Designer.cs
this.ComboBox_Rank_0.DataSource = Ranks;
I heart ValueMember and DisplayMember were good things to have, but so far it works without them. When compiled the form loads and the comboboxes have the correct values.
The problem is that when I choose a value in one combobox the others get the same value selected, as well. Any ideas?
Use a BindingList<T>
This class allows you to handle the interaction with your list separately for each of the combos
For example
List<string> names = new List<string>()
{"Steve", "Mark", "Luke", "John", "Robert"};
BindingList<string> bl1 = new BindingList<string>(names);
ComboBox_Rank_0.DataSource = bl1;
BindingList<string> bl2 = new BindingList<string>(names);
ComboBox_Rank_1.DataSource = bl2;
The BindingList<T> requires using System.ComponentModel; and notice that you don't require the new string[] syntax in the constructor of your list
This may be a very simple question but I realized I could not get it work.
I have a Winform combo box with datasource as a List<int>
combo.DataSource = intList;
What do I put for .DisplayMember and .ValueMember in order to simply have a list of integer values? Not setting them will display nothing.
I have worked with other List<myObj> in which DisplayMember and ValueMember are myObj's properties. How about simple data type like int, string?
When retrieving the selected item, one could simply cast (int)(combo.SelectedItem) or have to go through the property corresponding to ValueMember?
The problem does not occur because you have a list of integers, it probably occurs because you add items to the list after assigning it to the .DataSource property. List does not have a mechanism to notify its container when items are added to or removed from it.
Either add items to the list before assigning it to the .DataSource property, or use a wrapper like BindingSource as Krishnraj Rana suggested.
Here BindingSource comes into picture. You can use it like this.
BindingSource bSource = new BindingSource();
bSource.DataSource = new List<int> { 1, 2, 3 };
combo.DataSource = bSource;
Though you can set datasource of combobox directly with list. like this -
combo.DataSource = intList;
This also works perfectly fine.
You can add items from list using foreach like this.
foreach (var v in intList)
{
comboBox1.Items.Add(v.ToString());
}
How can I select multiple items from the listbox in a Windows Phone 7 application?
e.g
listboxName.SelectedIndex = 0;
listboxName.SelectedIndex = 1;
listboxName.SelectedIndex = 2;
The above code selects 2 while I need to select all three of those.
The values I need to preselect are given to me in a array like
{true,true,true,false,false}
So I tried the using IsSelected like shown below... doesn't work.
int i = 0;
foreach (ListBoxItem currentItem in listboxName.SelectedItems)
{
if (tagindexeselected[i])
{
currentItem.IsSelected = true;
}
i++;
}
What would be the proper way to select multiple items in a listbox?
Hard to say there's a single, best way - it depends on how you're populating your list box, etc. First, be sure your list box's Selection Mode is set to Multiple or Extended.
One option is to use the ListBox's SelectedItems collection:
listBox1.SelectedItems.Add(listBox1.Items[0]);
listBox1.SelectedItems.Add(listBox1.Items[1]);
listBox1.SelectedItems.Add(listBox1.Items[2]);
Note also, in your example above, you're iterating over the SelectedItems collection - not the Items collection. If nothing is selected, that's an empty collection. Also, if your list box ItemsSource is not a series of ListBox Items (you can set your itemsSource to almost any enumeration), you will get an InvalidCastException when you go to run your foreach statement.
foreach (DataRowView item in lstServer.SelectedItems)
{
string WebServerIP = item[lstServer.DisplayMember].ToString();
string WebServerUrl = item[lstServer.ValueMember].ToString();
_WebObjIgent.Url = WebServerUrl;
}
Note : lstServer is Listbox of window application . By using Displaymember and valuemember proprty you can access value and text of listbox.
How do I filter items in a listbox using a combobox using c# and windows forms?
the listbox contains files and the combobox needs to filter them by their extension
Please help I'm new to programming
This is almost an exact duplicate from your last question. The same answer applies.
On the selected index changed event of the combo box, I'd add the items to your listbox based off of the filter selected from your combobox. You can use System.IO.DirectoryInfo to filter your directory given a file extension.
//Clear your listBox before filtering if it contains items
if(yourListBox.Items.Count > 0)
yourListBox.Items.Clear();
DirectoryInfo dInfo = new DirectoryInfo(<string yourDirectory>);
FileInfo[] fileInfo = dInfo.GetFiles("*" + <string yourChosenFileExtension>);
foreach (FileInfo file in fileInfo)
{
yourListBox.Items.Add(file.Name);
}
Filtering a texbox with a combobox
Well you could load the items in a datatable and assing the datatable to the listbox.datasource property. Then you can set the Filter attribute on the DataTable to filter the items.
Another way is to hold the items in a separate list, an assing a linq query implementing the filter to the ListBox.DataSource property once the SelectedItem of the ComboBox changes.
you need to work over the DataSource for ListBox, say it is a List of file names
complete with extentions:
List<string> files = new List<string>(); // sample DataSource
get the selected extension from the ComboBoxto and use it to order ListBox DataSource (file).
string fileExtemsion;
var orderedFiles = files.OrderBy(o => o.EndsWith(fileExtemsion)); // order
listBox.DataSource = orderedFiles; // setting Datasource
listBox.DataBind();