How to sync multiple selection indices between two ListBoxes? - c#

I'm trying to move the selection from listbox1 to listbox2 with this code
if (listBox1.SelectedItems.Count > 0)
{
int selectedindex = listBox1.SelectedIndex;
for (int i =0 ; i < listBox1 .SelectedItems.Count; i++)
{
listBox2.SetSelected(selectedindex , true);
}
}
I got a result , but when I select more than one item in listbox1 it will only select one item in listbox2 which is the first one I've selected in listbox1.

You are only storing one selection index of listbox1 in selectedIndex. Later in the loop you are always instructing listbox2 to set its selections to this one index.
Try this instead:
foreach (int index in listBox1.SelectedIndices)
{
listBox2.SetSelected(index, true);
}

You have to set the SelectionMode property to allow multiple selections. You can set in property window or on load event.
listBox2.SelectionMode.MultiExtended = SelectionMode.MultiExtended;

Related

How to copy the other Unselected items from a listBox to another

How to copy selected items from ListBox1 to listBox2, and unselected Items to listBox3.
I already know how to copy selected items as below:
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{ listBox2.Items.Add(listBox1.SelectedItems[i]);}
But I don't know how to copy the other unselected items from the main ListBox1.
Thanks
You can use the GetSelected(x) method of Listbox to determine if it is selected to not, and then put it into the appropriate ListBox.
for (int i = 0; i < listBox1.Items.Count; i++)
{
if (listBox1.GetSelected(i))
listBox2.Items.Add(listBox1.Items[i]);
else
listBox3.Items.Add(listBox1.Items[i]);
}

Combobox dropdownlist is not filling from datagridview row

I want when I click on datagridview row then my two text boxes and combobox fill with the corresponding values but two text boxes fill accurately but category dropdown is not filling.
My C# code is
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1 && e.ColumnIndex !=-1)
{
//edit = 1;
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
proID = Convert.ToInt32(row.Cells["proIDGV"].Value.ToString());
proTxt.Text = row.Cells["nameGV"].Value.ToString();
barcodetxt.Text = row.Cells["barcodeGV"].Value.ToString();
catDD.SelectedValue = row.Cells["catIDGV"].Value.ToString();// not working properly due to which edit button is not working
// catDD.SelectedItem = row.Cells["catGV"].Value.ToString();//Also Write this line of code but not produce the desire result
MainClass.Disabled(leftPanel);
}
}
For category dropdown, which is a combo-box, to have a SelectedItem or SelectedValue you should already have all the possible categories in the "items" property, otherwise, the programming cannot select the category out of nowhere. To select items which don't exist you need to add an item first, or if you don't really need the items afterward you can just use:
catDD.Text = row.Cells["catIDGV"].Value.ToString();
Although I would not recommend doing it this way.
The other way is that you can add items by doing this (this is what I recommend):
Edit: You first add the item then select it.
string item = row.Cells["catIDGV"].Value.ToString(); // Your selected item in DataGridView
catDD.Items.Add(item); // Add the item
catDD.SelectedItem = item; // Select the item

There is in ListView the possibility of SetSelected like in ListBox?

my application have ListView and inside my foreach loop i am add files into my ListView and want the option to show the current file as marked, before using ListView i try ListBox and used SetSelected successfully.
i try listView.Items[listView.Items.Count - 1].Selected = true; nut this marked all the files inside my ListView
Make sure you are setting the selection outside the loop. if you do it in the loop, you will see that all items are selected because the listview item count is increasing as you add items to the listview:
for (int i; i<someList.Count; i++)
{
// Fill the listview here
}
listView.Items[listView.Items.Count - 1].Selected = true;
U are probably set the listView.Items[listView.Items.Count - 1].Selected = true; wrongly as stated by John Koerner.
If you have a reason to always select the last item in loop, u can remove all the selection before select again.
for (int i = 0; i < 5; i++)
{
foreach (int index in listView1.SelectedIndices)
{
listView1.Items[index].Selected = false;
}
listView1.Items.Add(i.ToString());
listView1.Items[listView1.Items.Count - 1].Selected = true;
}
or if multiple selection is not useful, u can set listView1.MultiSelect = false;

Compare a ListBox Item with ListView item based on indexes

I am new to c#. In my project I have two controls ListBox and ListView
ListBox --> lbxEmpName
ListView --> lvEmpDetails
I tried the below code:
if (lvEmpDetails.Items.Count > 0)
{
for (int intCount = 0; intCount < lbxEmpName.Items.Count; intCount++)
{
for (int intSubCount = 0; intSubCount < lvEmpDetails.Items.Count; intSubCount++)
{
if (lvEmpDetails.Items[intSubCount].Equals(lbxEmpName.Items[intCount]))
{
lbxEmpName.Items.Remove(lbxEmpName.Items[intCount]);
}
}
}
}
If I run the above code, there are no matches between ListView Items and ListBox Items (Infact there must be some matches). When I debug my code, I saw the below thing: It is saying SelectedItem whereas I am giving here Items (Thats why my program is not matching items)
why it is showing SelectedItem = "" instead of Items ?
Am I doing something wrong in my code? Please suggest.
ListView's Items contains objects of type ListViewItem. So there is no use in comparing those with objects in ListBox's Items.
If you want to compare their text, you must write something like this:
if (lvEmpDetails.Items[intSubCount].Text == (string)lbxEmpName.Items[intCount])
{
// Do something here
}
Please note that a ListViewItem can have multiple sub-items and its Text property returns the first column of its data.
Compare the string values that you want to compare not the object themselves.

How to reload item of combobox in c#?

I have 10 items in combobox and when I delete one of those items, the combobox don't show automatically next item it show white space. I want to display next item in range.
I delete them normaly
cmbsomename.Items.Remove(cmbsomename.SelectedItem);
How to do that?
Because you deleted the selected item, you need to set another item as selected:
comboBox.SelectedIndex = 0;
Edit:
The code above sets the selected item as the first item in the list.
For the next item, you need to find the index of the item you are deleting first:
int index = cmbsomename.SelectedIndex;
cmbsomename.Items.Remove(cmbsomename.SelectedItem);
if (index < cmbsomename.Items.Count) // Make sure there IS a next item
cmbsomename.SelectedIndex = index; // Next item will have same index value
var selectedIndex = cmbsomename.SelectedIndex;
cmbsomename.Items.Remove(cmbsomename.SelectedItem);
comboBox.SelectedIndex = selectedIndex < cmbsomename.Items.Count() ? ++selectedIndex : 0;
after deleting an Item set the selectedindex
int index = comboBox.SelectedIndex;
comboBox.Items.Remove(comboBox.SelectedItem);
if (index < comboBox.Items.Count)
comboBox.SelectedIndex = index;

Categories

Resources