469,356 Members | 1,994 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

how i can fill combo box?

hi dear
i'm programmer with VB6, but now i starting with C#.
first problem is coming when i need fill "ComboBox Control", as you know
when i fill combobox in VB6 by this code:

comboX.AddItem "Monday"
comboX.ItemData (comboX.NewIndex) = 1
comboX.text = ComboX.List(0)

where:
Item is what Client can see.
ItemData what i want to know to make something (its like a key).

how i can do that with C# code?
thanks
-----------
Tarek M. Siala
Software Developer
http://tarksiala.blogspot.com
Jul 21 '07 #1
5 22358
"Tark Siala" <ta*******@icc-libya.comwrote in message
news:eF****************@TK2MSFTNGP02.phx.gbl...
hi dear
i'm programmer with VB6, but now i starting with C#.
first problem is coming when i need fill "ComboBox Control", as you know
when i fill combobox in VB6 by this code:

comboX.AddItem "Monday"
comboX.ItemData (comboX.NewIndex) = 1
comboX.text = ComboX.List(0)

where:
Item is what Client can see.
ItemData what i want to know to make something (its like a key).

how i can do that with C# code?
thanks
-----------
Tarek M. Siala
Software Developer
http://tarksiala.blogspot.com

You need to learn about the Items collection of the ComboBox class.
This will help...

http://msdn2.microsoft.com/en-us/lib...ms(vs.80).aspx

Jul 21 '07 #2

"Tark Siala" <ta*******@icc-libya.comwrote in message
news:eF****************@TK2MSFTNGP02.phx.gbl...
hi dear
i'm programmer with VB6, but now i starting with C#.
first problem is coming when i need fill "ComboBox Control", as you know
when i fill combobox in VB6 by this code:

comboX.AddItem "Monday"
comboX.ItemData (comboX.NewIndex) = 1
comboX.text = ComboX.List(0)

where:
Item is what Client can see.
ItemData what i want to know to make something (its like a key).
Create a class to hold your data, override ToString() to control what the
user sees:

class ListBoxEntry
{
public int ItemData;
public string Caption;

public ListBoxEntry(int key, string text) { ItemData = key; Caption =
text; }
public override string ToString() { return Caption; }
}

comboX.Items.Add(new ListBoxEntry(1, "Monday"));
Jul 24 '07 #3
thanks

but how i can access to any "itemdata" or know what "itemdata" is selected
now in combobox?

for example: in VB6 i write this code:
Id = vba.trim(me.combobox.itemdata(vba.abs(me.combobox. listindex)))
how i can read itemdata in C# like VB6 ?

thanks
-----------
Tarek M. Siala
Software Developer
http://tarksiala.blogspot.com

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:uU**************@TK2MSFTNGP04.phx.gbl...
>
"Tark Siala" <ta*******@icc-libya.comwrote in message
news:eF****************@TK2MSFTNGP02.phx.gbl...
>hi dear
i'm programmer with VB6, but now i starting with C#.
first problem is coming when i need fill "ComboBox Control", as you know
when i fill combobox in VB6 by this code:

comboX.AddItem "Monday"
comboX.ItemData (comboX.NewIndex) = 1
comboX.text = ComboX.List(0)

where:
Item is what Client can see.
ItemData what i want to know to make something (its like a key).

Create a class to hold your data, override ToString() to control what the
user sees:

class ListBoxEntry
{
public int ItemData;
public string Caption;

public ListBoxEntry(int key, string text) { ItemData = key; Caption =
text; }
public override string ToString() { return Caption; }
}

comboX.Items.Add(new ListBoxEntry(1, "Monday"));


Jul 25 '07 #4
On 25 Jul., 13:37, "Tark Siala" <tarksi...@icc-libya.comwrote:
thanks

but how i can access to any "itemdata" or know what "itemdata" is selected
now in combobox?

for example: in VB6 i write this code:
Id = vba.trim(me.combobox.itemdata(vba.abs(me.combobox. listindex)))
how i can read itemdata in C# like VB6 ?

thanks
-----------
Tarek M. Siala
Software Developerhttp://tarksiala.blogspot.com

"Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote in messagenews:uU**************@TK2MSFTNGP04.phx.gbl. ..
"Tark Siala" <tarksi...@icc-libya.comwrote in message
news:eF****************@TK2MSFTNGP02.phx.gbl...
hi dear
i'm programmer with VB6, but now i starting with C#.
first problem is coming when i need fill "ComboBox Control", as you know
when i fill combobox in VB6 by this code:
comboX.AddItem "Monday"
comboX.ItemData (comboX.NewIndex) = 1
comboX.text = ComboX.List(0)
where:
Item is what Client can see.
ItemData what i want to know to make something (its like a key).
Create a class to hold your data, override ToString() to control what the
user sees:
class ListBoxEntry
{
public int ItemData;
public string Caption;
public ListBoxEntry(int key, string text) { ItemData = key; Caption =
text; }
public override string ToString() { return Caption; }
}
comboX.Items.Add(new ListBoxEntry(1, "Monday"));
A combo/listbox doesn't have "itemdata" as in VB6, instead you can
access the combo-box's value/tag as an object of any datatype you
like.

1) create a windows application.
2) Add 1 combobox (named comboxBox1) , 1 button named button1 and 2
labels named label1 and label2.
3) Add this class to your form-class:

public class ComboBoxItem
{
private string _Caption = "No Caption Set";
private int _Value = 0;

public string Caption
{
get
{
return this._Caption;
}
set
{
this._Caption = value;
}
}
public int Value
{
get
{
return this._Value;
}
set
{
this._Value = value;
}
}
}

Add paste this code into the form-class's button1_click event (double
click on the button to get the event-code):
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
label1.Text = "Caption:" +
((ComboBoxItem)comboBox1.SelectedItem).Caption;
label2.Text = "Value:" +
((ComboBoxItem)comboBox1.SelectedItem).Value.ToStr ing();
}
}

and then this :

void LoadCombo()
{
// This can/should only be done once.
comboBox1.DisplayMember = "Caption"; // Member from the ComboBoxItem
class to visualize.
// Using self-made class, for pupulation of the combobox.
// See ComboBoxItem (easy simple class)
ComboBoxItem CI;

CI = new ComboBoxItem();
CI.Caption = "My Caption";
CI.Value = 10;
comboBox1.Items.Add(CI); // Now the comboxbox item consists of a
ComboBoxItem (which can be any class).

CI = new ComboBoxItem();
CI.Caption = "My Caption2";
CI.Value = 20;
comboBox1.Items.Add(CI); // Now the comboxbox item consists of a
ComboBoxItem (which can be any class).
}

Now, add this to the form's load event:

private void Form1_Load(object sender, EventArgs e)
{
LoadCombo();
}
That's it ! - You may design your ComboBoxItem class just as you need,
it can be anything, and the DisplayMember can even hold bitmaps for
visualizing icons etc. as far as i remember! - Try it out ! :-)

Regards
Brian Bergh
Jul 25 '07 #5

"Tark Siala" <ta*******@icc-libya.comwrote in message
news:u0**************@TK2MSFTNGP05.phx.gbl...
thanks

but how i can access to any "itemdata" or know what "itemdata" is selected
now in combobox?

for example: in VB6 i write this code:
Id = vba.trim(me.combobox.itemdata(vba.abs(me.combobox. listindex)))
how i can read itemdata in C# like VB6 ?
int Id = ((ListBoxEntry)(comboBox1.SelectedItem)).ItemData;
>
thanks
-----------
Tarek M. Siala
Software Developer
http://tarksiala.blogspot.com

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:uU**************@TK2MSFTNGP04.phx.gbl...
>>
"Tark Siala" <ta*******@icc-libya.comwrote in message
news:eF****************@TK2MSFTNGP02.phx.gbl...
>>hi dear
i'm programmer with VB6, but now i starting with C#.
first problem is coming when i need fill "ComboBox Control", as you know
when i fill combobox in VB6 by this code:

comboX.AddItem "Monday"
comboX.ItemData (comboX.NewIndex) = 1
comboX.text = ComboX.List(0)

where:
Item is what Client can see.
ItemData what i want to know to make something (its like a key).

Create a class to hold your data, override ToString() to control what the
user sees:

class ListBoxEntry
{
public int ItemData;
public string Caption;

public ListBoxEntry(int key, string text) { ItemData = key; Caption =
text; }
public override string ToString() { return Caption; }
}

comboX.Items.Add(new ListBoxEntry(1, "Monday"));



Aug 2 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Eugene Anthony | last post: by
3 posts views Thread by Antoine Janssen | last post: by
3 posts views Thread by Kannan | last post: by
2 posts views Thread by Jacques | last post: by
2 posts views Thread by Jeremy Dillinger | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.