Combo Box

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 64
Reputation: atrusmre is an unknown quantity at this point 
Solved Threads: 0
atrusmre's Avatar
atrusmre atrusmre is offline Offline
Junior Poster in Training

Combo Box

 
0
  #1
Oct 29th, 2005
I'm sorry I've been posting so much, I'm still learning. My question how do you fill a combo using a varible. for example I want to fill my combo box in Visual C++ with something like this.
  1. char *names[2];
  2. names[0] = "Joe";
  3. names[1] = "Billy";
  4. names[2] = "Bob";
Please note this is an example, I realize that I could just fill it myself w/ this example.
Thanx again,
Atrus
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Combo Box

 
0
  #2
Oct 29th, 2005
Should be very similar to adding items to a listbox. Check out code snippet:
http://www.daniweb.com/code/snippet79.html
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 64
Reputation: atrusmre is an unknown quantity at this point 
Solved Threads: 0
atrusmre's Avatar
atrusmre atrusmre is offline Offline
Junior Poster in Training

Re: Combo Box

 
0
  #3
Oct 31st, 2005
Got anything a little easier?
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 287
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 28
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz in Training

Re: Combo Box

 
0
  #4
Oct 31st, 2005
Originally Posted by atrusmre
Got anything a little easier?


How about this:

  1. #include <windows.h>
  2.  
  3. #define ID_LIST 1
  4. #define ID_TEXT 2
  5.  
  6. /* Declare Windows procedure */
  7. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  8.  
  9. /* Make the class name into a global variable */
  10. char szClassName[ ] = "ComboBox App";
  11.  
  12. int WINAPI WinMain (HINSTANCE hThisInstance,
  13. HINSTANCE hPrevInstance,
  14. LPSTR lpszArgument,
  15. int nFunsterStil)
  16.  
  17. {
  18. HWND hwnd; /* This is the handle for our window */
  19. MSG messages; /* Here messages to the application are saved */
  20. WNDCLASSEX wincl; /* Data structure for the windowclass */
  21.  
  22. /* The Window structure */
  23. wincl.hInstance = hThisInstance;
  24. wincl.lpszClassName = szClassName;
  25. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  26. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  27. wincl.cbSize = sizeof (WNDCLASSEX);
  28.  
  29. /* Use default icon and mouse-pointer */
  30. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  31. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  32. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  33. wincl.lpszMenuName = NULL; /* No menu */
  34. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  35. wincl.cbWndExtra = 0; /* structure or the window instance */
  36. /* Use Windows's default color as the background of the window */
  37. wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  38.  
  39. /* Register the window class, and if it fails quit the program */
  40. if (!RegisterClassEx (&wincl))
  41. return 0;
  42.  
  43. /* The class is registered, let's create the program*/
  44. hwnd = CreateWindowEx (
  45. 0, /* Extended possibilites for variation */
  46. szClassName, /* Classname */
  47. "ComboBox App", /* Title Text */
  48. WS_OVERLAPPEDWINDOW, /* default window */
  49. CW_USEDEFAULT, /* Windows decides the position */
  50. CW_USEDEFAULT, /* where the window ends up on the screen */
  51. 544, /* The programs width */
  52. 375, /* and height in pixels */
  53. HWND_DESKTOP, /* The window is a child-window to desktop */
  54. NULL, /* No menu */
  55. hThisInstance, /* Program Instance handler */
  56. NULL /* No Window Creation data */
  57. );
  58.  
  59. /* Make the window visible on the screen */
  60. ShowWindow (hwnd, nFunsterStil);
  61.  
  62. /* Run the message loop. It will run until GetMessage() returns 0 */
  63. while (GetMessage (&messages, NULL, 0, 0))
  64. {
  65. /* Translate virtual-key messages into character messages */
  66. TranslateMessage(&messages);
  67. /* Send message to WindowProcedure */
  68. DispatchMessage(&messages);
  69. }
  70.  
  71. /* The program return-value is 0 - The value that PostQuitMessage() gave */
  72. return messages.wParam;
  73. }
  74.  
  75.  
  76. //User Defined Function(s)
  77.  
  78. //Compare the efficiency of my C++ function vs. Charles Petzold's C function
  79. //Page #407
  80. void FillListBox(HWND hwndList)
  81. {
  82. TCHAR *pVarName[] = {"Item1", "Item2", "Item3", "Item4", "Item5"};
  83.  
  84. for(int i=0; i<5; i++)
  85.  
  86. SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)pVarName[i]);
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93. /* This function is called by the Windows function DispatchMessage() */
  94.  
  95. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  96. {
  97. static HWND hwndList, hwndText;
  98. int iIndex, iLength, cxChar, cyChar;
  99. TCHAR pVarName[30];
  100.  
  101.  
  102.  
  103. switch (message) /* handle the messages */
  104. {
  105.  
  106. case WM_CREATE:
  107.  
  108. cxChar = LOWORD(GetDialogBaseUnits());
  109. cyChar = HIWORD(GetDialogBaseUnits());
  110.  
  111. //Create Display Window
  112. hwndText = CreateWindow(TEXT("static"),NULL, WS_CHILD | WS_VISIBLE |
  113. SS_LEFT, cxChar, cyChar,
  114. GetSystemMetrics(SM_CXSCREEN), cyChar,
  115. hwnd, (HMENU)ID_TEXT,
  116. (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
  117. NULL);
  118.  
  119. //Create ComboBox
  120. hwndList = CreateWindow(TEXT("Combobox"), NULL, WS_CHILD | WS_VISIBLE |
  121. LBS_STANDARD, cxChar, cyChar*3,
  122. cxChar*16 + GetSystemMetrics(SM_CXVSCROLL),
  123. cyChar*5, hwnd, (HMENU)ID_LIST,
  124. (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
  125. NULL);
  126.  
  127. //Populate ComboBox
  128. FillListBox(hwndList);
  129.  
  130. //Set a Default Selection
  131. SendMessage(hwndList, CB_SETCURSEL, 0, 0);
  132. GetWindowText(hwndList, pVarName, 30);
  133. SetWindowText(hwndText, pVarName);
  134.  
  135. return 0;
  136.  
  137. case WM_COMMAND:
  138.  
  139. if( LOWORD(wParam)==ID_LIST && HIWORD(wParam)==CBN_SELCHANGE)
  140. {
  141. //Get Selection from Combo Box
  142. GetWindowText(hwndList, pVarName, 30);
  143.  
  144. //Show Text in Display Window
  145. SetWindowText(hwndText, pVarName);
  146. }
  147.  
  148. return 0;
  149.  
  150. case WM_DESTROY:
  151.  
  152. PostQuitMessage (0); /* send a WM_QUIT to the message queue */
  153. break;
  154.  
  155. default:
  156. /* for messages that we don't deal with */
  157. return DefWindowProc (hwnd, message, wParam, lParam);
  158. }
  159.  
  160. return 0;
  161. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 64
Reputation: atrusmre is an unknown quantity at this point 
Solved Threads: 0
atrusmre's Avatar
atrusmre atrusmre is offline Offline
Junior Poster in Training

Re: Combo Box

 
0
  #5
Nov 8th, 2005
I was hopeing for something simpler. For example a x = y type answer. I tried setting a control varible (called m_cPeople) to my list. For example:
  1. char *names[2];
  2. names[0] = "Joe";
  3. names[1] = "Billy";
  4. names[2] = "Bob";
  5. m_cPeople = names;
However at this point I get a "CComboBox : 'operator =' is unavailble"
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 287
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 28
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz in Training

Re: Combo Box

 
0
  #6
Nov 8th, 2005
*names[2] will only have two elements..




*names[0] and *names[1]
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Combo Box

 
0
  #7
Nov 8th, 2005
nothing in MS-Windows programming is "simple". If you want "simple" then stay with console programs. The function FillListBox() that was previously posted looks simple to me -- only 3 program lines. The rest of that post is common code that is required by all MS-Windows program using win32 api functions.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 64
Reputation: atrusmre is an unknown quantity at this point 
Solved Threads: 0
atrusmre's Avatar
atrusmre atrusmre is offline Offline
Junior Poster in Training

Re: Combo Box

 
0
  #8
Nov 9th, 2005
After much searching, I found my answer. Turns out I was on the right track. I set a the Combo Box to have a varible in the Control category (of type CComboBox) called it m_cPeople. Assuming to previos code I put in, I filled it like so.
  1. for (int i = 0, i <= 2, i++)
  2. m_cPeople.AddString( (LPCTSTR)names[i]);
Simple, and effective. Thak you for helping me get on the right track guys.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Combo Box

 
0
  #9
Nov 9th, 2005
Again, be careful with array sizes.
Originally Posted by atrusmre
  1. for (int i = 0, i <= 2, i++)
  2. m_cPeople.AddString( (LPCTSTR)names[i]);
This loop executes 3 times and tries to write from three strings.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 64
Reputation: atrusmre is an unknown quantity at this point 
Solved Threads: 0
atrusmre's Avatar
atrusmre atrusmre is offline Offline
Junior Poster in Training

Re: Combo Box

 
0
  #10
Nov 10th, 2005
Ack, sorry, was a typo. And sorry for not mentioning it was a MFC application.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC