?? ch07.htm
字號:
<LI>Disabled <LI>Group <LI>Tab Stop <LI>Owner Draw <LI>Has Strings <LI>Sort <LI>Vertical Scroll <LI>No Integral Height <LI>Help ID <LI>Disable No Scroll</UL><P>The following combo box properties are identical to properties offered for editcontrols (discussed in Hour 6, "Using Edit Controls"):<UL> <LI>Auto HScroll <LI>OEM Convert</UL><P>These two properties are unique to combo box controls:<UL> <LI>List Choices, used to list items that appear by default when the dialog box is created. Press Ctrl+Enter after each entry.<BR> <BR> <LI>Type, used to specify the type of the combo box. You can choose between Simple, Dropdown, and Drop List. Dropdown is the default choice.</UL><H4><FONT COLOR="#000077">Adding Items to a Combo Box</FONT></H4><P>You add strings to combo boxes just as you add them to list boxes. Just like <TT>CListBox</TT>,the <TT>CComboBox</TT> class contains <TT>AddString</TT> and <TT>InsertString</TT>member functions:</P><PRE><FONT COLOR="#0066FF"><TT>comboBox.AddString( "Riley" );</TT></FONT></PRE><P>or</P><PRE><FONT COLOR="#0066FF"><TT>comboBox.InsertString( 0, "Mitch" );</TT></FONT></PRE><P>All positions in a combo box are numbered beginning with zero, just like listboxes. However, if an error occurs, <TT>CB_ERR</TT> is returned instead of <TT>LB_ERR</TT>.If an item cannot be added due to insufficient space, <TT>CB_ERRSPACE</TT> is returned.</P><P>To determine the number of items currently in a combo box, <TT>CComboBox</TT>includes the <TT>GetCount</TT> member function:</P><PRE><FONT COLOR="#0066FF"><TT>nItems = comboBox.GetCount();</TT></FONT></PRE><P>Remember, <TT>CB_ERR</TT> is returned instead of <TT>LB_ERR</TT> when using a<TT>CComboBox</TT> object.<H4><FONT COLOR="#000077">Collecting Input from a Combo Box</FONT></H4><P>You can collect input from a combo box by using the <TT>GetWindowText</TT> memberfunction, just like an edit control. For simple combo boxes and drop-down combo boxes,this is the easiest way to get the current selection. You can also use the <TT>GetCurSel</TT>member function to determine the current selection position from the list box.<H3><FONT COLOR="#000077"><B>A Combo Box Example</B></FONT></H3><P>To create a sample project using a combo box and the <TT>CComboBox</TT> class,follow these steps:<DL> <DD>1. Create a dialog box-based project named <TT>ComboList</TT> using AppWizard, as described in previous examples.<BR> <BR> 2. Add a drop-down combo list to the <TT>IDD_COMBOLIST_DIALOG</TT> resource, as you did for the list box earlier in this hour.<BR> <BR> 3. Give the combo box the resource ID <TT>IDC_COMBO</TT>. Use the default values for all other properties.<BR> <BR> 4. Add a static-text control to the dialog box, and give it the resource ID <TT>IDC_RESULT</TT>. This text control will be used to display information about messages received from the combo box.<BR> <BR> 5. Using ClassWizard, add a member variable to the <TT>CComboListDlg</TT> class named <TT>m_comboList</TT>. Set the Category to Control.<BR> <BR> 6. Using ClassWizard, add a message-handling function for the IDOK control <TT>BN_CLICKED</TT> message to the <TT>CComboListDlg</TT> class.<BR> <BR> 7. Using ClassWizard, add message-handling functions for <TT>IDC_COMBO</TT> control messages to the <TT>CComboListDlg</TT> class. Add functions to handle <TT>CBN_CLOSEUP</TT> and <TT>CBN_EDITUPDATE</TT> messages.</DL><H4><FONT COLOR="#000077">Adding Strings to a Combo Box</FONT></H4><P>After completing these steps, add the source code in Listing 7.6 to the <TT>CComboListDlg::OnInitDialog</TT>member function. This code adds three entries to the combo box. There are alreadyseveral lines of code in the function; don't remove them. Just add the code fromListing 7.6 after the <TT>//TODO</TT> comment provided by AppWizard.<H4><FONT COLOR="#000077">TYPE: Listing 7.6. Source code added to the CComboListDlg::OnInitDialogfunction.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>// In OnInitDialog...</TT><TT>// TODO: Add extra initialization here</TT><TT> m_comboList.AddString( "Foo" );</TT><TT> m_comboList.AddString( "Bar" );</TT><TT> m_comboList.AddString( "Baz" );</TT></FONT></PRE><H4><FONT COLOR="#000077">Getting the Current Combo Box Selection</FONT></H4><P>Edit the <TT>CComboListDlg::OnOK</TT> member function so it looks like the sourcecode provided in Listing 7.7. This code uses member functions from the <TT>CComboBox</TT>class to display information about the current combo box selection.<H4><FONT COLOR="#000077">TYPE: Listing 7.7. Source code added to the CComboListDlg::OnOKfunction.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>void CComboListDlg::OnOK()</TT><TT>{</TT><TT> CString szCombo;</TT><TT> m_comboList.GetWindowText( szCombo );</TT><TT> AfxMessageBox( szCombo );</TT><TT> int nChoice = m_comboList.GetCurSel();</TT><TT> szCombo.Format( "The current selection is %d", nChoice );</TT><TT> AfxMessageBox( szCombo );</TT><TT> CDialog::OnOK();</TT><TT>}</TT></FONT></PRE><H4><FONT COLOR="#000077">Detecting Combo Box Events</FONT></H4><P>Add the source code provided in Listing 7.8 to the <TT>CComboListDlg::OnCloseupCombo</TT>function. When the <TT>CBN_CLOSEUP</TT> message is received, a message is displayedon the static-text control <TT>IDC_RESULT</TT>.<H4><FONT COLOR="#000077">TYPE: Listing 7.8. Source code added to the CComboListDlg::OnCloseupCombofunction.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>void CComboListDlg::OnCloseupCombo()</TT><TT>{</TT><TT> CString szChoice;</TT><TT> CString szResult;</TT><TT> int nChoice;</TT><TT> // Get current selections from edit and list-box controls</TT><TT> m_comboList.GetWindowText( szChoice );</TT><TT> nChoice = m_comboList.GetCurSel();</TT><TT> if( nChoice != CB_ERR )</TT><TT> {</TT><TT> // If a valid choice was made from the list box, fetch</TT><TT> // the item's text string.</TT><TT> m_comboList.GetLBText( nChoice, szChoice );</TT><TT> szResult = "Closing after selecting " + szChoice;</TT><TT> }</TT><TT> else if( szChoice.IsEmpty() == TRUE )</TT><TT> {</TT><TT> // No choice was made from the list box, and the edit</TT><TT> // control was empty.</TT><TT> szResult = "No choice selected";</TT><TT> }</TT><TT> else if( m_comboList.FindStringExact(-1, szChoice) != CB_ERR )</TT><TT> {</TT><TT> // The string from the edit control was found in the</TT><TT> // list box.</TT><TT> szResult = "Closing after selecting " + szChoice;</TT><TT> }</TT><TT> else</TT><TT> {</TT><TT> // The edit control contains a new string, not currently</TT><TT> // in the list box. Add the string.</TT><TT> m_comboList.AddString( szChoice );</TT><TT> szResult = "Adding " + szChoice + " to list";</TT><TT> }</TT><TT> // Get a pointer to the static-text control, and display an</TT><TT> // appropriate result message.</TT><TT> CWnd* pWnd = GetDlgItem( IDC_RESULT );</TT><TT> ASSERT( pWnd );</TT><TT> if( pWnd )</TT><TT> pWnd->SetWindowText( szResult );</TT><TT>}</TT></FONT></PRE><P>The <TT>CComboListDlg::OnCloseupCombo</TT> function collects the contents fromthe edit control section of the combo box and the selected item from the list boxsection of the combo box. If a selection has been made in the list box, the item'sstring is retrieved and displayed. Otherwise, if a string was entered in the editcontrol, it is displayed. The string is not currently in the list box; it is addedto it.</P><P>Add the source code provided in Listing 7.9 to the <TT>CComboListDlg::OnEditupdateCombo</TT>member function. <TT>CBN_EDITUPDATE</TT> is received when the user types inside theedit control. When the <TT>CBN_EDITUPDATE</TT> message is received, the contentsof the edit control are displayed on the <TT>IDC_RESULT</TT> text control.<H4><FONT COLOR="#000077">TYPE: Listing 7.9. Source code added to the CComboListDlg::OnEditupdateCombofunction.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>void CComboListDlg::OnEditupdateCombo()</TT><TT>{</TT><TT> CString szChoice;</TT><TT> CString szResult;</TT><TT> m_comboList.GetWindowText( szChoice );</TT><TT> szResult = "Choice changed to " + szChoice;</TT><TT> CWnd* pWnd = GetDlgItem( IDC_RESULT );</TT><TT> ASSERT( pWnd );</TT><TT> if( pWnd )</TT><TT> pWnd->SetWindowText( szResult );</TT><TT>}</TT></FONT></PRE><P>Compile and run the ComboList project. Experiment by adding new entries to thecombo box and by expanding and closing the combo box. Other messages sent to thecombo box can be trapped and displayed just as <TT>CBN_EDITUPDATE</TT> was handledin Listing 7.9.<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2><P>In this hour, you learned about list box and combo box controls and how they areused in Windows programs. You also learned how to associate these controls with <TT>CListBox</TT>and <TT>CComboBox</TT> objects.<H2><FONT COLOR="#000077"><B>Q&A</B></FONT></H2><DL> <DD><B>Q What is the easiest way to create a list box that has a bitmap image next to each item?</B><BR> <BR> <B>A</B> The only way to display a bitmap in a list box is to create an owner-drawn list box, where you take responsibility for drawing each item in the list box. You can easily achieve a similar effect by using a list view control, which is discussed in Hour 18, "List View Controls."<BR> <BR> <B>Q When should I use a combo box drop list, and when is a list box more appropriate?</B><BR> <BR> <B>A</B> A drop list is appropriate when space on your dialog box is at a premium. A list box is more appropriate when the user must see more than one item without clicking on the control.</DL><H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2><P>The Workshop is designed to help you anticipate possible questions, review whatyou've learned, and begin thinking ahead to putting your knowledge into practice.The answers to the quiz are in Appendix B, "Quiz Answers."<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3><DL> <DD>1. Which MFC class is used to manage list box controls?<BR> <BR> 2. What message is sent to your dialog box when a user double-clicks a dialog box?<BR> <BR> 3. What functions are used to add items to a list box control?<BR> <BR> 4. What function is used to retrieve the number of items in a list box control?<BR> <BR> 5. What function is used to retrieve the currently selected index in a list box?<BR> <BR> 6. What are the three styles used for list box controls?<BR> <BR> 7. What are the three types of loops used in C++ programs?<BR> <BR> 8. Which MFC class is used to manage combo boxes?<BR> <BR> 9. What function is used to add an item to a combo box at a specific index?<BR> <BR> 10. What are the three styles used for combo boxes?</DL><H3><FONT COLOR="#000077"><B>Exercise</B></FONT></H3><DL> <DD>1. Modify the ListBox project by adding a new button labeled Loop. When a user clicks the Loop button, display each item in the list box in a message box, one item at a time.<FONT COLOR="#000077"></FONT></DL><CENTER><P><HR><A HREF="../ch06/ch06.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch08/ch08.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR><BR><BR><IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"BORDER="0"></P><P>© <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -