[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

Editing an listbox item

Yawen Chan

11/21/2002 8:59:00 AM

(C#)
I added items to a listbox using:

listBox_A.Items.Add("Item1");

Then I want to edit the item's string to something else, so I did

listBox_A.SelectedItem = "xxx";

But the item didn't work. The listbox item was still "Item1", but not
"xxx". Why is that?

Thanks a lot.

Yawen


5 Answers

callie

11/21/2002 11:04:00 AM

0

have u selected the item "item1" in the listBox_A before you call
listBox_A.SelectedItem = "xxx";?

"Yawen Chan" <y3chan@engmail.uwaterloo.ca> wrote in message
news:ucyArPTkCHA.716@tkmsftngp11...
> (C#)
> I added items to a listbox using:
>
> listBox_A.Items.Add("Item1");
>
> Then I want to edit the item's string to something else, so I did
>
> listBox_A.SelectedItem = "xxx";
>
> But the item didn't work. The listbox item was still "Item1", but not
> "xxx". Why is that?
>
> Thanks a lot.
>
> Yawen
>
>


(Mike Clay (MSFT))

11/21/2002 5:32:00 PM

0

Hi Yawen,

You probably also need to call:

ListBox_A.SelectedItem.Text = "xxx";

instead of

ListBox_A.SelectedItem = "xxx"

You'll see the difference is calling the Text property of SelectedItem.

Mike Clay, MCSD
Beta Technical Support


This posting is provided "AS IS" with no warranties, and confers no rights.
© 2002 Microsoft Corporation. All rights reserved.

Yawen Chan

11/22/2002 2:35:00 AM

0

"have u selected the item "item1" in the listBox_A before you call
listBox_A.SelectedItem = "xxx";?"

Yes

"You probably also need to call:
ListBox_A.SelectedItem.Text = "xxx"; "

The ListBox_A.SelectedItem returns an Object, which doesn't have the Text
field. So I can't set the text.

What's the other way to do that?

Yawen


"Mike Clay (MS)" <mclay@online.microsoft.com> wrote in message
news:mlPVNuXkCHA.2516@cpmsftngxa06...
> Hi Yawen,
>
> You probably also need to call:
>
> ListBox_A.SelectedItem.Text = "xxx";
>
> instead of
>
> ListBox_A.SelectedItem = "xxx"
>
> You'll see the difference is calling the Text property of SelectedItem.
>
> Mike Clay, MCSD
> Beta Technical Support
>
>
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> © 2002 Microsoft Corporation. All rights reserved.
>


(Felix Wu [MS])

11/25/2002 11:34:00 AM

0

Hi Yawen,

"listBox_A.SelectedItem = "xxx"; " does not work because the SelectedItem
property is designed for setting the currently selected item in the
ListBox. The set_SelectedItem method calls the
ObjectCollection::IndexOf(object) method to get the index of the object you
specified on the right side of the "=" operator. In your case, it is a
string object with the text of "xxx". If a matching object is found in the
ListBox's ObjectCollection, the set_SelectedIndex method will be called
with the returned Index to set the found object to be the current item. If
no matching object is found, the set_SelectedItem method just returns.
This is why nothing happens when you call "listBox_A.SelectedItem = "xxx";

To modify the selected item text, use this method instead:

listBox1.Items[listBox1.SelectedIndex]="New Text";

Hope this helps.

Regards,

Felix Wu
=============
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
>From: "Yawen Chan" <y3chan@engmail.uwaterloo.ca>
>Subject: Editing an listbox item
>Date: Thu, 21 Nov 2002 15:59:34 +0800
>Lines: 17
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
>Message-ID: <ucyArPTkCHA.716@tkmsftngp11>
>Newsgroups:
microsoft.public.dotnet.framework.sdk,microsoft.public.dotnet.framework.wind
owsforms,microsoft.public.dotnet.framework.windowsforms.controls
>NNTP-Posting-Host: 147.8.183.244
>Path: cpmsftngxa08!tkmsftngp01!tkmsftngp11
>Xref: cpmsftngxa08 microsoft.public.dotnet.framework.windowsforms:34033
microsoft.public.dotnet.framework.windowsforms.controls:3960
microsoft.public.dotnet.framework.sdk:4917
>X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
>
>(C#)
>I added items to a listbox using:
>
>listBox_A.Items.Add("Item1");
>
>Then I want to edit the item's string to something else, so I did
>
>listBox_A.SelectedItem = "xxx";
>
>But the item didn't work. The listbox item was still "Item1", but not
>"xxx". Why is that?
>
>Thanks a lot.
>
>Yawen
>
>
>

Joe

12/3/2002 1:07:00 PM

0

You can do it like this:

listBox_A.Items.FindByText("Item1").Text = "xxx";

The reason yours fails is it is not selected when you add
it. to select it do:

listBox_A.Items.FindByText("Item1").Selected = True;
listBox_A.Items.SelectedItem.Text = "xxx"

hope this helps

Joe.

>-----Original Message-----
>(C#)
>I added items to a listbox using:
>
>listBox_A.Items.Add("Item1");
>
>Then I want to edit the item's string to something else,
so I did
>
>listBox_A.SelectedItem = "xxx";
>
>But the item didn't work. The listbox item was
still "Item1", but not
>"xxx". Why is that?
>
>Thanks a lot.
>
>Yawen
>
>
>.
>