Solved

Replace ComboBox ListIndex

Posted on 2004-08-25
3
1,241 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 1

Expert Comment

by:chmohan
ID: 11897021
0
 
LVL 19

Accepted Solution

by:
drichards earned 500 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Web Form VB.Net  import CSV 4 39
Moving code from one TFS branch to another 3 25
Hey!!!! 1 22
What Does This C# Code Block Do? 5 30
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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