Go through a checked listbox and check all items C# - c#

I need to loop through a checked listbox, and for each of the items in it, I need to check them (basically like a "select all" function).
Is there a basic example you could give me to help me out please?

Use SetSelected and interate through all the Items
// Loop through and set all to selected.
for (int x = 0; x < listBox1.Items.Count; x++)
{
listBox1.SetSelected(x, true);
}
To check the items, use SetItemChecked
// Loop through and set all to checked.
for (int x = 0; x < listBox1.Items.Count; x++)
{
listBox1.SetItemChecked(x, true);
}

You can look through all the items as ListItems:
foreach (ListItem li in CheckBoxList1.Items)
{
li.Selected = true;
}

Related

C# Windows Forms how to get the ListView Items

I am trying to create a string with items from a ListView in C#(Windows Forms). I have two columns and over hundreds of three digit numbers in my ListView. The values indicate which X and Y axis my mouse was on. But as soon as I try to output the values in e.g. a text box, only the last X and Y values appear, the rest are ignored.
What I have tried:
listView1.Items[a].SubItems[0].Text
int.Parse(listView1.Items[a].SubItems[0].Text)
maybe someone has a suggestion
You need to iterate over the items and subitems in this manner:
foreach (ListViewItem item in listView1.Items)
{
Debug.WriteLine($"Item: {item.Text}");
foreach (ListViewItem.ListViewSubItem subitem in item.SubItems)
{
Debug.WriteLine($"\tSubitem:{subitem.Text}");
}
}
Do you use a loop for your items? It's not clear where a comes from. You might also want to use a loop for your subitems.
you can loop through all items and subitems like this:
for (int i = 0; i < listView1.Items.Count; i++)
{
for (int k = 0; k < listView1.Items[i].SubItems.Count; k++)
{
string s = listView1.Items[i].SubItems[k].Text;
}
}
thanks for your help guys,
the only thing missing was the loop and this line Value += Environment.NewLine +
your suggestions both worked
foreach (ListViewItem item in listView1.Items)
{
Value += Environment.NewLine + item.Text;
foreach (ListViewItem.ListViewSubItem subitem in item.SubItems)
{
Value += Environment.NewLine + subitem.Text;
}
}
and
for (int i = 0; i < listView1.Items.Count; i++)
{
for (int k = 0; k < listView1.Items[i].SubItems.Count; k++)
{
Value += Environment.NewLine + listView1.Items[i].SubItems[k].Text;
}
}

C# List start with value 1

When i use for (int i = 1; ..) skip the loop the first item.
How can i start with index 1 and dont skip any item?
private void buttonReadAndSort_Click(object sender, EventArgs e)
{
ReadFromFile rd = new ReadFromFile();
var fileList = rd.readFromFile();
for (int i = 0; i < fileList.Count; i++)
{
var item = (fileList[i]);
Console.WriteLine(item);
list.Add(item);
listBox1.Items.Add(item);
}
buttonReadAndSort.Enabled = false;
}
I guess you want to start with index 1 but access the item at index 0:
for (int i = 1; i <= fileList.Count; i++)
{
var item = fileList[i-1];
Console.WriteLine(item);
list.Add(item);
listBox1.Items.Add(item);
}
But you could also loop normally and add +1 where you need it 1 based:
for (int i = 0; i < fileList.Count; i++)
{
var item = fileList[i];
Console.WriteLine("item:{0} #{1}", item, i + 1);
list.Add(item);
listBox1.Items.Add(item);
}
List, like array, start at zero. Not sure why you would want it to start it at 1 and not skip any items. I do something like csvLinesPartNumber.Add("stuff") to add to the list and then I can get "stuff" from the particular index like so: csvLinesPartNumber[4]. For example, I am doing a while sqlreader dot read which will loop through the table with the particular ID. Then you can do another loop to get the data. Just make sure you put the index in the square brackets.

CheckedListBox Select All Items - Windows Forms C#

