Solved

Replace ComboBox ListIndex

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now