I am trying to implement a multiple items select in multiple listboxes. Is this possible?
Requirements:
When user selects an item in ListBox1 - items in ListBox2 and ListBox3 should be selected.
When user deselects and item in ListBox1 - Items in ListBox2 and ListBox2 should be deselected.
I am able to achieve this on LB1.
Same behavior 1 and 2 should repeat for ListBox2 and ListBox3.
I am having difficulty here.
I have the code below - but of course it runs into stackoverflow exception. What am I missing?
private void listBox_1_SelectedIndexChanged_(object sender, EventArgs e)
{
int userSelectedIndex = listBox_1.Items.Count;
if (listBox_1.SelectedIndices.Count > 0)
{
for (int count = 0; count < listBox_1.Items.Count; count++)
{
// Determine if the item is selected.
if (listBox_1.GetSelected(count) == true)
{
if (count <= listBox_2.Items.Count)
listBox_2.SetSelected(count, true);
if (count <= listBox_3.Items.Count)
listBox_3.SetSelected(count, true);
}
else if (listBox_1.GetSelected(count) == false)
{
// Select all items that are not selected.
if (count <= listBox_2.Items.Count)
listBox_2.SetSelected(count, false);
if (count <= listBox_3.Items.Count)
listBox_3.SetSelected(count, false);
}
}
}
}
private void listBox_2_SelectedIndexChanged(object sender, EventArgs e)
{
int userSelectedIndex = listBox_2.Items.Count;
if (listBox_2.SelectedIndices.Count > 0)
{
for (int count = 0; count < listBox_2.Items.Count; count++)
{
// Determine if the item is selected.
if (listBox_2.GetSelected(count) == true)
{
if (count <= listBox_1.Items.Count)
listBox_1.SetSelected(count, true);
if (count <= listBox_3.Items.Count)
listBox_3.SetSelected(count, true);
}
else if (listBox_2.GetSelected(count) == false)
{
// Select all items that are not selected.
if (count <= listBox_1.Items.Count)
listBox_1.SetSelected(count, false);
if (count <= listBox_3.Items.Count)
listBox_3.SetSelected(count, false);
}
}
}
}
This field is used to avoid Stack Overflow Exceptions.
private bool _isUpdating;
I am also using the SelectedValueChanged event instead of SelectedIndexChanged but it should still work, here is the 3 different checkboxes (this could be improved by refactoring how it handles which listbox to update)
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (this._isUpdating)
{
//// Prevent Stack Overflow by exiting out
return;
}
var selectedIndexes = this.listBox1.SelectedIndices;
this._isUpdating = true;
this.listBox2.SelectedItems.Clear();
this.listBox3.SelectedItems.Clear();
foreach (int index in selectedIndexes)
{
//// Listbox2
var foundItem = this.listBox2.Items[index];
this.listBox2.SelectedItems.Add(foundItem);
//// Listbox3
var foundItem2 = this.listBox3.Items[index];
this.listBox3.SelectedItems.Add(foundItem2);
}
this._isUpdating = false;
}
private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
if (this._isUpdating)
{
//// Prevent Stack Overflow by exiting out
return;
}
var selectedIndexes = this.listBox2.SelectedIndices;
this._isUpdating = true;
this.listBox1.SelectedItems.Clear();
this.listBox3.SelectedItems.Clear();
foreach (int index in selectedIndexes)
{
//// Listbox1
var foundItem = this.listBox1.Items[index];
this.listBox1.SelectedItems.Add(foundItem);
//// Listbox3
var foundItem2 = this.listBox3.Items[index];
this.listBox3.SelectedItems.Add(foundItem2);
}
this._isUpdating = false;
}
private void listBox3_SelectedValueChanged(object sender, EventArgs e)
{
if (this._isUpdating)
{
//// Prevent Stack Overflow by exiting out
return;
}
var selectedIndexes = this.listBox3.SelectedIndices;
this._isUpdating = true;
this.listBox1.SelectedItems.Clear();
this.listBox2.SelectedItems.Clear();
foreach (int index in selectedIndexes)
{
//// Listbox1
var foundItem = this.listBox1.Items[index];
this.listBox1.SelectedItems.Add(foundItem);
//// Listbox2
var foundItem2 = this.listBox2.Items[index];
this.listBox2.SelectedItems.Add(foundItem2);
}
this._isUpdating = false;
}
Which as stated previously, avoids the Stack Overflow Exception and ensures that selecting / deselecting an item in the Listbox at a given index will also select the item at the other listboxes index.
Related
I have a checkeboxlist with 100 items. Obviously user can check items one by one as many as he need, but I would like to give to user option check range of items (let's say with Shift hold button). So, user check one of the items (let's say item index 5) and then press and hold shift button and check next item (index 10), so I range of the items should be checked from 5...10
I have not found anything about such implementation, looks like it doesn't exist and no one did such kind of things.
How to do it?
Keep track of your last index:
int lastIndex = -1;
In your form's constructor, wire things up:
public Form1() {
InitializeComponent();
checkedListBox1.CheckOnClick = true;
checkedListBox1.SelectedIndexChanged += CheckedListBox1_SelectedIndexChanged;
checkedListBox1.MouseDown += CheckedListBox1_MouseDown;
}
And then use these methods to change the items in the range:
private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e) {
lastIndex = checkedListBox1.SelectedIndex;
}
private void CheckedListBox1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left && Control.ModifierKeys == Keys.Shift) {
var useIndex = Math.Max(lastIndex, 0);
var x = checkedListBox1.IndexFromPoint(e.Location);
if (x > -1 && x != useIndex) {
if (useIndex > x) {
for (int i = useIndex - 1; i > x; i--) {
checkedListBox1.SetItemChecked(i, !checkedListBox1.GetItemChecked(i));
}
} else {
for (int i = useIndex + 1; i < x; i++) {
checkedListBox1.SetItemChecked(i, !checkedListBox1.GetItemChecked(i));
}
}
}
}
}
i want to get next data in gridview, the thing is when i use break; it totally out of loop, but when i dont use break; it otomatically call the last row data of the text that i type.
private void button7_Click(object sender, EventArgs e)
{
for (int i = 0; i < gridView1.RowCount; i++)
{
var row = gridView1.GetDataRow(i);
var genre = row["genre"].ToString();
if (genre.IndexOf(textBox8.Text, StringComparison.OrdinalIgnoreCase) >= 0)
{
gridView1.FocusedRowHandle = i;
break;
}
}
}
if i dont use break it will select the last data with 'ba' = basketball.
what i want is when i click next it select ballet and stop, and when i click next button again it select basketball
use an int variable to store the current iteration of loop:
int iteration=0;
private void button7_Click(object sender, EventArgs e)
{
for (int i = iteration; i < gridView1.RowCount; i++)
{
var row = gridView1.GetDataRow(i);
var genre = row["genre"].ToString();
if (genre.IndexOf(textBox8.Text, StringComparison.OrdinalIgnoreCase) >= 0)
{
gridView1.FocusedRowHandle = i;
iteration=i+1;
break;
}
}
}
And on Find button you can reset iteration to 0
I have two classes that get all the minimized windows:
The first class is WindowSnap.cs:
WindowSnap.cs
The second class is WindowSnapCollection.cs:
WindowSnapCollection.cs
In the first class in the windowSnap there is a method called GetAllWindows:
public static WindowSnapCollection GetAllWindows(bool minimized, bool specialCapturring)
{
windowSnaps = new WindowSnapCollection();
countMinimizedWindows = minimized;//set minimized flag capture
useSpecialCapturing = specialCapturring;//set specialcapturing flag
EnumWindowsCallbackHandler callback = new EnumWindowsCallbackHandler(EnumWindowsCallback);
EnumWindows(callback, IntPtr.Zero);
return new WindowSnapCollection(windowSnaps.ToArray(), true);
}
In the end i used a breakpoint on the return line and windowSnaps variable contain 15 items.
For example the first item is:
[0] = {Window Text: , Handle: 31918532}
The second one is:
[3] = {Window Text: ?How can i get from a window only the text without the Handle number ? - Stack Overflow - Google Chrome?, Handle: 64424060}
In the first item i was able to do on my own to remove this item since it's text is only "," and this window show nothing.
But number [3] and other items i want first to remove from them the part Handle: 64424060
I don't need that the user will see this information. But only the text so for example number [3] should look like:
?How can i get from a window only the text without the Handle number ? - Stack Overflow - Google Chrome?
And to fix those "?" that are in the beginning of the line.
But the idea is to display only the name of each window and not the whole text with the handle number.
This is how i add the items(windows) to form1 listBox:
this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
In the listBox i want to see the items names or description/titles not the whole text with the handle number.
You can do a little trick. It is not nice but works. Add a new listbox eg listBox1 with the same size on top of listBoxSnap.
add in public Form1():
public Form1()
{
...
for (int i = listBoxSnap.Items.Count - 1; i >= 0; i--)
{
string tt = listBoxSnap.Items[i].ToString();
if (tt.Contains(" ,"))
{
listBoxSnap.Items.RemoveAt(i);
}
}
buttonSnap.Enabled = true;
//add here ->
string[] myList = new string[listBoxSnap.Items.Count];
for (int i = 0; i < listBoxSnap.Items.Count; i++)
{
string tt = listBoxSnap.Items[i].ToString();
int index = tt.LastIndexOf(",");
myList [i] = tt.Substring(0, index);
}
listBox1.Items.AddRange(myList);
rectangles = new Rectangle[listBoxSnap.Items.Count];
isCropped = new bool[listBoxSnap.Items.Count];
if (this.listBoxSnap.Items.Count > 0)
{
this.listBoxSnap.SetSelected(0, true);
this.listBox1.SetSelected(0, true);
}
listBoxSnap.Select();
listBox1.Select();
}
change private void listBoxSnap_SelectedIndexChanged(object sender, EventArgs e) to:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBoxSnap.SelectedIndex = listBox1.SelectedIndex;
drawpicbox(this.listBoxSnap.SelectedIndex);
}
and delete this.listBoxSnap.SelectedIndexChanged += new System.EventHandler(this.listBoxSnap_SelectedIndexChanged); from Form1.Designer.cs
Add this event:
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var item = listBox1.IndexFromPoint(e.Location);
if (item >= 0)
{
listBox1.SelectedIndex = item;
listBoxSnap.SelectedIndex = listBox1.SelectedIndex;
cm.Show(listBox1, e.Location);
}
}
}
And lastly add in private void RefreshWindowsList()
private void RefreshWindowsList()
{
...
for (int i = listBoxSnap.Items.Count - 1; i >= 0; i--)
{
string tt = listBoxSnap.Items[i].ToString();
if (tt.Contains(" ,"))
{
listBoxSnap.Items.RemoveAt(i);
}
}
//add here ->
string[] myList = new string[listBoxSnap.Items.Count];
for (int i = 0; i < listBoxSnap.Items.Count; i++)
{
string tt = listBoxSnap.Items[i].ToString();
int index = tt.LastIndexOf(",");
myList [i] = tt.Substring(0, index);
}
listBox1.Items.Clear();
listBox1.Items.AddRange(myList);
rectangles = new Rectangle[listBoxSnap.Items.Count];
isCropped = new bool[listBoxSnap.Items.Count];
textBoxIndex.Text = listBoxSnap.Items.Count.ToString();
if (this.listBoxSnap.Items.Count > 0)
{
this.listBoxSnap.SetSelected(0, true);
this.listBox1.SetSelected(0, true);
}
listBoxSnap.Select();
listBox1.Select();
}
Valter
I have this two list-box In that first list-box is fill on Combo-box Selected index changed, so List-box 1 is Bounded. Now when I press the > button all selected item in List-box 1 is display in List-box 2.
But instead of Names, I get System.Data.DataRowView
so my question is I want Names instead of this System.Data.DataRowView
my code is this
private void btnSelect1ItemFrom_Click(object sender, EventArgs e)
{
if (listBoxSelectToLedger.Items.Count > 0)
{
for (int i = 0; listBoxSelectToLedger.Items.Count > i; )
{
listBoxSelectToLedger.Items.Remove(listBoxSelectToLedger.Items[i].ToString());
}
}
if (listBoxSelectFromLedger.SelectedItem != null)
{
** for (int i = 0; i < listBoxSelectFromLedger.SelectedItems.Count; i++)
{
listBoxSelectToLedger.Items.Add(listBoxSelectFromLedger.SelectedItems[i].ToString());
} **
}
else
{
MessageBox.Show("No item Selected");
}
* I think I am some where Wrong in Second IF Condition in my Code *
Plz Help me
Thanks in Advance
Try this...
private void button1_Click(object sender, EventArgs e)
{
if(listBoxFrom.SelectedItems.Count>0)
{
for (int x = listBoxFrom.SelectedIndices.Count - 1; x >= 0; x--)
{
int idx = listBoxFrom.SelectedIndices[x];
listBoxTo.Items.Add(listBoxFrom.Items[idx]);
listBoxFrom.Items.RemoveAt(idx);
}
}
}
Hiii.. Deep, use the below code to add ListItem.
foreach (ListItem LI in listBoxFrom.Items)
{
if (LI.Selected)
listBoxTo.Items.Add(LI);
}
To add in to 2nd listbox and remove that from the first listbox you can use below code:
int[] indices = listBoxFrom.GetSelectedIndices();
for (int i = indices.Length - 1; i >= 0; i--)
{
ListItem LI = listBoxFrom.Items[indices[i]];
listBoxTo.Items.Add(LI);
listBoxFrom.Items.RemoveAt(indices[i]);
}
put your No items selected message where you require.
I got answer of my own question.
i have to set my DataRowView object
if (listBoxSelectToLedger.Items.Count > 0)
{
for (int i = 0; listBoxSelectToLedger.Items.Count > i; i = 0)
{
listBoxSelectToLedger.Items.Remove(listBoxSelectToLedger.Items[i].ToString());
}
}
if (listBoxSelectFromLedger.SelectedItem != null)
{
foreach (DataRowView objDataRowView in listBoxSelectFromLedger.SelectedItems)
{
listBoxSelectToLedger.Items.Add(objDataRowView["item_name"].ToString());
}
}
else
{
MessageBox.Show("No Item selected");
}
I was wondering but I have 4 listboxes and what I was wondering is how would I delete all 4 of the items by just selecting one item?
As you can see here this is the part that deletes the selected item but how would I delete all 4 items in my listbox if I just selected the "seriesName" one.
for (int x = lstb_seriesName.SelectedIndices.Count - 1; x >= 0; x--)
{
int a = lstb_seriesName.SelectedIndices[x];
lstb_seriesName.Items.RemoveAt(a);
}
Here's my code for the delete button.
private void btn_Delete_Click(object sender, EventArgs e)
{
if (lstb_seriesName.SelectedItems.Count <= 0)
{
MessageBox.Show("You need to select an item to delete first!");
}
else
{
for (int x = lstb_seriesName.SelectedIndices.Count - 1; x >= 0; x--)
{
int a = lstb_seriesName.SelectedIndices[x];
lstb_seriesName.Items.RemoveAt(a);
}
System.IO.StreamWriter DeleteFileData = new System.IO.StreamWriter(sPath);
foreach (var item in lstb_seriesName.Items)
{
DeleteFileData.WriteLine(item);
}
foreach (var item in lstb_seriesDay.Items)
{
DeleteFileData.WriteLine(item);
}
foreach (var item in lstb_seriesTime.Items)
{
DeleteFileData.WriteLine(item);
}
foreach (var item in lstb_seriesActive.Items)
{
DeleteFileData.WriteLine(item);
}
DeleteFileData.Close();
MessageBox.Show("Program deleted!");
}
}
The only way to do this is if you can guarantee that they are all in the same order.
In which case you could take the IndexOf(item) and use the index value to delete the other list items by index.
Otherwise, you need some way to link/relate these guys to each other.
Edit Per Request:
private void btn_Delete_Click(object sender, EventArgs e)
{
if (lstb_seriesName.SelectedItems.Count <= 0)
MessageBox.Show("You need to select an item to delete first!");
else
{
var indexesToRemove = lstb_seriesName.SelectedIndices;
foreach(var index in indexesToRemove)
{
lstb_seriesName.Items.RemoveAt(index);
lstb_seriesDay.Items.RemoveAt(index);
lstb_seriesTime.Items.RemoveAt(index);
lstb_seriesActive.Items.RemoveAt(index);
}
MessageBox.Show("Program deleted!");
}
}