944,181 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 26817
  • C++ RSS
Oct 29th, 2005
0

Combo Box

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atrusmre is offline Offline
64 posts
since Mar 2005
Oct 29th, 2005
0

Re: Combo Box

Should be very similar to adding items to a listbox. Check out code snippet:
http://www.daniweb.com/code/snippet79.html
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 31st, 2005
0

Re: Combo Box

Got anything a little easier?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atrusmre is offline Offline
64 posts
since Mar 2005
Oct 31st, 2005
1

Re: Combo Box

Quote originally posted by atrusmre ...
Got anything a little easier?


How about this:

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Nov 8th, 2005
0

Re: Combo Box

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:
C++ Syntax (Toggle Plain Text)
  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"
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atrusmre is offline Offline
64 posts
since Mar 2005
Nov 8th, 2005
0

Re: Combo Box

*names[2] will only have two elements..




*names[0] and *names[1]
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Nov 8th, 2005
0

Re: Combo Box

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 9th, 2005
0

Re: Combo Box

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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atrusmre is offline Offline
64 posts
since Mar 2005
Nov 9th, 2005
0

Re: Combo Box

Again, be careful with array sizes.
Quote originally posted by atrusmre ...
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 10th, 2005
0

Re: Combo Box

Ack, sorry, was a typo. And sorry for not mentioning it was a MFC application.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atrusmre is offline Offline
64 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Can someone tell me what I am doing wrong?
Next Thread in C++ Forum Timeline: 2-D Arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC