⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch07.htm

📁 Visual C++ 的学习资料 Visual C++ 的学习资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
	<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 edit
controls (discussed in Hour 6, &quot;Using Edit Controls&quot;):

<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( &quot;Riley&quot; );</TT>
</FONT></PRE>
<P>or</P>
<PRE><FONT COLOR="#0066FF"><TT>comboBox.InsertString( 0, &quot;Mitch&quot; );</TT>
</FONT></PRE>
<P>All positions in a combo box are numbered beginning with zero, just like list
boxes. 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> member
function, 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 already
several lines of code in the function; don't remove them. Just add the code from
Listing 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::OnInitDialog
function.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>// In OnInitDialog...</TT>
<TT>// TODO: Add extra initialization here</TT>
<TT>    m_comboList.AddString( &quot;Foo&quot; );</TT>
<TT>    m_comboList.AddString( &quot;Bar&quot; );</TT>
<TT>    m_comboList.AddString( &quot;Baz&quot; );</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 source
code 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::OnOK
function.</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( &quot;The current selection is %d&quot;, 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 displayed
on the static-text control <TT>IDC_RESULT</TT>.
<H4><FONT COLOR="#000077">TYPE: Listing 7.8. Source code added to the CComboListDlg::OnCloseupCombo
function.</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 = &quot;Closing after selecting &quot; + 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 = &quot;No choice selected&quot;;</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 = &quot;Closing after selecting &quot; + 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 = &quot;Adding &quot; + szChoice + &quot; to list&quot;;</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-&gt;SetWindowText( szResult );</TT>
<TT>}</TT>
</FONT></PRE>
<P>The <TT>CComboListDlg::OnCloseupCombo</TT> function collects the contents from
the edit control section of the combo box and the selected item from the list box
section of the combo box. If a selection has been made in the list box, the item's
string is retrieved and displayed. Otherwise, if a string was entered in the edit
control, it is displayed. The string is not currently in the list box; it is added
to 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 the
edit control. When the <TT>CBN_EDITUPDATE</TT> message is received, the contents
of 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::OnEditupdateCombo
function.</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 = &quot;Choice changed to &quot; + szChoice;</TT>

<TT>    CWnd* pWnd = GetDlgItem( IDC_RESULT );</TT>
<TT>    ASSERT( pWnd );</TT>
<TT>    if( pWnd )</TT>
<TT>        pWnd-&gt;SetWindowText( szResult );</TT>
<TT>}</TT>
</FONT></PRE>
<P>Compile and run the ComboList project. Experiment by adding new entries to the
combo box and by expanding and closing the combo box. Other messages sent to the
combo box can be trapped and displayed just as <TT>CBN_EDITUPDATE</TT> was handled
in 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 are
used 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&amp;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, &quot;List View Controls.&quot;<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 what
you've learned, and begin thinking ahead to putting your knowledge into practice.
The answers to the quiz are in Appendix B, &quot;Quiz Answers.&quot;
<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"><IMG
SRC="../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>&#169; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>


</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -