Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replace ComboBox ListIndex

Posted on 2004-08-25
3
Medium Priority
?
1,269 Views
Last Modified: 2008-02-01
Hey Experts,

The following code is from VB6, and it actually replace the ListIndex of a ComboBox with my table's primary Key.
Does someone knows of any way to do this in C# ?


mission.AddItem tb_mission("mission_type_name_e")
mission.ItemData(mission.NewIndex) = Val(tb_mission("mission_type_id"))

val_mission_type = combo_mission_type.ItemData(combo_mission_type.ListIndex)
0
Comment
Question by:Francis_B
  • 2
3 Comments
 
LVL 1

Expert Comment

by:chmohan
ID: 11897011
use combobox.SelectedIndex in c#  .net
0
 
LVL 19

Accepted Solution

by:
drichards earned 2000 total points
ID: 11897133
You don't do things quite the same way with a .NET ComboBox.  Instead if having strings in the list and itemData for each item, .NET puts an object in the list and uses the default string representation (ToString() method) of the object for display.  So, this means in .NET (C# in your case) you need to create a class that holds the string value and key value from the data table.  The ToString() method should return the string value.  For each item in the combobox, you create a new instance of the object and do 'comboBox1.Add(newItem);' where newItem is an object instance.   It will look something like this:

        class CBItem
        {
            public CBItem(string name, int key) { m_name = name; m_key = key; }
            public override string ToString() { return m_name; }
            public int Key() { return m_key; }

            private int m_key;
            private string m_name;

        }


Then in your code loading the combobox:

   comboBox1.Items.Add(new CBItem(<db string>, <db key>)); // replace <db name> and <db key> with appropriate values

Then to view "item data":

   int key = ((CBItem)comboBox1.Items[idx]).Key();
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question