Managed C++ ComboBox Issues

This topic is 4852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Ok, so here is the deal. I have in my program for it setup to get information from a specific directory, and fill in ComboBox A. Now comes up problem 1. When the information is put into ComboBox A, I would like for my program to automatically fill in the blank field with the first option, instead of just leaving it blank. From there, I want ComboBox A to run an event that effects ComboBox B. What I want to happen, is I want ComboBox B to read information from a subdirectory of the selected choice in ComboBox A. Now comes problem 2. The event is setup in the header file (as I just double clicked on the combobox for the event), and I cannot seem to get it to where I can use DirectoryInfo* in the header file. Questions being: 1. How can I make ComboBox A automatically highlight option 1? 2. How can I make ComboBox A's SelectedIndexChanged event either A. Allow usage of the DirectoryInfo code B. be put into the main .cpp file.

Share on other sites
PyroBlizzard

1. You can make ComoboBoxA automatically highlight option 1 by calling the following when you read in the directory info:

comboBoxA->SelectedIndex = 0;

2. I'm not sure what you mean here. If you mean, "How do I populate box2 with the subdirectories located in the directory selected in ComboA," the answer is, create an event handler for "SelectedIndexChange" for ComboA, then whenever the event is called, just get the name of the directory from the selected index of boxA, then get the directories much as you did for ComboA the first time. Use the new list of subdirectories or files to populate ComboB. Here's a quick code example of that:

// Please note that its 12:26AM.  This code is not as clean as it could be. =)// But it should serve its purposeForm1(void){    InitializeComponent();    DirectoryInfo* di = new DirectoryInfo(S"C:\\Workspace\\MyDir");    if( di->Exists )     {        String* dirs[] = Directory::GetDirectories( S"c:\\Workspace\\MyDir" );        Collections::IEnumerator* myEnum = dirs->GetEnumerator();        while( myEnum->MoveNext() )         {	    comboBox1->Items->Add( myEnum->Current );	}        comboBox1->SelectedIndex = 0;    }}private: System::Void comboBox1_SelectedIndexChanged(System::Object *  sender, System::EventArgs *  e){    int index = comboBox1->SelectedIndex;    String* pString = comboBox1->Items->Item[index]->ToString();    comboBox2->Items->Clear();    comboBox2->Text = S"";    DirectoryInfo* di = new DirectoryInfo( pString );    if( di->Exists )     {        String* dirs[] = Directory::GetDirectories( pString );	Collections::IEnumerator* myEnum = dirs->GetEnumerator();	while( myEnum->MoveNext() ) 	{	    comboBox2->Items->Add( myEnum->Current );	}        comboBox2->SelectedIndex = 0;    }}

3. I wrote a quick mock-up Managed C++ application. I was able to use the DirectoryInfo class just fine in the .h file by adding the following "using" statement:

using namespace System::IO;

Share on other sites
System:IO is the only one I didnt put in the header file, and now I feel stupid, cause I remember that is right. Thank you so much for all your help.

Share on other sites
Ok, new problem. First, the code

private: System::Void cPlayerAccounts_SelectedIndexChanged(System::Object *  sender, System::EventArgs *  e)		 {			System::String* directory = this->tWoWDir->get_Text();			System::String* dirName = String::Concat(directory, "\\WTF\\Account\\");			System::String* accountName = this->cPlayerAccounts->			DirectoryInfo* accountFolder = String::Concat(dirName, accountName);			if (accountFolder->Exists)			{				DirectoryInfo* playerName[] = accountFolder->GetDirectories();				for (int i = 0; i < playerName->Length; i++)				{					this->cPlayerCharacters->Items->Add(playerName);				}			}		 }

Now, the issue is the System::String* accountName part. I dont know how to get the text of the currently selected item in ComboBox A (cPlayerAccounts). How would I go about doing this?

Share on other sites
PyroBlizzard,

If you check in the example code I provided you'll see the following:

int index = comboBox1->SelectedIndex;
String* pString = comboBox1->Items->Item[index]->ToString();

The first line gets the currently selected item from comboBox1, and then passes it into the array of items in order to get the string at that index.

For you that would be:

int index = cPlayerAccounts->SelectedIndex;
System::String* accountName = this->cPlayerAccounts->Items->Item[index]->ToString();

Again, good luck!

Share on other sites
Alright, Thank you so much for your help, JWalsh, and I have increased your rating cause of it.

New issue. Im running an open file dialog box, and I have it set that when the user clicks on ok, I need it to rerun a function that I have defined in my main cpp file. Only problem is that, once again, the event handlers are in the header file, and I cannot seem to figure out how I would run my function that is defined in my cpp file. Any ideas?

EDIT: Got it somewhat. I just copied over the function code into my event in the header file. Must be some better way though.

Anyways, new thing. I dont need an open file dialog box, I need an open directory dialog box. Any idea?

[Edited by - PyroBlizzard on June 8, 2005 7:31:29 PM]

Share on other sites
Ok, nevermind on that last one as well, as I figured it out. Just used a Regex to split it up into an array and used the first part of the array. Now I need to know how to send a post request to a server CGI script. Any ideas?

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
16
5. 5

• 9
• 12
• 9
• 33
• 13
• Forum Statistics

• Total Topics
632593
• Total Posts
3007277

×