Click here to Skip to main content
15,881,882 members
Home / Discussions / C#
   

C#

 
QuestionlistBox Item Removal Pin
electriac22-Oct-12 0:42
electriac22-Oct-12 0:42 
AnswerRe: listBox Item Removal PinPopular
electriac22-Oct-12 1:07
electriac22-Oct-12 1:07 
GeneralRe: listBox Item Removal Pin
V.22-Oct-12 1:27
professionalV.22-Oct-12 1:27 
AnswerRe: listBox Item Removal Pin
BobJanova22-Oct-12 4:22
BobJanova22-Oct-12 4:22 
GeneralRe: listBox Item Removal Pin
electriac22-Oct-12 5:48
electriac22-Oct-12 5:48 
GeneralRe: listBox Item Removal Pin
DaveyM6922-Oct-12 11:41
professionalDaveyM6922-Oct-12 11:41 
GeneralRe: listBox Item Removal Pin
electriac23-Oct-12 10:22
electriac23-Oct-12 10:22 
GeneralRe: listBox Item Removal Pin
DaveyM6923-Oct-12 22:36
professionalDaveyM6923-Oct-12 22:36 
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 Smile | :)

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.
C#
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;
    }
}

C#
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)



GeneralRe: listBox Item Removal Pin
BobJanova24-Oct-12 6:27
BobJanova24-Oct-12 6:27 
GeneralRe: listBox Item Removal Pin
DaveyM6924-Oct-12 21:30
professionalDaveyM6924-Oct-12 21:30 
GeneralRe: listBox Item Removal Pin
electriac24-Oct-12 12:35
electriac24-Oct-12 12:35 
AnswerRe: listBox Item Removal Pin
Braj_1223-Oct-12 2:36
Braj_1223-Oct-12 2:36 
GeneralRe: listBox Item Removal Pin
electriac23-Oct-12 10:24
electriac23-Oct-12 10:24 
Questionconstructor call Pin
anshumansingh7121-Oct-12 20:24
anshumansingh7121-Oct-12 20:24 
AnswerRe: constructor call PinPopular
J4amieC21-Oct-12 21:32
J4amieC21-Oct-12 21:32 
QuestionHow to Send & Receive data using USB port in C# Pin
willington.d21-Oct-12 19:35
willington.d21-Oct-12 19:35 
AnswerRe: How to Send & Receive data using USB port in C# Pin
Richard MacCutchan21-Oct-12 22:00
mveRichard MacCutchan21-Oct-12 22:00 
GeneralRe: How to Send & Receive data using USB port in C# Pin
willington.d21-Oct-12 23:02
willington.d21-Oct-12 23:02 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Richard MacCutchan21-Oct-12 23:24
mveRichard MacCutchan21-Oct-12 23:24 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Dave Kreskowiak22-Oct-12 2:08
mveDave Kreskowiak22-Oct-12 2:08 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 5:07
protectorMarco Bertschi22-Oct-12 5:07 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Dave Kreskowiak22-Oct-12 10:25
mveDave Kreskowiak22-Oct-12 10:25 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 21:35
protectorMarco Bertschi22-Oct-12 21:35 
AnswerRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 5:02
protectorMarco Bertschi22-Oct-12 5:02 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Richard Andrew x6422-Oct-12 7:48
professionalRichard Andrew x6422-Oct-12 7:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.