📄 class_book_begin064.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
.redLink{ color:red;}
.blueLink{color:blue;}
A{font-size:12pt;text-decoration:none;}
P{font:12pt;line-height:22px;}
a:hover{text-decoration:underline}
h2{font-size:30px;text-align:center;color:rgb(0,144,144);letter-spacing:.5em;font-weight:bold}
.whiteB{color:black;background-color:white;}
.blueB{color:yellow;background-color:blue;}
.blackB{color:white;background-color:black;}
</style>
<title>第六章:输入(一)</title>
</head>
<script langue="javascript">
<!--
var source;
var flagDU="Up";
var oldClass="";
function ChangeColor(){
source=window.event.srcElement;
if (source.className=="redLink"){
source.className="blueLink";
}
else{
source.className="redLink";
}
}
function moveChangeBack(){
source=window.event.srcElement;
if (source.className==oldClass){oldClass=source.className;
if (flagDU=="Up"){
if (source.className="whiteB"){
source.className="blueB";
}else{
source.className="whiteB";
}
}else{source.className="blackB";
}
}else{//oldClass=source.className;
if (source.className=="whiteB"){
source.className="blueB";
}else{
source.className="whiteB";
}
}
}
function clickChangeBack(){
index01.className="whiteB"
index02.className="whiteB"
source=window.event.srcElement;
source.className="blackB";
flagDU="Down";
oldClass=source.className;
}
function returnValue(){
index01.className="whiteB";
index02.className="whiteB";
flagDU="Up";
}
function getAppVersion()
{
appname= navigator.appName;
appversion = navigator.appVersion;
majorver = appversion.substring(0, 1);
if ( (appname == "Netscape") && ( majorver >= 3 ) ) return 1;
if ( (appname == "Microsoft Internet Explorer") && (majorver>= 4) ) return 1;
return 0;
}
function switchGraphic(num, imgname) {
if (getAppVersion()) {
document[imgname].src = img[num].src;
}
}//-->
</script>
<body>
<script language="javascript">
<!--
imgsrc = new Array();
imgsrc[0] = "images/dot_blank.gif";
imgsrc[1] = "images/dot_filled.gif";
if (getAppVersion())
{
img = new Array();
for (i = 0; i < imgsrc.length; i++)
{
img[i] = new Image();
img[i].src = imgsrc[i];
}
}
//-->
</script>
<div align="center"><center>
<table border="0" cellpadding="8" cellspacing="0" width="100%">
<tr>
<td width="24%" valign="top"><h2><img src="../images/class_img.gif" WIDTH="160" HEIGHT="51"></h2>
<hr size="4" color="#FF0000" width="75%">
<p align="center"><img src="../images/menu_bar_jc_d.gif" WIDTH="150" HEIGHT="35"><img src="../images/dot_blank.gif" name="b1" WIDTH="10" HEIGHT="35"><br>
<a href="../class_api.htm" onMouseOver="switchGraphic('1','b2')" onMouseOut="switchGraphic('0','b2')"><img src="../images/menu_bar_api.gif" border="0" WIDTH="150" HEIGHT="35"></a><img src="../images/dot_blank.gif" name="b2" WIDTH="10" HEIGHT="35"><br>
<span align="center"><a href="../class_wz.htm" onMouseOver="switchGraphic('1','b5')" onMouseOut="switchGraphic('0','b5')"><img src="../images/menu_bar_wz.gif" border="0" WIDTH="150" HEIGHT="35"></a><img src="../images/dot_blank.gif" name="b5" WIDTH="10" HEIGHT="35"></span><br>
<a href="../class_tip.htm" onMouseOver="switchGraphic('1','b3')" onMouseOut="switchGraphic('0','b3')"><img src="../images/menu_bar_tip.gif" border="0" WIDTH="150" HEIGHT="35"></a><img src="../images/dot_blank.gif" name="b3" WIDTH="10" HEIGHT="35"><br>
<a href="../class_fqa.htm" onMouseOver="switchGraphic('1','b4')" onMouseOut="switchGraphic('0','b4')"><img src="../images/menu_bar_end_fqa.gif" border="0" WIDTH="150" HEIGHT="35"></a><img src="../images/dot_blank.gif" name="b4" WIDTH="10" HEIGHT="35"></p>
<p align="center"><a href="../context.htm"><img src="../images/logo_return.jpg" border="0" WIDTH="70" HEIGHT="260"></a></td>
<td width="76%" valign="top" align="center"><div align="center"><center><table border="0" cellpadding="8" cellspacing="0" width="27%" style="background-color: rgb(0,128,128)">
<tr>
<td width="100%" valign="top" align="center"><a name="top"><font size="5" color="#C0C0C0"><strong>VB入门教程</strong></font></a></td>
</tr>
</table>
</center></div><p><a href="class_book_begin061.htm">第一页</a> <a href="class_book_begin062.htm">第二页</a> <a href="class_book_begin063.htm">第三页</a>
第四页<br>
<a href="class_book_begin065.htm">第五页</a> <a href="../class_book.htm">返回</a>
</p>
<div align="center"><center><table border="0" cellpadding="8" cellspacing="0" width="100%">
<tr>
<td width="100%"><font size="3" color="#8000FF"><strong><em>第六章:输入(一)</em></strong></font></td>
</tr>
<tr>
<td width="100%" style="background-color: rgb(232,255,254)"><p ALIGN="JUSTIFY"><font size="3">从刚才的例子里,我们接触了一些VB中的输入方法,其实数据输入不仅仅局限于向Text框中输入数据,数据的输入还有许多其他的方法,譬如前面的Check1也是向程序发出一定的信息,只要是通过操作向计算机发布命令就可以叫做输入,下面我们来一一介绍。</font></p>
<p ALIGN="JUSTIFY"><font size="3">首先要介绍的当然是输入用到最多的Text框。Text没有Click事件,它的缺省事件是Change,也就是当Text框里的一旦内容改变就会激发该事件发生,还是让我们用实例说话吧。</font></p>
<p ALIGN="JUSTIFY"><font size="3">Text有几个特殊的属性,一是MultiLine(多行)属性,MultiLine属性为False的时候,不论文本有多长,它都会在一行里显示下去,换行符无效,当它为True的时候,字符串显示到Text框边缘的时候就会自动换行。另一个与之配合的属性是ScrollBars属性,它有4个值,0到3,分别是没有滚动条,水平滚动条,垂直滚动条,和二者都有,它是一个只读属性,也就是说只有在程序设计过程中才能改变,而不能在程序运行中改变,但是我们可以通过API解决运行中改变的问题,我们放在以后再讲。PasswordChart属性,我们经常在程序操作中遇到要填写密码的部分,为了保密起见,在屏幕上显示的字符一般“*”表示,并且这时是无法用剪贴板的,这在VB中非常容易做到,把PasswordChart中设一个用来代替字符串的字符即可,并不一定是“*”哦。</font></p>
<p ALIGN="JUSTIFY"><font size="3">下面我们完成一个有趣的小程序,是我以前遇到过的一个程序,我对其进行了部分修改。程序的意思是这样的,假设你是一个司机应超速被警察扣留,现在需要你向警察陈述你的理由,如果你的话能打动他,就可以免交罚金,如果你在规定的字数里没有说出什么有震撼力的话,那老兄你可就惨了,交钱吧。J
</font></p>
<p ALIGN="JUSTIFY"><font size="3">这个程序比原来的英文程序有了几点不同,第一,由于用到的是双字节中文的中文,所以对于检测中文字符和英文有很大不同,第二,原来的英文程序不管你说多少,警察依然是依法从事,本例中给了犯规者10%的免罚机会(有点无聊……L
,不过也是为了学一些东东嘛)。下面就跟我来设计窗体吧。(如图6-6)</font></p>
<p ALIGN="JUSTIFY"><img src="06-06.gif" alt="06-06.gif (5508 字节)" WIDTH="416" HEIGHT="321"></p>
<table BORDER="1" CELLSPACING="1" CELLPADDING="7" WIDTH="463" bordercolor="#800080" bordercolorlight="#800080" bordercolordark="#800080">
<tr>
<td WIDTH="35%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Form1</font></td>
<td WIDTH="65%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Icon Stop图标</font></p>
<p ALIGN="JUSTIFY"><font size="3">Caption 停车检查</font></p>
<p ALIGN="JUSTIFY"><font size="3">BorderStyle 3</font></td>
</tr>
<tr>
<td WIDTH="35%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Label1</font></td>
<td WIDTH="65%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Caption
警官,我超速是有原因的,请听我说:</font></p>
<p ALIGN="JUSTIFY"><font size="3">BackColor &H00808080&</font></p>
<p ALIGN="JUSTIFY"><font size="3">ForeColor &H80000012&</font></td>
</tr>
<tr>
<td WIDTH="35%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Label2</font></td>
<td WIDTH="65%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Caption 你必须在30字之内完成你理由的申诉,否则将受到罚款!</font></p>
<p ALIGN="JUSTIFY"><font size="3">BackColor &H00808080&</font></p>
<p ALIGN="JUSTIFY"><font size="3">ForeColor &H00FF0000&</font></td>
</tr>
<tr>
<td WIDTH="35%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Text1</font></td>
<td WIDTH="65%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">MultiLink True</font></p>
<p ALIGN="JUSTIFY"><font size="3">Text “”</font></td>
</tr>
<tr>
<td WIDTH="35%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Picture1</font></td>
<td WIDTH="65%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Autosize True</font></p>
<p ALIGN="JUSTIFY"><font size="3">BackColor &H00808080&</font></p>
<p ALIGN="JUSTIFY"><font size="3">Visible True</font></td>
</tr>
<tr>
<td WIDTH="35%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Picture2</font></td>
<td WIDTH="65%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Autosize True</font></p>
<p ALIGN="JUSTIFY"><font size="3">BackColor &H00808080&</font></p>
<p ALIGN="JUSTIFY"><font size="3">Visible False</font></td>
</tr>
<tr>
<td WIDTH="35%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Picture3</font></td>
<td WIDTH="65%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Autosize True</font></p>
<p ALIGN="JUSTIFY"><font size="3">BackColor &H00808080&</font></p>
<p ALIGN="JUSTIFY"><font size="3">Visible False</font></td>
</tr>
<tr>
<td WIDTH="35%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Command1</font></td>
<td WIDTH="65%" VALIGN="TOP"><p ALIGN="JUSTIFY"><font size="3">Visible False</font></td>
</tr>
</table>
<p> </p>
<p ALIGN="JUSTIFY"> </p>
<p ALIGN="JUSTIFY"><font size="3">三个图片框Picture1-3的Picture属性分别为绿灯,黄灯和红灯,这些图标在VB目录下的Graphics\Icons\Traffic下(VB5),或是VB目录下的Icons\Traffic下(VB4、VB3),你还可以为Label1在Font属性里选一个合适的字体和字重。下面我们来看代码:</font></p>
<p ALIGN="JUSTIFY"><font size="3">首先初始化程序:</font></p>
<p ALIGN="JUSTIFY"><em><strong><font size="3" color="#004080">Private Sub Form_Load()</font></strong></em></p>
<blockquote>
<p ALIGN="JUSTIFY"><font size="3" color="#008000"><em><strong>'把Command1放在水平的中央</strong></em></font></p>
<p ALIGN="JUSTIFY"><em><strong><font size="3" color="#004080">Command1.Top = Label2.Top+200</font></strong></em></p>
<p ALIGN="JUSTIFY"><em><strong><font size="3" color="#004080">Command1.Left = (Form1.Width
- Command1.Width) / 2</font></strong></em></p>
<p ALIGN="JUSTIFY"><font size="3" color="#008000"><em><strong>'重叠三个图片框</strong></em></font></p>
<p ALIGN="JUSTIFY"><em><strong><font size="3" color="#004080">Picture2.Left =
Picture1.Left</font></strong></em></p>
<p ALIGN="JUSTIFY"><em><strong><font size="3" color="#004080">Picture2.Top = Picture1.Top</font></strong></em></p>
<p ALIGN="JUSTIFY"><em><strong><font size="3" color="#004080">Picture3.Left =
Picture1.Left</font></strong></em></p>
<p ALIGN="JUSTIFY"><em><strong><font size="3" color="#004080">Picture3.Top = Picture1.Top</font></strong></em></p>
</blockquote>
<p ALIGN="JUSTIFY"><em><strong><font size="3" color="#004080">End Sub</font></strong></em></p>
<p ALIGN="JUSTIFY"><font size="3">下面我来讲一讲程序是怎样运行的,我们将用到Text的Change事件,每当字符串改变一次就对它进行一次检查,算出已经说的字数,当字数超过10时,下面的红绿灯会变成黄色,并且在这时随机抽取一个数,它在一个范围内出现的可能是10%,如果在这个范围内就可以免交罚金,如果不在范围则继续,当文字超过20的时候,红绿灯会变成红色,再一次抽取随机数字,步骤同上,如果30个字已满,再进行一次检查,如果你真的那么不幸就该罚款了。</font></p>
<p ALIGN="JUSTIFY"><font size="3">程序的步骤清楚了,我们在来看看程序的要点。由于中文是双字节字符,也就是说一个汉字占两个ASC码的位置,而VB显然是为英语国家设计的用Len函数无法返回汉字字节,也就是说一个汉字和和一个ASC字符在Len函数中得到的值是一样的,这就需要特殊处理。用Asc()函数,在括号内放入一个ASC字符或字符串,它会把第一个ASC码转化为0-255的数值,而把一个汉字放入,它的值必然不在0-255,这也就可以作为判定汉字的基础。我们可以把英文算作半个汉字。</font></td>
</tr>
</table>
</center></div><p align="right"><strong><a href="#top"><img src="top2.gif" border="0" WIDTH="27" HEIGHT="30"></a></strong></td>
</tr>
</table>
</center></div>
<p> </p>
</body>
</html>
<!--webbot bot="HTMLMarkup" TAG="XBOT" StartSpan --></SCRIPT><!--webbot BOT="HTMLMarkup" endspan -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -