?? ch06.htm
字號(hào):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <TITLE>Teach Yourself Visual C++® 5 in 24 Hours -- Hour 6 -- Using Edit 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="../ch05/ch05.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch07/ch07.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 6 -<BR>Using Edit Controls</FONT></H1></CENTER><P>In Windows programs, user input is often collected using edit controls. In thishour you will also learn about<UL> <LI>Identifier scope and lifetime, an important topic for C++ programming<BR> <BR> <LI>Using edit controls to collect and display free-form text supplied by the user<BR> <BR> <LI>Associating an edit control with <TT>CEdit</TT> and <TT>CString</TT> objects using ClassWizard<BR> <BR> <LI>Using DDV and DDX routines for data validation and verification</UL><P>You will also create an SDI project and use it to show how data is transferredin and out of edit controls used in dialog boxes.<H2><FONT COLOR="#000077"><B>Identifier Scope and Lifetime</B></FONT></H2><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>The <I>scope</I> of an identifierrefers to its visibility in a C++ program.</P><P>Every identifier used to name a variable or function has a specific scope whenit is created, and this scope determines how and where that name can be used. Ifa variable is "in scope" at a certain point in a program, it is visibleand can be used in most circumstances. If it is "out of scope," it is notvisible, and your program will not be capable of using that variable.</P><P>One simple type of scope is shown in the following code sample. The followingcode is not legal because the variable <TT>myAge</TT> is used before it is declared:</P><PRE><FONT COLOR="#0066FF"><TT>myAge = 12;</TT><TT>int myAge;</TT></FONT></PRE><P>Because the identifier <TT>myAge</TT> is not in the current scope, it cannot beassigned a value.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>The preceding example illustrates one simple property about visibility: it almost always runs "downward," beginning at the point where the variable is declared. There are also several different types of scope, ranging from very small to very large. <HR></P> <P><HR><B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>In general, your program should use variables that have as small a scope as possible. The smaller the scope, the less chance that an identifier will be accidentally misused or subjected to side effects. For example, passing objects as parameters to a function is always better than relying on global variables. Using variables that are local to the function helps make the function more reusable. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>Using Different Types of Scope</B></FONT></H3><P>The scope of an identifier comes into play whenever an identifier is declared.The most commonly used types of scope available in a C++ program are<UL> <LI>Local scope <LI>Function scope <LI>Class scope</UL><P>Each of these types of scope is discussed in the following sections.<H4><FONT COLOR="#000077">Local Scope</FONT></H4><P>The simplest example of local scope is a variable or other object that is declaredoutside any functions, like this:</P><PRE><FONT COLOR="#0066FF"><TT>int foo;</TT><TT>int main()</TT><TT>{</TT><TT> return 0;</TT><TT>}</TT></FONT></PRE><P>In this example, the variable <TT>foo</TT> is in scope from the point of its declarationto the end of the source file. For this reason, this type of local scope is sometimescalled <I>file scope</I>. All declarations that occur outside class or function definitionshave this type of scope.</P><P>Variables declared inside a function body have local scope and are visible onlywithin the function.</P><P>Another type of local scope is <I>block scope</I>, where a variable within a compoundstatement or other block is visible until the end of the block, as shown in Listing6.1.<H4><FONT COLOR="#000077">TYPE: Listing 6.1. An example of local block scope.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>if( bPrinting == true)</TT><TT>{</TT><TT> int nMyAge = 42;</TT><TT> cout << "My age is " << nMyAge << endl;</TT><TT>}</TT><TT>// nMyAge is not in scope here.</TT></FONT></PRE><P>The variable <TT>nMyAge</TT> has block scope and can be used only between thecurly braces.<H4><FONT COLOR="#000077">Function Scope</FONT></H4><P>Function scope is rarely an issue. <I>Function scope</I> applies to labels declaredinside a function definition. The only time you would use a label is with the widelydiscouraged <TT>goto</TT> statement.</P><P>None of the labels declared in a function are visible outside the function. Thismeans that the C++ language does not directly support jumping to a label outsidethe current function. It also means that you can reuse labels in different functions.<H4><FONT COLOR="#000077">Class Scope</FONT></H4><P>All identifiers used in a class, union, or structure are tightly associated withthe class and have <I>class scope</I>. An identifier with class scope can be usedanywhere within the class, union, or structure.</P><P>If a class or variable name is used to qualify access to the identifier, it alsois visible outside the class. For example, if a class is defined as follows, thevariables <TT>m_myVar</TT> and <TT>m_myStaticVar</TT> are in scope for all the <TT>CFoo</TT>member functions:</P><PRE><FONT COLOR="#0066FF"><TT>// class CFoo</TT><TT>class CFoo</TT><TT>{</TT><TT>public:</TT><TT> CFoo();</TT><TT> int GetMyVar();</TT><TT> int GetStaticVar();</TT><TT> int m_myVar;</TT><TT> static int m_myStaticVar;</TT><TT>};</TT><TT>int CFoo::m_myStaticVar;</TT></FONT></PRE><P>Outside the <TT>CFoo</TT> class, the variables can be accessed only through a<TT>CFoo</TT> object, like this:</P><PRE><FONT COLOR="#0066FF"><TT>aFoo.m_myVar = 42;</TT></FONT></PRE><P>There is one exception to the rule that requires a member to be accessed witha variable name: A class member declared as <I>static</I> is shared by all objectsof that class. Static members of a class exist even when no objects of a class havebeen created. To access a static class member without using a class object, prefixthe class name to the member name, like this:</P><PRE><FONT COLOR="#0066FF"><TT>CFoo::m_myStaticVar = 1;</TT></FONT></PRE><H3><FONT COLOR="#000077"><B>Understanding Identifier Lifetime</B></FONT></H3><P>In a C++ program, every variable or object has a specific lifetime, which is separatefrom its visibility. It is possible for you to determine when a variable is createdand when it is destroyed.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Lifetime can be an important issue when you design your program. Large objects can be costly to create and destroy. By understanding the lifetime of objects created in your programs, you can take advantage of features in the C++ language that help your programs run more efficiently. <HR></BLOCKQUOTE><H4><FONT COLOR="#000077">Static Lifetime</FONT></H4><P>A variable declared as <TT>static</TT> in a function is created when the programstarts and is not destroyed until the program ends. This is useful when you wantthe variable or object to remember its value between function calls. Listing 6.2is an example of a static object in a function.<H4><FONT COLOR="#000077">TYPE: Listing 6.2. A static object in a function, destroyedwhen the program ends.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>#include <iostream></TT><TT>using namespace std;</TT><TT>void PrintMessage();</TT><TT>int main()</TT><TT>{</TT><TT> for( int nMessage = 0; nMessage < 10; nMessage++ )</TT><TT> PrintMessage();</TT><TT> return 0;</TT><TT>}</TT><TT>void PrintMessage()</TT><TT>{</TT><TT> static int nLines = 1;</TT><TT> cout << "I have printed " << nLines << " lines." << endl;</TT><TT> nLines++;</TT><TT>}</TT></FONT></PRE><H2><FONT COLOR="#000077"><B>Understanding Edit Controls</B></FONT></H2><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>An <I>edit control</I> isa window used to store free-form text input by a user.</P><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>single-line edit control</I>is an edit control that enables a single line of text to be entered.</P><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>multiple-line edit control</I>,sometimes called an <I>MLE</I>, is an edit control that enables multiple lines oftext to be entered.</P><P>Edit controls are usually found in dialog boxes. Almost anywhere user input isrequired, you can usually find an edit control.<H3><FONT COLOR="#000077"><B>Why Use an Edit Control?</B></FONT></H3><P>Single-line edit controls are used when text must be collected. For example, whena name or address must be entered in a dialog box, an edit control is used to collectthat information. Multiline edit controls often use scrollbars that enable more textto be entered than can be displayed.</P><P>A prompt in the form of default text can be provided for an edit control. In somesituations, this can reduce the amount of typing required by a user. All edit controlsalso support a limited amount of editing, without any need for extra programmingon your part. For example, the standard cut-and-paste commands work as expected inan edit control. Table 6.1 lists the editing commands available in an edit control.<H4><FONT COLOR="#000077">Table 6.1. Editing commands available in an edit control.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT"><B>Command</B></TD> <TD ALIGN="LEFT"><B>Keystroke</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT">Cut</TD> <TD ALIGN="LEFT">Control+X</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT">Paste</TD> <TD ALIGN="LEFT">Control+V</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT">Copy</TD> <TD ALIGN="LEFT">Control+C</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT">Undo</TD> <TD ALIGN="LEFT">Control+Z</TD> </TR></TABLE><BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Because of the built-in editing capabilities of the edit control, it's possible to create a simple text editor using a multiple-line edit control. Although an MLE cannot replace a real text editor, it does provide a simple way to collect multiple lines of text from a user. <HR></BLOCKQUOTE>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -