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

📄 class_book_begin067.htm

📁 关于VB开发的教程
💻 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;}
a:hover{text-decoration:underline}
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_begin066.htm">第一页</a> 第二页 <a href="class_book_begin068.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)"><font FACE="宋体" SIZE="3"><p ALIGN="JUSTIFY">上次说到,输入不仅仅是向文本框输入字符,凡是从外界向程序传递数据都可以叫做输入,我们一一说明白。</p>
        <p ALIGN="JUSTIFY">无线钮,记得我们曾在第一章中完成过我们的第一个程序――“</font><font SIZE="3">Hello World</font><font FACE="宋体" SIZE="3">!”它就是一种输入的方法,在设计</font><font SIZE="3">VB</font><font FACE="宋体" SIZE="3">程序时可以通过一组这样的无线钮,它们之间互相排斥,也就是说,只能有一个按钮被选中。它因为很象无线电收音机上的按钮因而得名无线钮。如果要在一个窗体内放置多组无线钮必须用</font><font SIZE="3">Frame</font><font FACE="宋体" SIZE="3">控件把它们区分开来。下面我们在来完成一个例子,顺便熟悉一下另外两个处理字符串的函数――</font><font SIZE="3">LTrim()</font><font FACE="宋体" SIZE="3">和</font><font SIZE="3">Rtrim()</font><font FACE="宋体" SIZE="3">,它们分别用来删除字符串左边的空格和右边的空格,让我们看看它们是如何工作的。</p>
        <p ALIGN="JUSTIFY"><img src="06-08.gif" alt="06-08.gif (2920 字节)" WIDTH="354" HEIGHT="221"></p>
        <p ALIGN="JUSTIFY">程序窗体如图</font><font SIZE="3">6</font><font FACE="宋体" SIZE="3">-</font><font SIZE="3">8</font><font FACE="宋体" SIZE="3">,由于窗体比较简单,无需修改更多的属性,一共有三个文本框(</font><font SIZE="3">Text1</font><font FACE="宋体" SIZE="3">到</font><font SIZE="3">3</font><font FACE="宋体" SIZE="3">),分别用来显示输入字符,显示把空格换成星号的字符,和经过处理的字符,还有三个无线钮(</font><font SIZE="3">Option1</font><font FACE="宋体" SIZE="3">到</font><font SIZE="3">3</font><font FACE="宋体" SIZE="3">),要修改的只有</font><font SIZE="3">Caption</font><font FACE="宋体" SIZE="3">属性,分别是“删除左边的空格”、“删除右边的空格”和“删除两边的空格”。因为空格在我们在文本框中无法看到空格,所以我们通过一个称作</font><font SIZE="3">AddStar</font><font FACE="宋体" SIZE="3">的函数把原有字符串中的空格转换为星号,使我们能够看到函数的效果。下面在程序中添加代码,首先在</font><font SIZE="3">General</font><font FACE="宋体" SIZE="3">中声明窗体级变量:</font><font SIZE="3"></p>
        <blockquote>
          <p ALIGN="JUSTIFY"><font color="#004080">Dim LTrimmedword$, RTrimmedword$</font> <font color="#008000" SIZE="3">'</font><font FACE="宋体" SIZE="3" color="#008000">删除左边空格和右边空格后的字符串</font></p>
          <p ALIGN="JUSTIFY"><font color="#004080">Dim LRTrimmedword$</font> <font color="#008040" SIZE="3">'</font><font FACE="宋体" SIZE="3" color="#008040">删除两边空格后的字符串</font></font><font FACE="宋体" SIZE="3"></p>
          </font>
        </blockquote>
        <font FACE="宋体" SIZE="3"><p ALIGN="JUSTIFY">然后,我们通过一个叫做</font><font SIZE="3">addstar</font><font FACE="宋体" SIZE="3">的函数为字符串加上星号,以前曾说过函数与过程的分别,函数会返回一个函数值,所以</font><font SIZE="3">addstar</font><font FACE="宋体" SIZE="3">也就相当于一个变量,其实这很好理解,就象中学代数中的函数,函数就相当于变量</font><font SIZE="3">Y</font><font FACE="宋体" SIZE="3">,而函数中的参数则相当于自变量</font><font SIZE="3">X</font><font FACE="宋体" SIZE="3">,当然可能有很多自变量</font><font SIZE="3">X1</font><font FACE="宋体" SIZE="3">、</font><font SIZE="3">X2</font><font FACE="宋体" SIZE="3">……请原谅,我并不想在这里上数学课,因为我厌恶那玩意儿,特别是上了大学之后,好在大二以后就再也不会有数学课了,哈哈……</font><font SIZE="3"></p>
        <p ALIGN="JUSTIFY"><font color="#004080">Function addstar(tempword As String) As String</font></p>
        <blockquote>
          <p ALIGN="JUSTIFY"><font color="#004080">Dim charindex%</font><br>
          <font color="#004080" SIZE="3">Dim pasteword$</font><font color="#008000"> '<font FACE="宋体" SIZE="3">加星号后的字符串</font></font><br>
          <font color="#004080">For charindex = 1 To Len(tempword)</font></p>
          <blockquote>
            <p ALIGN="JUSTIFY"><font color="#004080" SIZE="3">If Mid(tempword, charindex, 1) = &quot; 
            &quot; Then </font><font color="#008040">'<font FACE="宋体" SIZE="3">如果字符为空格</font></font></p>
            <blockquote>
              <p ALIGN="JUSTIFY"><font color="#004080">pasteword = pasteword + &quot;*&quot;</font></p>
            </blockquote>
            <p ALIGN="JUSTIFY"><font color="#004080">Else: pasteword = pasteword + Mid(tempword, 
            charindex, 1)<br>
            End If</font></p>
          </blockquote>
          <p ALIGN="JUSTIFY"><font color="#004080">Next</font><br>
          <font color="#004080" SIZE="3">addstar = pasteword </font><font color="#008040">'<font FACE="宋体" SIZE="3">返回函数值</font></font></p>
        </blockquote>
        <p ALIGN="JUSTIFY"><font color="#004080">End Function</font></p>
        <p ALIGN="JUSTIFY"></font><font FACE="宋体" SIZE="3">下面的代码很简单,并且我为它做了注释,就不再一一解释了,我只想简要的提几句,</font><font SIZE="3">Ltrim</font><font FACE="宋体" SIZE="3">和</font><font SIZE="3">Rtrim</font><font FACE="宋体" SIZE="3">只有一个参数(字符型),另外下面用到了焦点事件,我们曾在第三章中介绍过,当</font><font SIZE="3">Text1</font><font FACE="宋体" SIZE="3">得到焦点(</font><font SIZE="3">GotFocus</font><font FACE="宋体" SIZE="3">)时,清空所有的文本框,当它失去焦点(</font><font SIZE="3">LostFocus</font><font FACE="宋体" SIZE="3">)时,对字符进行</font><font SIZE="3">addstar</font><font FACE="宋体" SIZE="3">并且在</font><font SIZE="3">Text2</font><font FACE="宋体" SIZE="3">中显示。</font><font SIZE="3"></p>
        <p ALIGN="JUSTIFY"></font><font color="#004080" SIZE="3">Private Sub Text1_GotFocus()</font><font color="#008040"><font SIZE="3"> '</font><font FACE="宋体" SIZE="3">但焦点于</font></font><font SIZE="3" color="#008040">Text1</font><font SIZE="3"></p>
        <blockquote>
          <p ALIGN="JUSTIFY"><font SIZE="3" color="#008040">'</font><font FACE="宋体" SIZE="3" color="#008040">清除所有文本框中的字符</font><br>
          <font color="#004080">Text1.Text = &quot;&quot;<br>
          Text2.Text = &quot;&quot;<br>
          Text3.Text = &quot;&quot;</font></p>
        </blockquote>
        <p ALIGN="JUSTIFY"><font color="#004080">End Sub</font></p>
        <p ALIGN="JUSTIFY"> </p>
        <p ALIGN="JUSTIFY"><font color="#004080">Private Sub Text1_LostFocus()</font></p>
        <blockquote>
          <p ALIGN="JUSTIFY"><font color="#004080">Dim Origionword As String<br>
          Origionword = Text1.Text<br>
          Text2.Text = addstar(Origionword)<br>
          LTrimmedword = addstar(LTrim(Origionword))<br>
          RTrimmedword = addstar(RTrim(Origionword))<br>
          LRTrimmedword = LTrim(RTrim(Origionword))</font></p>
        </blockquote>
        <p ALIGN="JUSTIFY"><font color="#004080">End Sub</font></font><font FACE="宋体" SIZE="3"></p>
        <p ALIGN="JUSTIFY"></font><font SIZE="3"> </p>
        <p ALIGN="JUSTIFY"><font color="#004080">Private Sub Option1_Click()</font></p>
        <blockquote>
          <p ALIGN="JUSTIFY"><font color="#004080" SIZE="3">Text3.Text = LTrimmedword </font><font color="#008040">'<font FACE="宋体" SIZE="3">显示删除左空格后的字符串</font></font></p>
        </blockquote>
        <p ALIGN="JUSTIFY"><font color="#004080">End Sub</font></p>
        <p ALIGN="JUSTIFY"> </p>
        <p ALIGN="JUSTIFY"><font color="#004080">Private Sub Option2_Click()</font></p>
        <blockquote>
          <p ALIGN="JUSTIFY"><font color="#004080" SIZE="3">Text3.Text = RTrimmedword</font><font color="#008040"> '<font FACE="宋体" SIZE="3">显示删除右空格后的字符串</font></font></p>
        </blockquote>
        <p ALIGN="JUSTIFY"><font color="#004080">End Sub</font></p>
        <p ALIGN="JUSTIFY"> </p>
        <p ALIGN="JUSTIFY"><font color="#004080">Private Sub Option3_Click()</font></p>
        <blockquote>
          <p ALIGN="JUSTIFY"><font color="#004080" SIZE="3">Text3.Text = LRTrimmedword </font><font color="#008040">'<font FACE="宋体" SIZE="3">显示删除两边空格后的字符串</font></font></p>
        </blockquote>
        <p ALIGN="JUSTIFY"><font color="#004080">End Sub</font></font><font FACE="宋体" SIZE="3"></p>
        <p ALIGN="JUSTIFY">最后结果如图</font><font SIZE="3">6</font><font FACE="宋体" SIZE="3">-</font><font SIZE="3">9</font><font FACE="宋体" SIZE="3">。</p>
        <p ALIGN="JUSTIFY"><img src="06-09.gif" alt="06-09.gif (2626 字节)" WIDTH="354" HEIGHT="221"></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 + -