listBox Item Removal
-
//My list box contains some empty lines or lines just containing a "\t" and sometimes I want to remove one of these.
//If the selected line contains text the line is removed without problem but when the selected line is blank "", " ", or "\t"
// the removal goes to the first blank line and removes it rather than the selected line.selLoc = ListBox1.SelectedIndex;
listBox1.Items.Remove(listBox1.SelectedItems[0]);
// Removes The first occurance of empty line (not selected)// and this is the same
listBox1.Items.Remove(listBox1.Items[selLoc]);
// Removes The first occurance of empty line (not selected)//How can I remove a blank selected line?</pre>
-
//My list box contains some empty lines or lines just containing a "\t" and sometimes I want to remove one of these.
//If the selected line contains text the line is removed without problem but when the selected line is blank "", " ", or "\t"
// the removal goes to the first blank line and removes it rather than the selected line.selLoc = ListBox1.SelectedIndex;
listBox1.Items.Remove(listBox1.SelectedItems[0]);
// Removes The first occurance of empty line (not selected)// and this is the same
listBox1.Items.Remove(listBox1.Items[selLoc]);
// Removes The first occurance of empty line (not selected)//How can I remove a blank selected line?</pre>
-
5 because you found the answer I was about to give you, yourself :-D
V.
(MQOTD Rules ) -
//My list box contains some empty lines or lines just containing a "\t" and sometimes I want to remove one of these.
//If the selected line contains text the line is removed without problem but when the selected line is blank "", " ", or "\t"
// the removal goes to the first blank line and removes it rather than the selected line.selLoc = ListBox1.SelectedIndex;
listBox1.Items.Remove(listBox1.SelectedItems[0]);
// Removes The first occurance of empty line (not selected)// and this is the same
listBox1.Items.Remove(listBox1.Items[selLoc]);
// Removes The first occurance of empty line (not selected)//How can I remove a blank selected line?</pre>
The reason that this happens is because Remove uses equality checking to work out which line you meant – and the first line with the same text will match. (I really don't think it should do that, but that's a separate discussion.) The answer, as you discovered, is to use RemoveAt.
-
The reason that this happens is because Remove uses equality checking to work out which line you meant – and the first line with the same text will match. (I really don't think it should do that, but that's a separate discussion.) The answer, as you discovered, is to use RemoveAt.
-
The reason that this happens is because Remove uses equality checking to work out which line you meant – and the first line with the same text will match. (I really don't think it should do that, but that's a separate discussion.) The answer, as you discovered, is to use RemoveAt.
BobJanova wrote:
I really don't think it should do that, but that's a separate discussion
... I agree, and you are correct about the equality check. It should use reference checking IMO but then that wouldn't work with boxed value types. Hmm..
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
//My list box contains some empty lines or lines just containing a "\t" and sometimes I want to remove one of these.
//If the selected line contains text the line is removed without problem but when the selected line is blank "", " ", or "\t"
// the removal goes to the first blank line and removes it rather than the selected line.selLoc = ListBox1.SelectedIndex;
listBox1.Items.Remove(listBox1.SelectedItems[0]);
// Removes The first occurance of empty line (not selected)// and this is the same
listBox1.Items.Remove(listBox1.Items[selLoc]);
// Removes The first occurance of empty line (not selected)//How can I remove a blank selected line?</pre>
-
BobJanova wrote:
I really don't think it should do that, but that's a separate discussion
... I agree, and you are correct about the equality check. It should use reference checking IMO but then that wouldn't work with boxed value types. Hmm..
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Use listBox1.Items.RemoveAt(ListBox1.SelectedIndex); in place of your code then it will remove your selected item from List Box.
-
I've just had a quick play with this and actually, it does use reference equality. What's happening is due to string interning - if you don't know what that is have a google :) So, when you add your strings to the list box, if they are the same text then they are actually the same reference, therefore it will remove the first one it finds. A quick solution is to wrap a string in another class and use that in place of string.
public class MyString
{
private string value;public MyString(string value) { this.value = value; } public static implicit operator string(MyString myString) { return myString.value; } public static implicit operator MyString(string value) { return new MyString(value); } public override string ToString() { return value; }
}
MyString one = " ";
MyString two = "2";
MyString three = " ";listBox.Items.Add(one);
listBox.Items.Add(two);
listBox.Items.Add(three);Selecting the third item and calling
listBox.Items.Remove(listBox.SelectedItem);
successfully removes the third item.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
I've just had a quick play with this and actually, it does use reference equality. What's happening is due to string interning - if you don't know what that is have a google :) So, when you add your strings to the list box, if they are the same text then they are actually the same reference, therefore it will remove the first one it finds. A quick solution is to wrap a string in another class and use that in place of string.
public class MyString
{
private string value;public MyString(string value) { this.value = value; } public static implicit operator string(MyString myString) { return myString.value; } public static implicit operator MyString(string value) { return new MyString(value); } public override string ToString() { return value; }
}
MyString one = " ";
MyString two = "2";
MyString three = " ";listBox.Items.Add(one);
listBox.Items.Add(two);
listBox.Items.Add(three);Selecting the third item and calling
listBox.Items.Remove(listBox.SelectedItem);
successfully removes the third item.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
I've just had a quick play with this and actually, it does use reference equality. What's happening is due to string interning - if you don't know what that is have a google :) So, when you add your strings to the list box, if they are the same text then they are actually the same reference, therefore it will remove the first one it finds. A quick solution is to wrap a string in another class and use that in place of string.
public class MyString
{
private string value;public MyString(string value) { this.value = value; } public static implicit operator string(MyString myString) { return myString.value; } public static implicit operator MyString(string value) { return new MyString(value); } public override string ToString() { return value; }
}
MyString one = " ";
MyString two = "2";
MyString three = " ";listBox.Items.Add(one);
listBox.Items.Add(two);
listBox.Items.Add(three);Selecting the third item and calling
listBox.Items.Remove(listBox.SelectedItem);
successfully removes the third item.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Oh, my mistake then. E: Although your test doesn't actually test what you think it does. The MyString class has no equality checking, so it will always use reference equality. Try adding Equals, ==, != and Hashcode to it and see what the result is.
Yes, that was deliberate as the OP was wanting reference checking so two identical strings were treated as different objects. Even with those added it behaves as expected - it's just the interning of the string class that screws it! Example below. Warning: No null checking on
value
using System;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
// listBox added in designer
// buttonRemove added in designer
// buttonRemove Click handler added in designer
public Form1()
{
InitializeComponent();
listBox.Items.Add(new MyString(" "));
listBox.Items.Add(new MyString("2"));
listBox.Items.Add(new MyString(" "));
}private void buttonRemove\_Click(object sender, EventArgs e) { if (listBox.SelectedItem != null) listBox.Items.Remove(listBox.SelectedItem); } }
}
public class MyString : IEquatable
{
private string value;public MyString(string value) { this.value = value; } public static implicit operator MyString(string value) { return new MyString(value); } public static implicit operator string(MyString myString) { return myString.value; } public static bool operator ==(MyString first, MyString other) { if (object.ReferenceEquals(first, other)) return true; if (object.ReferenceEquals(null, first) || object.ReferenceEquals(null, other)) return false; return first.value == other.value; } public static bool operator !=(MyString first, MyString other) { return !(first == other); } public override bool Equals(object obj) { return Equals(obj as MyString); } public bool Equals(MyString other) { if (object.ReferenceEquals(null, other)) return false; return value.Equals(other.value); } public override int GetHashCode() { return value.GetHashCode(); } public override string ToString() { return value; }
}
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us.