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

📄 ch06.htm

📁 Visual C++ 的学习资料 Visual C++ 的学习资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
	
	<TITLE>Teach Yourself Visual C++&#174; 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++&#174; 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"><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> 
<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 this
hour 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 transferred
in 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 identifier
refers to its visibility in a C++ program.</P>
<P>Every identifier used to name a variable or function has a specific scope when
it is created, and this scope determines how and where that name can be used. If
a variable is &quot;in scope&quot; at a certain point in a program, it is visible
and can be used in most circumstances. If it is &quot;out of scope,&quot; it is not
visible, 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 following
code 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 be
assigned 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 &quot;downward,&quot;
	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 declared
outside 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 declaration
to the end of the source file. For this reason, this type of local scope is sometimes
called <I>file scope</I>. All declarations that occur outside class or function definitions
have this type of scope.</P>
<P>Variables declared inside a function body have local scope and are visible only
within the function.</P>
<P>Another type of local scope is <I>block scope</I>, where a variable within a compound
statement or other block is visible until the end of the block, as shown in Listing
6.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 &lt;&lt; &quot;My age is &quot; &lt;&lt; nMyAge &lt;&lt; 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 the
curly braces.
<H4><FONT COLOR="#000077">Function Scope</FONT></H4>
<P>Function scope is rarely an issue. <I>Function scope</I> applies to labels declared
inside a function definition. The only time you would use a label is with the widely
discouraged <TT>goto</TT> statement.</P>
<P>None of the labels declared in a function are visible outside the function. This
means that the C++ language does not directly support jumping to a label outside
the 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 with
the class and have <I>class scope</I>. An identifier with class scope can be used
anywhere within the class, union, or structure.</P>
<P>If a class or variable name is used to qualify access to the identifier, it also
is visible outside the class. For example, if a class is defined as follows, the
variables <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 with
a variable name: A class member declared as <I>static</I> is shared by all objects
of that class. Static members of a class exist even when no objects of a class have
been created. To access a static class member without using a class object, prefix
the 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 separate
from its visibility. It is possible for you to determine when a variable is created
and 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 program
starts and is not destroyed until the program ends. This is useful when you want
the variable or object to remember its value between function calls. Listing 6.2
is an example of a static object in a function.
<H4><FONT COLOR="#000077">TYPE: Listing 6.2. A static object in a function, destroyed
when the program ends.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>#include &lt;iostream&gt;</TT>
<TT>using namespace std;</TT>

<TT>void PrintMessage();</TT>
<TT>int main()</TT>
<TT>{</TT>
<TT>    for( int nMessage = 0; nMessage &lt; 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 &lt;&lt; &quot;I have printed &quot; &lt;&lt; nLines &lt;&lt; &quot; lines.&quot; &lt;&lt; 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> is
a 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 of
text to be entered.</P>
<P>Edit controls are usually found in dialog boxes. Almost anywhere user input is
required, 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, when
a name or address must be entered in a dialog box, an edit control is used to collect
that information. Multiline edit controls often use scrollbars that enable more text
to be entered than can be displayed.</P>
<P>A prompt in the form of default text can be provided for an edit control. In some
situations, this can reduce the amount of typing required by a user. All edit controls
also support a limited amount of editing, without any need for extra programming
on your part. For example, the standard cut-and-paste commands work as expected in
an 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>

⌨️ 快捷键说明

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