I am trying to get the text that is choosen in a comboBox.
I have tried these codes but no of them are working. What am I doing wrong here ?

std::string Text;

// I have tested all 4 of these and this will not compile.  
//I belevie it has to be something with SelectedItem.

Text = comboBox3->Text;

Text = comboBox3->Text->ToString();

Text = comboBox3->Item->ToString();

Text = comboBox3->SelectedItem();

Recommended Answers

All 15 Replies

What is the type of comboBox3? What library are you using?

I am not sure what library you are using, but if it is similar to one that I'm used to, then it would be something similar to this:

Text = comboBox3->text(i); //where i is the item in the box

or maybe

Text = comboBox3->currentText();

(by the way, I usually use Qt, which is most likely different from what you're doing)

Thanks. I am using VC++ 2008.

If I try these 2 examples:

Text = comboBox3->Text(i)

The compiler says that "i" is undeclared
and currentText() does not exist in my library for

Text = comboBox3->currentText();

i is just the index of what you want to read

I am not sure if I understand what index is ment here. It is the first time I am trying out the comboBox.
Does it meen that I have to specify something instead of "i" here.
What I will get from the comboBox3 is the Text that is choosen there so that is a string anyway.

Index would be the index of the selected item.

Is this .NET?

How do you declare comboBox3?

What happens with Text = comboBox3.Text; ?

Yes I use . Net
If I put:

Text = comboBox3.Text;
Then I will have a compileError.

I have tried out this and this compile:

Text = comboBox3->SelectedIndex;

I beleive this will get the String that is selected from the comboBox ?

Thank You...

Is this .NET?

How do you declare comboBox3?

What happens with Text = comboBox3.Text; ?

Try: Text = comboBox3->SelectedItem;

Or rather: Text = comboBox3->SelectedItem.ToString();

Text = comboBox3->SelectedItem;

This does not compile.

HowEver this does:

Text = comboBox3->SelectedIndex;

I dont know if that is correct but what I will do with this string is this and this does also compile so I think I am doing right here ?

string Text;
Text= comboBox3->SelectedIndex;

string path = "c:\\Folder1\\Groups\\" + Text + ".txt";

Try: Text = comboBox3->SelectedItem;

Check my last post.
SelectedItem not SelectedIndex.

Yes thanks, I tried this out and it sounds most correct to me also but If I write:

Text = comboBox3->SelectedItem.ToString();

This does not compile however.

Check my last post.
SelectedItem not SelectedIndex.

hehe Text = comboBox3->SelectedItem->ToString(); I've never done .NET in c++. I think they do some weird things, if I remember correctly.

Yes you are right. I just figured it out at the same time as you :-)

Text = comboBox3->SelectedItem->ToString();

Thanks a lot for your help.

hehe Text = comboBox3->SelectedItem->ToString(); I've never done .NET in c++. I think they do some weird things, if I remember correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.