Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit Item in DropDownList

Status
Not open for further replies.

AT76

Technical User
Apr 14, 2005
460
US
Hi,

I have a DropDownList with 3 string items. Is there a way to edit the text of of one of the items in my list?

Thanks!
 
this.my_combo_box.Items[0] = "This is number one";
this.my_combo_box.Items[1] = "This is number two";
this.my_combo_box.Items[2] = "This is number three";


 
Thanks gplusplus! If you can do it by way of index, my next question is how would find the index of a particular field if you know the text?
 
OK, I got it using a for loop! Thanks for your help!
 
Code:
string key = "This is the text i want";

for(int i = 0; i < this.my_combo_box.Items.Count; i++)
{
     if(this.my_combo_box.Items[i].ToString() == key )
     {
         MessageBox.Show("Found at :: " + i.toString());
              return;

     }
}
 
Why use a loop when the functionality is already built into the ComboBox?

I'll leave you to convert this from VB to C#

either:
Code:
    Dim position As Integer = ComboBox1.FindStringExact("<TextToFind>")
    If position >= 0 Then ComboBox1.Items(position) = "<ReplacementText>"

or:

Code:
    Dim position As Integer = ComboBox1.FindString("<TextToFind>")
    If position >= 0 Then ComboBox1.Items(position) = "<ReplacementText>"

Hope this helps


[vampire][bat]
 
thank you all...

Earthandfire, I'm trying to apply your approach but in c#, my dropdownlist does not have builtin functions FindString or FindStringExact. Is a ComboBox the same as a DropDownList? Thanks!
 
I missed that in you original post -sorry. I know nothing about ASP.NET or its controls.

[vampire][bat]
 
That's just because in C# combobox lists objects, not strings. Kinda stupid looking for those coming from C++ to be honest.

------------------
When you do it, do it right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top