Sync. SelectedIndex of two multiselect Listboxes - c#

I'm strungling to sync. the selectedIndexs of two multi-select Listboxes.
With single-select enabled the code is just:
private void libHT_SelectedIndexChanged(object sender, EventArgs e)
{
libMonth.SelectedIndex = libHT.SelectedIndex;
}
But this doesn't work if multi-select is enabled.
Can you help me? Do I have to use a for or foreach?
Thanks for your help.
Thomas

There is the SelectedIndices property.
private void libHT_SelectedIndexChanged(object sender, EventArgs e)
{
libMonth.SelectedIndices.Clear();
foreach (var index in libHT.SelectedIndices.Cast<int>())
{
libMonth.SelectedIndices.Add(index);
}
}
Try that

Yes, you will have to loop over all the selections. Code like below can help you with that
private void libHT_SelectedIndexChanged(object sender, EventArgs e) {
libMonth.SelectedIndices.Clear();
foreach (int indx in libHT.SelectedIndices)
libMonth.SelectedIndices.Add(indx);
}
Don't forget:
To hook the index changed event: libHT.SelectedIndexChanged += libHT_SelectedIndexChanged;
To set the selection mode correctly libHT.SelectionMode = libMonth.SelectionMode = SelectionMode.MultiExtended;
To watch out for your programmatic selection, causing infinite recursion

Related

Items in Combobox gets added

I've two radiobuttons and a single checkbox.
Items in checkbox will pop up according to the radiobutton selected.
The problem is when I click the second radiobutton after clicking the first radiobutton, Combobox pops us all the values from both radiobutton 1 and 2.
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
foreach (string apacservers in Properties.Settings.Default.B)
{
comboBox1.Items.Add(ItemsB);
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
foreach (string europeservers in Properties.Settings.Default.A)
{
comboBox1.Items.Add(ItemsA);
}
}
Any help would be much appreciated
you need to add comboBox1.Items.Clear(); before both of your foreachs.

How to select all checkbox using Itemcheck event?

Can you guys help me code if I select one of the checkboxes on the ListView section, the rest of the checkboxes should be checked.
My ListView name is lvBase and I want to used the ListView ItemCheck events.
This is my code.
private void lvBase_ItemCheck_1(object sender, ItemCheckEventArgs e)
{
}
Use the following line to add an itemcheck or itemchecked event:
this.listView1.ItemCheck += new ItemCheckEventHandler(listView1_ItemCheck);
Hope I understood your question correctly, If you want to check all the checkbox in the list, you can loop through them and set Checked Property to true.
private void lvBase_ItemChecked(object sender, ItemCheckedEventArgs e)
{
for (int i = 0; i < lvBase.Items.Count; i++)
{
lvBase.Items[i].Checked = e.Item.Checked;
}
}

Selected index changed fail into ListBox

I created a windows forms like this
As you can see in selected changed event I disable button move to, it works correctly, problem starts when I try to
return an item it to main list, move to button keeps disable instead enable it again when I select item of first list. Someone knows
what occurs there?
Events:
private void lstTechUnnotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnReturnTo.Enabled = false;
btnMoveTo.Enabled = true;
}
private void lstTechToNotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnReturnTo.Enabled = true;
btnMoveTo.Enabled = false;
}
You need to make sure that there actually is an item being selected since ListBox.SelectedIndexChanged event gets fired even when there're no items selected - making the new SelectedIndex equal to -1. Also, from the way you asking, I expect you want to enable btnMoveTo when there's a selected item in lstTechUnnotified and otherwise, disable it - and the same for btnReturnTo and lstTechToNotified; if that's it, then the easy way is:
private void lstTechUnnotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnMoveTo.Enabled = (lstTechUnnotified.SelectedIndex > -1);
}
private void lstTechToNotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnReturnTo.Enabled = (lstTechToNotified.SelectedIndex > -1);
}
Though I'm not sure about your button names..

C# How do i make a timer Check if a ComboBox has more items?

Ok so this is what i got right now:
private void timer3_Tick(object sender, EventArgs e)
{
SendKeys.Send(comboBox3.Text);
ComboBox3.SelectedIndex += 1;
SendKeys.Send(comboBox4.Text);
ComboBox4.SelectedIndex += 1;
}
but i want it so if there is more than one item in ComboBox3 that it changes and types that 1st before it does ComboBox4 and if there isnt more than one i want it to jus continue to ComboBox4
also this is my 1st post on this website so srry if you dnt understand
I think you're looking for an if statement?
private void timer3_Tick(object sender, EventArgs e)
{
if (ComboBox3.Items.Count > 1)
{
SendKeys.Send(comboBox3.Text);
}
//rest of code
}

select row from winforms listview

I have listview which is populated with list of data. Now I want to select desired row and on click button to recognize that item to delete from a collection.
Question is how to recognize selected row from the listview?
private void buttonDelete_Click(object sender, EventArgs e)
{
//selected data is of custom type MyData
var selected = (MyData)....?
}
Thanks
This should works
private void buttonDelete_Click(object sender, EventArgs e)
{
//selected data is of custom type MyData
var selected = yourListView.SelectedItems.First();
}
To add to #Zaphod's answer and make a little more robust:
private void buttonDelete_Click(object sender, EventArgs e)
{
if (yourListView.SelectedItems.Any())
{
//selected data is of custom type MyData
var selected = yourListView.SelectedItems.First();
}
}
You could use .Count > 0 instead of .Any() and .SelectedItems[0] instead of .First(). Whatever you find more readable/maintainable.
Old School answer :) without any LINQ statements
if(yourListView.SelectedItems.Count > 0)
{
var item = yourListView.SelectedItems[0];
}
I don't think you have to use casting for a deleting operation, just remove all the selected indices like this:
private void buttonDelete_Click(object sender, EventArgs e){
for (int i = listView1.SelectedIndices.Count - 1; i >= 0; i--)
listView1.Items.RemoveAt(listView1.SelectedIndices[i]);
}
or more simply:
private void buttonDelete_Click(object sender, EventArgs e){
foreach(ListViewItem item in listView1.SelectedItems)
listView1.Items.Remove(item);
}
As you can see the item which is selected is of type ListViewItem, you can bind your data to this item via Text property (if the data is string) or Tag property. I don't understand what your CustomData is, is it a type inheriting ListViewItem?
YOu should do this
private void buttonDelete_Click(object sender, EventArgs e)
{
if (yourListView.SelectedItems.Any())
{
//selected data is of custom type MyData
var selected = (MyData)yourListView.SelectedItems[0];
YourCollection.Remove(selected);
}
}

Categories