?? ch07.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <TITLE>Teach Yourself Visual C++® 5 in 24 Hours -- Hour 7 -- Using List Box and Combo Box Controls</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR><FONT COLOR="#000077">Teach Yourself Visual C++® 5 in 24 Hours</FONT></H1></CENTER><CENTER><P><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> <HR></CENTER><CENTER><H1><FONT COLOR="#000077">- Hour 7 -<BR>Using List Box and Combo Box Controls</FONT></H1></CENTER><P>List boxes and combo boxes are two types of controls that are used often in Windowsprogramming. The list box often is used to enable a user to select from a large numberof possible choices, whereas the combo box is a combination of the list box and editcontrols. In this hour, you will learn about these controls and use them in simpleexamples.<H2><FONT COLOR="#000077"><B>Using Loops</B></FONT></H2><P>In Hour 5, "Button Controls," you learned about using conditional expressionsto control the flow of execution in C++ programs. Another way to control the flowof execution in your program is to execute sequences, also known as loops or iterations.Popular uses for loops include waiting for user input, printing a certain numberof reports, or reading input from a file until an End Of File (EOF) mark is detected.Three different loop statements are used in C++:<UL> <LI>The <TT>while</TT> loop <LI>The <TT>do-while</TT> loop <LI>The <TT>for</TT> loop</UL><H3><FONT COLOR="#000077"><B>Using the <TT>while</TT> Loop</B></FONT></H3><P>The <TT>while</TT> loop is used to execute a statement as long as a test expressionevaluates as <TT>true</TT>. Listing 7.1 shows an example of a <TT>while</TT> loop.<H4><FONT COLOR="#000077">TYPE: Listing 7.1. Executing a while loop 10 times.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>CString szMsg;</TT><TT>szMsg.Format("This is loop number %d", nLoopCounter);</TT><TT>int nLoopCounter = 0;</TT><TT>while(nLoopCounter < 10)</TT><TT>{</TT><TT> nLoopCounter++;</TT><TT> AfxMessageBox(szMsg);</TT><TT>}</TT><TT>AfxMessageBox("The loop is finished");</TT></FONT></PRE><P>In Listing 7.1, the compound statement following the <TT>while</TT> loop is executedas long as <TT>nLoopCounter</TT> is less than 10. When <TT>nLoopCounter</TT> is equalto 10, the condition tested by <TT>while</TT> becomes <TT>false</TT>, and the nextstatement following the block controlled by <TT>while</TT> is executed. In this example,a compound statement is executed; however, a single statement can also be executed.</P><P>Compound statements were discussed in Hour 5. A compound statement is a groupof statements that are enclosed within curly braces.<H3><FONT COLOR="#000077"><B>Using a <TT>do-while</TT> Loop</B></FONT></H3><P>A relative of the <TT>while</TT> loop is the <TT>do-while</TT> loop. The <TT>do-while</TT>loop is used when a statement or series of statements must be executed at least once.Listing 7.2 is an example of a <TT>do-while</TT> loop used to check an input characterfor <TT>Q</TT>.<H4><FONT COLOR="#000077">TYPE: Listing 7.2. Using the do-while loop to test foruser input in a console mode program.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>#include <iostream></TT><TT>using namespace std;</TT><TT>int main()</TT><TT>{</TT><TT> char ch;</TT><TT> do{</TT><TT> cout << "\nPress `Q' to exit ->";</TT><TT> cin >> ch;</TT><TT> // Ignore input until a carriage return.</TT><TT> cin.ignore( 120, `\n');</TT><TT> }while( ch != `Q' );</TT><TT> cout << "Goodbye" << endl;</TT><TT> return 0;</TT><TT>}</TT></FONT></PRE><H3><FONT COLOR="#000077"><B>Using the <TT>for</TT> Loop</B></FONT></H3><P>The <TT>for</TT> loop is often used in C++ programs to write a very compact loopstatement. The <TT>for</TT> loop enables you to write loops in a more compact stylethan is possible using <TT>while</TT> loops. Listing 7.3 is equivalent to Listing7.1, except that it has been rewritten using the <TT>for</TT> loop.<H4><FONT COLOR="#000077">TYPE: Listing 7.3. Using a for loop to display a message10 times.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>CString szMsg;</TT><TT>szMsg.Format("This is loop number %d", nLoopCounter);</TT><TT>for(int nLoopCounter = 0; nLoopCounter < 10; nLoopCounter++)</TT><TT>{</TT><TT> AfxMessageBox(szMsg);</TT><TT>}</TT><TT>AfxMessageBox("The loop is finished");</TT></FONT></PRE><P>There are four components to every <TT>for</TT> statement:</P><PRE><FONT COLOR="#0066FF"><TT>for( <I>expression1</I>; <I>expression2</I>; <I>expression3</I> )</TT><TT> <I>statement1</I></TT></FONT></PRE><P>When the <TT>for</TT> loop begins, <I><TT>expression1</TT></I> is executed. Thisis usually where you declare loop counters. As long as <I><TT>expression2</TT></I>is true, the statement controlled by the loop is executed. After the controlled statement(<I><TT>statement1</TT></I>) has been performed, <I><TT>expression3</TT></I> is executed.Loop counters are usually incremented in <I><TT>expression3</TT></I>.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>In the example provided in Listing 7.3, the expression <TT>nLoopCounter++</TT> was used as a way to increment the value of <TT>nLoopCounter</TT> by one. To decrement the value, you can use <TT>nLoopCounter--</TT>. <HR></P> <P><HR><B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>As a rule of thumb, if the loop is executed a fixed number of times, it's usually easier to use <TT>for</TT> instead of <TT>while</TT>. However, if you are waiting for an event to occur, or if the number of loops isn't easily predicted, it's better to use <TT>while</TT>. <HR></BLOCKQUOTE><H2><FONT COLOR="#000077"><B>What Are List Boxes?</B></FONT></H2><P><FONT COLOR="#000077"><I><B>New Term:</B></I></FONT><I><B> </B>List box</I> controlsare used to contain a list of items available for selection. The user can selectitems by using the keyboard or by clicking an individual item using a mouse.</P><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>single selection listbox</I> enables one item to be selected at a time. This is the default style fora list box.</P><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>multiple selection listbox </I>enables multiple items to be selected at one time.</P><P>A list box normally is found in a dialog box, control bar, or other window thatcontains controls. List boxes often are used to contain a large number of items.If some items cannot be displayed, a scrollbar is displayed to help the user navigatethrough the list.<H3><FONT COLOR="#000077"><B>Why Use a List Box?</B></FONT></H3><P>List boxes are the simplest control that enables an arbitrary number of itemsto be displayed to a user. List boxes are often used to display lists of informationthat are extracted from databases or reports. Because the list box doesn't have tobe sized, it is well-suited for this type of data. When a sorted list box is used,it's easy for a user to search through a large number of text items and make a selection.</P><P>List boxes are also extremely easy to program. If you have created a list boxobject, you can add an item to the list box with just one line of code, like this:</P><PRE><FONT COLOR="#0066FF"><TT>listBox.AddString("Gwen");</TT></FONT></PRE><P>No other control is as flexible and easy to use for both the programmer and theuser.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>The list box is also the first control you have seen that uses indexes. Whenever an item is selected, inserted, or deleted, a zero-based index is used to identify the item. This index can be synchronized with a database index or used to identify the item in other ways. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>MFC Support for List Boxes</B></FONT></H3><P>You normally add list boxes to a dialog box resource just as you added buttonsand edit controls in the previous two hours. After you have added the control, useClassWizard to add message-handling functions and associate the control with a <TT>CListBox</TT>object.</P><P>You can use the MFC <TT>CListBox</TT> class to manage and interact with the listbox control. Like other control classes, <TT>CListBox</TT> is derived from <TT>CWnd</TT>,and most <TT>CWnd</TT> functions can be used with <TT>CListBox</TT> objects. Youwill see more details about the <TT>CListBox</TT> later, in the section, "Usingthe <TT>CListBox</TT> Class."<H3><FONT COLOR="#000077"><B>Adding a List Box to a Dialog Box</B></FONT></H3><P>For demonstration purposes, create a dialog box-based project named ListBox usingAppWizard, following the steps presented in Hour 4, "Dialog Boxes and C++ Classes."Click the ResourceView tab in the project workspace. Open the dialog box editor bydouble-clicking the <TT>IDD_LISTBOX_DIALOG</TT> icon in the Dialog resource folder.</P><P>Adding a list box to <TT>IDD_LISTBOX_DIALOG</TT>, the main dialog box, is justlike adding a button or edit control. Either drag and drop a list box control fromthe control palette to the main dialog box, or select the list box control on thetool palette using the mouse, and click the desired position in the main dialog box.Figure 7.1 shows the <TT>IDD_LISTBOX_DIALOG</TT> with a list box control.</P><P><A NAME="01"></A><A HREF="01.htm"><B>Figure 7.1.</B></A> <I><BR>The main dialog box used in the ListBox sample program.</I></P><P>Open the Properties dialog box for the list box by right-clicking the controland selecting Properties from the shortcut menu. Change the resource ID to <TT>IDC_LIST</TT>.Set all other properties to their default values.<H2><FONT COLOR="#000077"><B>List Box Properties</B></FONT></H2><P>Just like other controls, list boxes have properties that you can configure usingthe Developer Studio resource editor. Some of these properties are available in othercontrols, and some are unique to list boxes. These properties are available for alist box control:<UL> <LI>ID: Used for the list box resource ID. Developer Studio supplies a default resource ID, such as <TT>IDC_LIST</TT>.<BR> <BR> <LI>Visible: Indicates that the list is initially visible. This check box is normally checked.<BR> <BR> <LI>Disabled: Indicates the list should be initially disabled. This check box is normally cleared.<BR> <BR> <LI>Group: Marks the first control in a group. This check box is normally cleared.<BR> <BR> <LI>Tab Stop: Indicates that this control can be reached by pressing the Tab key. This check box is normally checked.<BR> <BR> <LI>Help ID: Creates a context-sensitive help ID for this control.<BR> <BR> <LI>Selection: Determines how items in a list box can be selected. A single-selection list box enables one item to be selected at any given time. Multiple-selection list boxes enable several selections at once, but ignore the Shift and Control keys. Extended selection list boxes use the Shift and Control keys during selection.<BR> <BR> <LI>Owner Draw: Indicates that the button will be drawn by the button's owner, in this case the dialog box. In most cases, this option is set to no.<BR> <BR> <LI>Has Strings: Specifies that an owner-drawn list box contains strings. All other list boxes contain strings by default.<BR> <BR> <LI>Border: Specifies a border for the list box. This option is enabled by default.<BR> <BR> <LI>Sort: Indicates that the list box contents should be sorted. This option is normally selected.<BR> <BR> <LI>Notify: Indicates that notification messages should be sent to the dialog box. This option is normally selected.<BR> <BR> <LI>Multi-Column: Creates a multicolumn list box. This option is normally cleared.<BR> <BR> <LI>Horizontal Scroll: Creates a list box with a horizontal scrollbar. This option is normally cleared.<BR> <BR> <LI>Vertical Scroll: Creates a list box with a vertical scrollbar. This option is normally selected.<BR> <BR> <LI>No Redraw: Indicates that the list box should not update its appearance when its contents are changed. This option is rarely selected and is cleared by default.<BR> <BR> <LI>Use Tabstops: Specifies that text items displayed in the list box can contain tabs. This option is normally cleared.<BR>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -