📄 book_begin072.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>VB教程:第七章(2)</title>
<style>
<!--
div.content {position:absolute;left:0;right;0}
#content {font-size:12px;line-height:18px}
a {text-decoration:none}
a:hover {text-decoration:underline;color:red;}
p {font-size:12px}
-->
</style>
</head>
<body link="#00003A" vlink="#00003A">
<div align="center"><center>
<table border="0" cellpadding="4" width="90%" bgcolor="#C8E2F0" cellspacing="0">
<tr>
<td width="20%"><p align="left" id="content"><span id="content"><a name="VB教程" href="../files/book.htm"><strong>VB教程</strong></a><font color="#0080C0">◤</font></span></td>
<td width="20%"><font color="#FF0000"><p align="left"></font><span id="content"><a href="book_begin07.htm"><strong>上一章</strong></a><font color="#0080C0">◥</font></span></td>
<td width="20%"><font color="#FF0000"><p align="left"></font><span id="content"><strong><font color="#000000">第七章(2)</font></strong><font color="#FF0000">★</font></span></td>
<td width="20%"><p class="content" id="content"><span id="content"><a href="book_begin08.htm"><strong>下一章</strong></a><font color="#0080C0">◢</font></span></td>
<td width="20%"><p id="content"><span id="content"><strong><a href="../index.htm" target="_parent">返回主页</a><font color="#0000FF">◎</font></strong></span></td>
</tr>
</table>
</center></div>
<p> </p>
<div align="center"><center>
<table border="0" cellpadding="3" cellspacing="0" width="80%" class="content" id="content">
<tr>
<td width="50%" style="background-color: rgb(255,255,255); font-family: 宋体; font-size: 10pt; font-weight: bold"><p align="center"><font size="3" color="#FF0000">输出(2)</font></p>
<p ALIGN="left" class="content" id="content"> </p>
<font FACE="宋体" SIZE="3"><p ALIGN="JUSTIFY" id="content"></font>很久没有继续VB教程了,也有很多初学VB的朋友来信留言要求继续,说起来惭愧,不是有时候犯懒,就是杂物缠身,今天好不容易做在电脑前想敲几个字,哪知打开VB5出现非法操作,卸载后重新安装非法提示依然出现,真是岂有此理!Windows系统用了很久了,看来冲突实在是太多,但这么久没有更新,实在说不过去,这次就算是一次纸上谈兵,带来不便之处还请大家包涵。</p>
<p ALIGN="JUSTIFY" id="content">上次为大家讲解了MsgBox的用法,这次我来讲讲其他的输出方法。</p>
<p ALIGN="JUSTIFY" id="content">在程序中我们如果想输出执行的结果最经常要用到什么对象,你可能会告诉我是文本框或标签控件,但事实往往不是这样,因为它们的显示方法单调,不能把各种字体的文字、图形结合在一起,因此最符合要求的控件应该是图片框(PictureBox),或直接就在Form本身上显示,其中要用到一个很重要的方法:“Print”,它的用法是:</p>
<p ALIGN="JUSTIFY" id="content">对象.Print Spc(n)/Tab(n);字符串表达式;……</p>
<p ALIGN="JUSTIFY" id="content">其中Spc(),表示打印字符之间的空格,n表示空格数目,Tab()表示绝对列数,n表示列数,也就是说你可以指定在那一列上打印,它们用“;”和字符串表达式分开,但也可以是这样:</p>
<p ALIGN="JUSTIFY" id="content">对象.Print 字符串表达式1,字符串表达式2,……</p>
<p ALIGN="JUSTIFY" id="content">用“,”隔开字符串,这样系统就会按默认的(14列)为一个输出位。每使用一次Print方法,程序就会在指定的对象上打印一行,在用Print则在下一行打印,当对象是窗体时,对象可以省略,例如打印一个表格:</p>
<p ALIGN="JUSTIFY" id="content">(注意:要把使用Print的对象的Autoredraw属性设为True,我们知道,当有多个窗口层叠时,哪个窗口得到焦点就会显示到最上面,当它显示时就是一种重绘的过程,Windows系统需要重新计算原来它的图形界面,这是一个非常复杂的过程,不过在VB中只需用一个Autoredraw属性就可以做到了,不信的话可以保持该属性为False,看看会怎样?)</p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Private Sub Form_Load()</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Print </font><font color="#008080">'空一行</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">FontUnderline = True</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">FontSize = 18</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">FontBold = True</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Print , "中国VB联盟成员列表"</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Print</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">FontUnderline = False</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">FontSize = 10</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Print Tab(3); "姓名";
Tab(15); "年龄"; Tab(25); "性别"; Tab(35); "个人简介"</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">FontBold = False</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Print Tab(3); "Mars";
Tab(15); "21"; Tab(25); "男"; Tab(35); "……"</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">……………</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">……………</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">End Sub</font></p>
<p ALIGN="JUSTIFY" id="content">更多的内容我将在下一章的用VB作图中向你说明。</p>
<p ALIGN="JUSTIFY" id="content"> </p>
<p ALIGN="JUSTIFY" id="content">当然,在平时用文本框或标签控件显示输出结果也是一种简单的方法,这些和我们以前讲到的大同小异,也就不再进行过多的介绍了,下面我来讲讲对字符串的格式化方法。</p>
<p ALIGN="JUSTIFY" id="content">这里要用到Format()函数,它可以对字符串按照指定的形式输出,也可以用做数字、日期的格式化,方法是:</p>
<p ALIGN="JUSTIFY" id="content">Format (表达式,[格式化表达式])</p>
<p ALIGN="JUSTIFY" id="content">其中格式化的表达式可以为:“0”,“#”,“.”,“,”,“:”,“$”,以及“()+-E+E-”等等表示,下面我将举一个小例子说明Format的所有功能(奇迹!奇迹!这时在打开VB5居然非法操作消失了!):</p>
<p ALIGN="JUSTIFY" id="content">请先建立一个项目文件,在Form1上添加两个框架控件(Frame)和若干按钮、文本框、无线钮和标签控件,具体如图所示:(图7-2-1):</p>
<p ALIGN="JUSTIFY" id="content"><img src="7-2-1.gif" alt="7-2-1.gif (5304 字节)" WIDTH="370" HEIGHT="335"></p>
<p ALIGN="JUSTIFY" id="content">其中有两排八个按钮为Command1,Index从0到7,又有七个无线钮Option1,Index从0到7,下面为了能够使用,请为它们修改Caption属性,如图7-2-2。</p>
<p ALIGN="JUSTIFY" id="content"><img src="7-2-2.gif" alt="7-2-2.gif (6380 字节)" WIDTH="370" HEIGHT="335"></p>
<p ALIGN="JUSTIFY" id="content">然后加入如下代码:</p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Dim FormatExp As String</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Dim ThisTime As Date</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Private Sub Command1_Click(Index As
Integer)</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">FormatExp = FormatExp +
Command1(Index).Caption</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Text1.Text = FormatExp</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">End Sub</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0"> </font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Private Sub Command2_Click()</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">a$ =
Format("1234567890.0987654321", FormatExp)</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Label1.Caption = "格式化后的数值:"
+ a$</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Text1.Text = ""</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">FormatExp = ""</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">End Sub</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0"> </font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Private Sub Form_Load()</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">ThisTime = Now</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Label2.Caption = ThisTime</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">End Sub</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0"> </font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Private Sub Option1_Click(Index As
Integer)</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">Text2.Text = Format(ThisTime,
Option1(Index).Caption)</font></p>
<p ALIGN="JUSTIFY" id="content"><font color="#0080C0">End Sub</font></p>
<p ALIGN="JUSTIFY" id="content">其中0和#代表数值显示的位数,不同的是如果定义格式化表达式的位数如果多于原表达式,则使用0则补0,使用#则省略0;“.”用来指定小数点的位置;其余字符用来分割数值,如“,”。</p>
<p ALIGN="JUSTIFY" id="content">结果如下图:7-2-3。</p>
<p ALIGN="JUSTIFY" id="content"><img src="7-2-3.gif" alt="7-2-3.gif (6339 字节)" WIDTH="370" HEIGHT="335"></p>
<p ALIGN="JUSTIFY" id="content">关于时间的格式化表达式的定义方法如下表:</p>
<table BORDER="1" CELLSPACING="1" CELLPADDING="7" WIDTH="487">
<tr>
<td WIDTH="19%" VALIGN="TOP" HEIGHT="19"></td>
<td WIDTH="27%" VALIGN="TOP" HEIGHT="19"><p ALIGN="JUSTIFY" id="content">格式符</td>
<td WIDTH="54%" VALIGN="TOP" HEIGHT="19"><p ALIGN="JUSTIFY" id="content">含义</td>
</tr>
<tr>
<td WIDTH="19%" VALIGN="TOP" ROWSPAN="5"><p ALIGN="JUSTIFY" id="content">日</td>
<td WIDTH="27%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">d</td>
<td WIDTH="54%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">显示不带0的日期(1-31)</td>
</tr>
<tr>
<td WIDTH="27%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">dd</td>
<td WIDTH="54%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">显示带0的日期(01-31)</td>
</tr>
<tr>
<td WIDTH="27%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">ddd</td>
<td WIDTH="54%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">以Sun-Sat格式显示日期</td>
</tr>
<tr>
<td WIDTH="27%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">dddd</td>
<td WIDTH="54%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">以Sunday-Saturday格式显示日期</td>
</tr>
<tr>
<td WIDTH="27%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">ddddd</td>
<td WIDTH="54%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">以年月日标准格式显示日期</td>
</tr>
<tr>
<td WIDTH="19%" VALIGN="TOP" ROWSPAN="4"><p ALIGN="JUSTIFY" id="content">月/分钟</td>
<td WIDTH="27%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">m</td>
<td WIDTH="54%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">显示不带0的月,如果后面紧跟h或hh则显示不带0的分</td>
</tr>
<tr>
<td WIDTH="27%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">mm</td>
<td WIDTH="54%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">显示带0的月,如果后面紧跟h或hh则显示带0的分</td>
</tr>
<tr>
<td WIDTH="27%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">mmm</td>
<td WIDTH="54%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">以Jan-Dec显示月份</td>
</tr>
<tr>
<td WIDTH="27%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">mmmm</td>
<td WIDTH="54%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">以January-December显示月份</td>
</tr>
<tr>
<td WIDTH="19%" VALIGN="TOP" ROWSPAN="2"><p ALIGN="JUSTIFY" id="content">年</td>
<td WIDTH="27%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">yy</td>
<td WIDTH="54%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">以两位数显示年(00-99)</td>
</tr>
<tr>
<td WIDTH="27%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">yyy</td>
<td WIDTH="54%" VALIGN="TOP"><p ALIGN="JUSTIFY" id="content">以四位数显示年(1900-2040)</td>
</tr>
</table>
<p ALIGN="JUSTIFY" id="content">好吧,第七章的内容就讲完了,在第八章中我将把VB的简单绘图知识教给大家,下次再见!</td>
</tr>
<tr>
<td width="50%" style="background-color: rgb(255,255,255); font-family: 宋体; font-size: 10pt; font-weight: bold"><strong><p align="right"></strong><a href="#VB教程"><img src="../images/up1.gif" alt="回到文档顶端" border="0" WIDTH="28" HEIGHT="29"></a></td>
</tr>
</table>
</center></div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -