Solved

Replace ComboBox ListIndex

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

829 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