Click here to Skip to main content
15,559,491 members
Home / Discussions / C#
   

C#

 
QuestionlistBox Item Removal Pin
electriac22-Oct-12 1:42
electriac22-Oct-12 1:42 
AnswerRe: listBox Item Removal Pin
electriac22-Oct-12 2:07
electriac22-Oct-12 2:07 
GeneralRe: listBox Item Removal Pin
V.22-Oct-12 2:27
professionalV.22-Oct-12 2:27 
AnswerRe: listBox Item Removal Pin
BobJanova22-Oct-12 5:22
BobJanova22-Oct-12 5:22 
GeneralRe: listBox Item Removal Pin
electriac22-Oct-12 6:48
electriac22-Oct-12 6:48 
GeneralRe: listBox Item Removal Pin
DaveyM6922-Oct-12 12:41
professionalDaveyM6922-Oct-12 12:41 
GeneralRe: listBox Item Removal Pin
electriac23-Oct-12 11:22
electriac23-Oct-12 11:22 
GeneralRe: listBox Item Removal Pin
DaveyM6923-Oct-12 23:36
professionalDaveyM6923-Oct-12 23: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 7:27
BobJanova24-Oct-12 7:27 
GeneralRe: listBox Item Removal Pin
DaveyM6924-Oct-12 22:30
professionalDaveyM6924-Oct-12 22:30 
GeneralRe: listBox Item Removal Pin
electriac24-Oct-12 13:35
electriac24-Oct-12 13:35 
AnswerRe: listBox Item Removal Pin
Braj_1223-Oct-12 3:36
Braj_1223-Oct-12 3:36 
GeneralRe: listBox Item Removal Pin
electriac23-Oct-12 11:24
electriac23-Oct-12 11:24 
Questionconstructor call Pin
anshumansingh7121-Oct-12 21:24
anshumansingh7121-Oct-12 21:24 
AnswerRe: constructor call Pin
J4amieC21-Oct-12 22:32
J4amieC21-Oct-12 22:32 
QuestionHow to Send & Receive data using USB port in C# Pin
willington.d21-Oct-12 20:35
willington.d21-Oct-12 20:35 
AnswerRe: How to Send & Receive data using USB port in C# Pin
Richard MacCutchan21-Oct-12 23:00
mveRichard MacCutchan21-Oct-12 23:00 
GeneralRe: How to Send & Receive data using USB port in C# Pin
willington.d22-Oct-12 0:02
willington.d22-Oct-12 0:02 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Richard MacCutchan22-Oct-12 0:24
mveRichard MacCutchan22-Oct-12 0:24 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Dave Kreskowiak22-Oct-12 3:08
mveDave Kreskowiak22-Oct-12 3:08 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 6:07
professionalMarco Bertschi22-Oct-12 6:07 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Dave Kreskowiak22-Oct-12 11:25
mveDave Kreskowiak22-Oct-12 11:25 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 22:35
professionalMarco Bertschi22-Oct-12 22:35 
AnswerRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 6:02
professionalMarco Bertschi22-Oct-12 6:02 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Richard Andrew x6422-Oct-12 8:48
professionalRichard Andrew x6422-Oct-12 8: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.