Thread: Create new combo boxes based on items selected in preview combo box

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Create new combo boxes based on items selected in preview combo box

    I am trying to write a very simple Windows Forms application program using Visual C++ 2005. The title says it all but I would like to expand on it a little.

    What I am ultimately trying to do, is give a list of several different options (through the drop down combo boxes) that lead to more options (more combo boxes). For example, lets say that I have 1 combo box to start with 2 items in the list (yes and no). I need to be able to create a new combo box with their own unique items based on what was selected. If 'yes' was selected then I need to create a combo box that lists 4 or 5 items. If 'no' is selected then I need to create a different combo box with 1 or 2 items.

    Ultimately, when all the required choices have been made, I need to print out the answers chosen to a file.

    I am not new to programming but I am new to using Visual C++ 2005 (I am used to using Dev-C++ and Code::Blocks). Windows programming is a bit new to me as well (currently on chapter 3 of "Programming Windows" by Charles Petzold). So , here is a summary of what I am needing to know.

    1. How do I create a combo box based on which item was selected from a previous combo box.

    2. What is the least time consuming way of writing all of the chosen answers to a file?

    3. Reading the book I listed above, am I on the right track to learning how to use and code with Visual C++ 2005?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    1479
    Join Date
    Aug 2003
    Posts
    253

    Talking Wooohooo! Solved part of it myself!

    Ok, after spending several days toying with this I managed to figure out part of the problem myself.

    Instead of creating new combo boxes based on the items selected in a previous combo box I changed it up a bit. I have 1 combo box with a list of several items (labled 1-5 for simplicity reasons). Then I have a series of checkboxes that are disabled (Enabled = false). Then I just plugged in this code and got the results I was looking for ...
    Code:
    private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
    				 if (this->comboBox1->Text == "1"){
    					 this->checkBox1->Enabled = true;
    				  }
    				 else {
    					 this->checkBox1->Enabled = false;
    				 }
    			 }
    For the first time I would like to thank everyone for not helping me so far. I learned a lot of things (like how much normal C++ works with Visual C++ 2005 almost flawlessly). I guess thats the magic of .NET?

    Anyhow, part of the problem still remains. I need to collect all the information from ComboBoxes, CheckBoxes and TextBoxes and output all the information into a file. Since I pretty much know how to get variables and values to a file with C++ I don't think I should have to much trouble doing it with Visual C++.

    Oh the irony! Thanks for nothing!
    Knowledge is power and I want it all

    -0RealityFusion0-

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Is this what you're attempting to accomplish?

    Code:
    #pragma comment( lib, "user32.lib" ) 
    
    #include <windows.h>
    #include <string.h>
    
    #define ID_LIST 1
    #define ID_TEXT 2
    
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    char szClassName[ ] = "ComboBox App";
    
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
    					HINSTANCE hPrevInstance,
    					LPSTR lpszArgument,
    					int nCmdShow)
    {
    	HWND hwnd;             
    	MSG messages;          
    	WNDCLASSEX wincl;      
    	wincl.hInstance = hThisInstance;
    	wincl.lpszClassName = szClassName;
    	wincl.lpfnWndProc = WindowProcedure;    
    	wincl.style = CS_DBLCLKS;               
    	wincl.cbSize = sizeof (WNDCLASSEX);
    	wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    	wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    	wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    	wincl.lpszMenuName = NULL;              
    	wincl.cbClsExtra = 0;                   
    	wincl.cbWndExtra = 0;                   
    	wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    	if (!RegisterClassEx (&wincl))
    		return 0;
    	hwnd = CreateWindowEx (
    		0,                 
    		szClassName,       
    		"ComboBox App",     
    		WS_OVERLAPPEDWINDOW, 
    		CW_USEDEFAULT,       
    		CW_USEDEFAULT,       
    		544,                 
    		375,                 
    		HWND_DESKTOP,       
    		NULL,               
    		hThisInstance,      
    		NULL                
    		);
    	ShowWindow (hwnd, nCmdShow);
    
    	while (GetMessage (&messages, NULL, 0, 0))
    	{
    		TranslateMessage(&messages);
    		DispatchMessage(&messages);
    	}
    	return messages.wParam;
    }
    
    void FillListBox(HWND hwndList, DWORD items)
    {
    	TCHAR *pVarName1[] = {"Yes", "No"};
    	TCHAR *pVarName2[] = {"Yes_1","No_1" };
    	TCHAR *pVarName3[] = {"No_1_Second", "No_2_Second","No_3_second", "Go to Finished" };
    	TCHAR *pVarName4[] = {"Yes_1_Second", "Yes_2_Second","Yes_3_Second", "Go to Finished" };
    	TCHAR *pVarName5[] = {"Terminate App", "Go Back to Start"};
    	switch(items)
    	{
    		case 1:
    			SendMessage( hwndList, CB_RESETCONTENT, 0, 0 );
    			for(int i=0; i<2; i++)
    				SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)pVarName1[i]);
    			SendMessage(hwndList, CB_SETCURSEL, 0, 0);
    			break;
    		case 2:
    			SendMessage( hwndList, CB_RESETCONTENT, 0, 0 );
    			for(int i=0; i<2; i++)
    				SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)pVarName2[i]);
    			SendMessage(hwndList, CB_SETCURSEL, 0, 0);
    			break;
    		case 3:
    			SendMessage( hwndList, CB_RESETCONTENT, 0, 0 );
    			for(int i=0; i<4; i++)
    				SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)pVarName3[i]);
    			SendMessage(hwndList, CB_SETCURSEL, 0, 0);
    			break;
    		case 4:
    			SendMessage( hwndList, CB_RESETCONTENT, 0, 0 );
    			for(int i=0; i<4; i++)
    				SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)pVarName4[i]);
    			SendMessage(hwndList, CB_SETCURSEL, 0, 0);
    			break;
    		case 5:
    			SendMessage( hwndList, CB_RESETCONTENT, 0, 0 );
    			for(int i=0; i<2; i++)
    				SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)pVarName5[i]);
    			SendMessage(hwndList, CB_SETCURSEL, 0, 0);
    			break;
    		default:
    			break;
    	}
    }
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static HWND hwndList, hwndText;
    	int iIndex, iLength, cxChar, cyChar;
    	TCHAR pVarName[30];
    	switch (message)                  /* handle the messages */
    	{
    		case WM_CREATE:
    			cxChar = LOWORD(GetDialogBaseUnits());
    			cyChar = HIWORD(GetDialogBaseUnits());
    			hwndText = CreateWindow(TEXT("static"),NULL, WS_CHILD | WS_VISIBLE |
    				SS_LEFT, cxChar, cyChar, 
    				GetSystemMetrics(SM_CXSCREEN), cyChar, 
    				hwnd, (HMENU)ID_TEXT, 
    				(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), 
    				NULL);
    			hwndList =  CreateWindow(TEXT("Combobox"), NULL, WS_CHILD | WS_VISIBLE |
    				LBS_STANDARD, cxChar, cyChar*3, 
    				cxChar*16 + GetSystemMetrics(SM_CXVSCROLL), 
    				cyChar*5, hwnd, (HMENU)ID_LIST, 
    				(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
    				NULL);
    			FillListBox(hwndList, 1);
    
    			GetWindowText(hwndList, pVarName, 30);
    			SetWindowText(hwndText, pVarName);
    			return 0;
    
    		case WM_COMMAND:
    
    			if( LOWORD(wParam)==ID_LIST && HIWORD(wParam)==CBN_SELCHANGE)
    			{
    				GetWindowText(hwndList, pVarName, 30);      
    				if( strcmp(pVarName,"Yes") == 0)
    				{
    					SetWindowText(hwndText, pVarName);
    					FillListBox(hwndList, 2);
    				}
    				else if( strcmp(pVarName,"No") == 0)
    				{
    					SetWindowText(hwndText, pVarName);
    					FillListBox(hwndList, 3);
    				}
    				else if( strcmp(pVarName,"Yes_1") == 0)
    				{
    					SetWindowText(hwndText, pVarName);
    					FillListBox(hwndList, 4);
    				}
    				else if( strcmp(pVarName,"Go to Finished") == 0)
    				{
    					SetWindowText(hwndText, pVarName);
    					FillListBox(hwndList, 5);
    				}
    				else if( strcmp(pVarName,"Go Back to Start") == 0)
    				{
    					SetWindowText(hwndText, pVarName);
    					FillListBox(hwndList, 1);
    				}
    				else if( strcmp(pVarName,"Terminate App") == 0)
    				{
    					SetWindowText(hwndText, "App to be terminated");        
    					MessageBox(NULL, "We Will Terminate App", "TERMINATE", MB_OK);
    					PostQuitMessage (0);  
    				}
    				else    SetWindowText(hwndText, pVarName);
    			}   
    			return 0;    
    
    		case WM_DESTROY:
    			PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
    			break;
    		default: 
    			return DefWindowProc (hwnd, message, wParam, lParam);
    	}
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  2. No data showing in combo box
    By PJYelton in forum Windows Programming
    Replies: 6
    Last Post: 04-29-2005, 07:20 PM
  3. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Adding Items to Combo Boxes
    By tenthirty in forum Windows Programming
    Replies: 10
    Last Post: 12-21-2001, 02:37 AM