I have a checkbox that when a checked checks all items in a CheckedListBox.
When the checkbox goes unchecked it should uncheck all items in the list.
Code:
if (checkBoxCheckAllPrivileges.Checked)
for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
checkedListBoxUsersWhoSee.SetItemChecked(i, true);
else
for (int i = 0; i < listBoxUsers.Items.Count; i++)
checkedListBoxUsersWhoSee.SetItemChecked(i, false);
Is the problem in this code?
Does the .SetitemChecked work giving it the parameter as false?
Is there any other way to uncheck the items?
You have given wrong item in else part for loop,
if (checkBoxCheckAllPrivileges.Checked)
for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
checkedListBoxUsersWhoSee.SetItemChecked(i, true);
else
for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
checkedListBoxUsersWhoSee.SetItemChecked(i, false);
Use JavaScript Too
$(document).ready(function () {
$("#<%=checkBoxCheckAllPrivileges.ClientID%>").click(function () {
if ($(this).is(":checked"))
{
$("#<%=checkedListBoxUsersWhoSee.ClientID%> input[type=checkbox]").prop("checked", true);
}
else
{
$("#<%=checkedListBoxUsersWhoSee.ClientID%> input[type=checkbox]").prop("checked", false);
}
});

Validating preexistence of items when transfering from a ListBox to ComboBox

In C# I have a ListBox and a ComboBox. I'm trying to transfer all my items from my ListBox to my ComboBox while checking if the items are already present on the ComboBox. Repeated items shouldn't get transfer, while the rest should be removed from the ListBox. I'm also meant to do the same transferring of items from the ComboBox into my ListBox.
I have everything almost done, however removing the transferred items is not working as it should as I'm present with some indexing issues.
My code when transferring from List to Cbx
String[] items = intoArrayLst(lstItem2);
ArrayList moved = new ArrayList();
for (int i = 0; i < items.Length; i++)
{
if (!cbxItem.Items.Contains(items.ElementAt(i)))
{
cbxItem.Items.Add(items.ElementAt(i));
moved.Add(items.ElementAt(i));
}
}
for (int i = 0; i < lstItem2.Items.Count; i++)
{
if(moved.Contains(lstItem2.Items.GetItemAt(i)))
{
lstItem2.Items.RemoveAt(i);
}
}
From ComboBox to ListBox
String[] items = intoArrayCBX(cbxItem);
ArrayList moved = new ArrayList();
for (int i = 0; i < items.Count(); i++)
{
if(!lstItem2.Items.Contains(items.ElementAt(i)))
{
lstItem2.Items.Add(items.ElementAt(i));
moved.Add(items.ElementAt(i));
}
}
for (int i = 0; i < cbxItem.Items.Count; i++)
{
if(cbxItem.Items.Contains(moved[i]))
{
cbxItem.Items.RemoveAt(i);
}
}
Would really appreciate some suggestions on how to fix it.
A good way to avoid indexing issues when removing items from a list is to loop backwards through the list. That way the items that get index-shifted on a removal have already been processed.
for (int i = lstItem2.Items.Count - 1; i >= 0; i--)
{
if (!cbxItem.Items.Contains(lstItem2.Items[i]))
{
cbxItems.Items.Add(lstItems2.Items[i]);
lstItems2.Items.RemoveAt(i);
}
}

How to get the ValueMember of an item in a Listbox for a given index number?

I have a ListBox which holds aDisplayMember called "Data" and a ValueMember called "Number". I want to get the ValueMember of all the items using a loop as following.
for (int i = 0; i < ListBox1.Items.Count; i++)
{
//Get the `ValueMember` of `Item` where it's `Index` is `i`
}
Did you tried like code below:
for (int i = 0; i < ListBox1.Items.Count; i++)
{
Console.WriteLine((ListBox1.Items[i] as YourItemClassType).Number.ToString());
}
YourItemClassType is your class that you adding to ListBox1, YourItemClassType contains Number and Data properies
Hope helps

Categories