📄 class_book_begin044.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;}
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_begin041.htm">第一页</a> <a href="class_book_begin042.htm">第二页</a> <a href="class_book_begin043.htm">第三页</a>
第四页<br>
<a href="class_book_begin045.htm">第五页</a> <a href="class_book_begin046.htm">第六页</a>
<a href="class_book_begin047.htm">第七页</a> <a href="../class_book.htm">返回</a>
<br>
</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>第四章:按钮(4)</em></strong></font></td>
</tr>
<tr>
<td width="100%" style="background-color: rgb(232,255,254)"><p ALIGN="JUSTIFY"><strong>先实现数据的输入,请看代码。</strong></p>
<p ALIGN="JUSTIFY"><strong>与未创立组的控件相比,它的事件里多了Index参数,组里所有的对象都使用同一个事件。</strong></p>
<p ALIGN="JUSTIFY"><strong>先在General里声明窗体级全局变量:</strong></p>
<p ALIGN="JUSTIFY"><strong><font color="#004080">Dim Num1, Num2 As Single<br>
Dim StrNum1, StrNum2 As String<br>
Dim FirstNum As Boolean </font><font color="#008040">'判断是否是数字开头</font></strong></p>
<p ALIGN="JUSTIFY"><strong>因为Text1里是存放字符串的,所以要把输入的数据进行运算,必须把字符型变量转变为实型变量,分别用Num1、Num2和StrNum1、StrNum2进行存放。</strong></p>
<p ALIGN="JUSTIFY"><strong>在Form_Load事件里给变量初赋值:</strong></p>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>Num1 = 0<br>
Num2 = 0<br>
StrNum1 = ""<br>
StrNum2 = ""<br>
FirstNum = True</strong></font></p>
<p ALIGN="JUSTIFY"><strong>因为17个按钮使用的是同一事件,所以我们用一个Select
Case语句区分各个按钮,双击任意按钮输入代码:</strong></p>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>Private Sub Command1_Click(Index As
Integer)</strong></font></p>
<blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>Select Case Index</strong></font></p>
<blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>Case 0 To 9</strong></font></p>
<blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>If FirstNum Then</strong></font></p>
<blockquote>
<p ALIGN="JUSTIFY"><strong><font color="#004080">StrNum1 = Str(Index)</font><font color="#008040">’实型转字符型</font><br>
<font color="#004080">FirstNum = False</font><font color="#008040">’初赋值为假</font></strong></p>
</blockquote>
<p ALIGN="JUSTIFY"><strong><font color="#004080">Else</font><font color="#008040">’非初赋值</font></strong></p>
<blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>StrNum1 = StrNum1 + Str(Index)</strong></font></p>
</blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>End If</strong></font></p>
<p ALIGN="JUSTIFY"><strong><font color="#004080">Text1.Text = StrNum1</font><font color="#008040">’显示</font></strong></p>
</blockquote>
<p ALIGN="JUSTIFY"><font color="#FF0000"><strong><还有其他语句,等待插入></strong></font></p>
</blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>End Select</strong></font></p>
</blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>End Sub</strong></font></p>
<p ALIGN="JUSTIFY"><strong>运行程序的话已经可以输入数据,下面我们来处理小数点。</strong></p>
<p ALIGN="JUSTIFY"><strong>在General里声明新的变量:</strong></p>
<p ALIGN="JUSTIFY"><strong><font color="#004080">Dim PointFlag As Boolean </font><font color="#008040">'判断是否已有小数点</font></strong></p>
<p ALIGN="JUSTIFY"><strong>我们知道小数点只能输入一次,一个数据中不能有多个小数点存在,所以需要一个布尔变量做判断。</strong></p>
<p ALIGN="JUSTIFY"><strong>在Form_Load里进行初赋值:</strong></p>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>PointFlag = False</strong></font></p>
<p ALIGN="JUSTIFY"><strong>在Command的单击事件里接着上面输入新的代码:</strong></p>
<blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>Case 10</strong></font></p>
<blockquote>
<p ALIGN="JUSTIFY"><strong><font color="#004080">If Not PointFlag Then</font><font color="#008040">’如果没有小数点</font></strong></p>
<blockquote>
<p ALIGN="JUSTIFY"><strong><font color="#004080">If FirstNum Then</font><font color="#008040">’如果是第一个字符</font></strong></p>
<blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>StrNum1 = "0."<br>
FirstNum = False</strong></font></p>
</blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>Else</strong></font></p>
<blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>StrNum1 = StrNum1 + "."</strong></font></p>
</blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>End If</strong></font></p>
</blockquote>
<p ALIGN="JUSTIFY"><strong><font color="#004080">Else</font><font color="#008040">’已有小数点</font></strong></p>
<blockquote>
<p ALIGN="JUSTIFY"><strong><font color="#004080">Exit Sub</font><font color="#008040">’则退出单击事件</font></strong></p>
</blockquote>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>End If</strong></font></p>
<p ALIGN="JUSTIFY"><font color="#004080"><strong>PointFlag = True<br>
Text1.Text = StrNum1</strong></font></p>
</blockquote>
</blockquote>
</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 + -