I basically populate a comboBox with a range of numbers using the following code:
comboBox1.Items.AddRange(Enumerable.Range(0, 50).Cast<object>().ToArray())
The thing is that I'd like to have the unit of measurement comparing soon after the numbers. Thus my question, how can I add a string to each item of my comboBox?
You can do it using a Linq expression:
comboBox1.Items.AddRange(Enumerable.Range(0, 50).Select(x => x.ToString() + " sufix").Cast<object>().ToArray())
Additionally to the solution provided by Diego Rafauel Souza you could also append the text to each item of the combobox:
for (var index = 0; index < comboBox1.Items.Count; index++)
{
var item = comboBox1.Items [index];
comboBox1.Items[index] = $"{item} {suffix}";
}
This simply adds the suffix to every item of the comboBox.
Related
I have found many examples on how to find the selected items in a listbox and how to iterate through a listbox;
for(int index=0;index < listBox1.Items.Count; index++)
{
MessageBox.Show(listBox1.Items[index].ToString();
}
or
foreach (DataRowView item in listBox1.Items)
{
MessageBox.Show(item.Row["ID"].ToString() + " | " + item.Row["bus"].ToString());
}
While these methods work great for the selected items, what I have yet to figure out, or find, is how to get the selected state, selected and unselected, of every item in a listbox, as the above only gives the selected.
Basically, I need something like this;
for(int index=0;index < listBox1.Items.Count; index++)
{
if (index.SelectedMode == SelectedMode.Selected)
{
MessageBox.Show(listBox1.Items[index].ToString() +"= Selected";
}
else
{
MessageBox.Show(listBox1.Items[index].ToString() +"= Unselected";
}
}
I've found a snippet that said to use (listBox1.SelectedIndex = -1) to determine the selected state however I've not figured out or found how to build a loop around this to check each item in the listbox.
I've also read that I should put the listbox items into an array, but again nothing about getting the selected state of each item in the listbox.
I know I'll have to iterate through the listbox to accomplish what I'm needing, pretty sure it'll be one of the above loops, however I have yet to find how to extract the selected state of each item in the listbox.
I'm using VS2013, C# Windows Form, .NET Framework 4.0
Thanks in advance for any advice/direction.
This will get you the unselected items:
List<string> unselected = listBox1.Items.Cast<string>().Except(listBox1.SelectedItems.Cast<string>());
You can loop over that list like this:
foreach(string str in listBox1.Items.Cast<string>().Except(listBox1.SelectedItems.Cast<string>()))
{
System.Diagnostics.Debug.WriteLine($"{str} = Not selected");
}
I've made the assumption that you're using string as your item type. If you want to use something else then just replace string with your type and it should still work.
You then loop over the unselected items to do whatever you want with them then loop over listBox1.SelectedItems to do whatever you want with the selected ones.
You can use GetSelected method of the ListBox. It returns a value indicating whether the specified item is selected.
For example, the following code, sets the value of selected to true if the item at index 0 (the first item) is selected:
var selected = listBox1.GetSelected(0);
Example
Te following loop, shows a message box for each item, showing the item text and item selection status:
for (int i = 0; i < listBox1.Items.Count; i++)
{
var text = listBox1.GetItemText(listBox1.Items[i]);
var selected = listBox1.GetSelected(i);
MessageBox.Show(string.Format("{0}:{1}", text, selected ? "Selected" : "Not Selected"));
}
I am trying to iterate through two listBoxes for a program I am coding. Both listBoxes will have a different item count inside of it.
Basically, I want my program to get the selectedItem from one listBox and use the string or text from that item to replace the text from EACH and EVERY single item in the other listBox.
Once it's done using the selectedItem from the original listBox for all the items in the other listBox, I want it to go to the next item in the original listBox and do the same process all over again.
It should repeat this UNTIL it has gone through ALL of the items in the original listBox.
Hopefully that made sense....
Here is some example code I made. I created two for loops so that it could iterate through both listBoxes.
for (int i = 0; i < listBoxOriginal.Items.Count; i++)
{
string linkurl = listBoxOriginal.Items[i].ToString() + "..";
listBoxNewListBox.SelectedIndex = 0;
for (int o = 0; o < listBoxNewListBox.Items.Count; o++)
{
string s = listBoxNewListBox.Items[o] as string;
string newurl = s.Replace("DOMAIN", linkurl);
listBoxNewListBox.SelectedIndex++;
}
}
My issue is, when the inner for loop finishes iterating completely it errors out. I know the error is because it reached the end of the listBox and can't go any further, but I don't know how else to iterate through the listBox without having the items selected.
What it should do is, once it reaches the end of "listBoxNewListBox" it should go to the next item in "listBoxOriginal", and perform the same process all over again until it's done going through every item in "listBoxOriginal".
Any help would be appreciated!
I think problem is because of SelectedIndex at list. I think it's just going to far.
Here is a little modification:
for (int i = 0; i < listBoxOriginal.Items.Count; i++)
{
string linkurl = listBoxOriginal.Items[i].ToString() + "..";
for (int o = 0; o < listBoxNewListBox.Items.Count; o++)
{
string s = listBoxNewListBox.Items[o] as string;
string newurl = s.Replace("DOMAIN", linkurl);
listBoxNewListBox.SelectedIndex = o;
}
}
Here is explanation:
When in inner loop you are doing this operation: listBoxNewListBox.SelectedIndex++ you are setting this index as 1 more than index of loop. That means, if we look at very last iteration of inner loop, this index is set with value which is already to high. This is probably reason why application throws an exception.
I have a list box and I need to modify the list based on the content. I am trying to do this but it does not do any thing.
string itemRemove = "Apple";
lstFruits.Items.Remove(itemRemove);
The problem is that in a ListBox control, you cant remove an item like you remove them from List<T>(i.e using an enumerator). You have to you have to loop using an index, starting at the last item, like this :
for (int n = lstFruits.Items.Count - 1; n >= 0; --n)
{
string itemRemove = "Apple";
if (lstFruits.Items[n].ToString().Contains(itemRemove))
{
lstFruits.Items.RemoveAt(n);
}
}
I currently have a CheckedListBox with several boxes. I want to be able to test every Checkbox in the list to see if it's checked, and if it is, add it's text value (CheckBox.Text) to a List of strings.
Here is what I have:
for ( int i = 0; i < multiTaskChecks.Items.Count; i++ )
{
if ( multiTaskChecks.GetItemChecked(i) )
{
checkedMultiTasks.Add(multiTaskChecks.GetItemText(i));
}
}
Using this, GetItemText is returning 0, 1, 2, 3, etc instead of the text values that I'm after. I have also tried CheckedListBox.Text.IndexOf(i), CheckedListBox.Text.ToList(), each without any luck.
I just cannot get the label text of one of these CheckBoxes from the CheckedListBox. Any help with this would be really appreciated.
Firstly, you should be able to loop through the checked items only like so
foreach (var item in multiTaskChecks.CheckedItems)
{
}
then depending on the type of the item, get whatever property you want from it. Sounds like it is just a Text or you just want the string, so
foreach (var item in multiTaskChecks.CheckedItems)
{
checkedMultiTasks.Add(item.ToString());
}
or I prefer
checkedMultiTasks.AddRange(multiTaskChecks.CheckedItems.
OfType<object>().Select(i => i.ToString()));
Try this:
for (int i = 0; i < multiTaskChecks.Items.Count; i++)
{
if (multiTaskChecks.GetItemChecked(i))
{
checkedMultiTasks.Add(multiTaskChecks.GetItemText(multiTaskChecks.Items[i]));
}
}
ListControl.GetItemText Method
NOTE There's a caution regarding DisplayMember for this method:
If the DisplayMember property is not specified, the value returned by GetItemText is the value of the item's ToString method. Otherwise, the method returns the string value of the member specified in the DisplayMember property for the object specified in the item parameter.
This should work:
var checkedMultiTasks = new List<string>();
foreach(var item in multiTaskChecks.CheckedItems) {
checkedMultiTasks.Add(item.ToString());
}
i develop a webpage in that i need to add value field and text field to listbox item
i tried the following code , but it's not working , Please help me to solve the issue .
Thanks in advance
string[] category = new string[100];
char[] delimeter = { '~' };
category = Convert.ToString(dtable.Rows[0].ItemArray[5]).Split(delimeter);
for (int h = 0; h < category.Length; h++)
{
ListItem lstitem = lstCategory.Items.FindByText(category[h]);
if (lstitem != null)
{
lstSelCategory.DataTextField = category[h];
lstSelCategory.DataValueField = lstitem.Value;
}
}
lstSelCategory.DataBind();
}
DataValueField and DataTextField are for assigning which properties are displayed and used as values for the assigned DataSource, NOT for adding items to the List.
EDIT:
It looks to me as though you're looping through some categories from a DataTable, finding a matching item in another ListBox and then trying to add that item to another ListBox... You should be using the Add method of the ListBox (if that's what the Control is).
lstSelCategory.Items.Add(new ListItem(category[h], lisitem.Value));
ListBox.DataTextField and ListBox.DataValueField are used in conjuction with ListBox.DataSource.
See this for further information.