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

📄 单选按钮的代码.htm

📁 VB程序设计教程 1.0.0 是个学习vb的好
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>单选按钮的代码</title>
</head>

<body bgcolor="#CCFFFF" vlink="#0000FF">

<p align="center" style="margin-top: 0; margin-bottom: 0"><font color="#FF0000"><font face="华文彩云" size="6">VB程序设计基础</font></font></p>
<p align="left" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><br>
<font color="#0000FF">八、单选按钮的代码<br>
&nbsp;&nbsp;&nbsp; 接着</font>上一节的内容,我们来设计程序<font color="#800000">代码</font>,将选择的内容显示出来。<br>
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 启动</font>VB,选择“文件”菜单中的“打开”命令,找到<font color="#800000">自己的</font>文件夹,打开上一次我们保存的“<font color="#800000">单选按钮</font>”单击“打开”调出我们的工程,在右边的<font color="#800000">工程窗口</font>中双击其中的<font color="#800000">Form1</font>图标就能够打开对象窗口。</p>      
                    
                     
<p align="left" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font color="#0000FF">&nbsp;&nbsp;&nbsp;  
在</font>框架的下方添加一个<font color="#800000">文本框</font>,稍微长一点儿,将Text属性中的<font color="#800000">Text1</font>删去,再在框架和文本框之间添加一个<font color="#800000">命令按钮</font>,修改Caption为“<font color="#800000">确定</font>”,在提示中输入“在文本框中显示姓名选择”,将布局调整好,美观大方。<font color="#800000">保存一下文件</font><br>
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 双击</font>“确定”按钮,进入<font color="#800000">代码</font>窗口,按一下<font color="#800000">Tab键</font>缩进,我们准备添加代码。<br>
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 首先</font>添加姓名组中的选择,用<font color="#800000">if语句</font>来判断是哪一个按钮选中,检测<font color="#800000">第一个</font>按钮的代码是(每一行输完后都按一下<font color="#800000">回车键</font>到下一行):<br>
if option<font color="#800000">1</font>.value=true then<br>  
&nbsp;&nbsp;&nbsp; text1.text=option1.caption<br> 
end if<br>  
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 同样</font>检测<font color="#800000">第二个</font>按钮的代码接着输入:<br>
if option<font color="#800000">2</font>.value=true then<br>  
&nbsp;&nbsp;&nbsp; text1.text=option2.caption<br> 
end if<br>  
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 再</font>输入检测<font color="#800000">第三个</font>按钮:<br>
if option<font color="#800000">3</font>.value=true then<br>  
&nbsp;&nbsp;&nbsp; text1.text=option3.caption<br> 
end if<br>  
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 这样</font>从option1到option3都<font color="#800000">检查</font>了一遍,虽然有些麻烦,但却是很保险,不会遗漏。<font color="#800000">整个</font>的代码是:
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0">
    If Option1.Value = True Then<br>  
&nbsp;&nbsp;&nbsp; Text1.Text = Option1.Caption<br>  
    End If<br>  
<br>  
    If Option2.Value = True Then<br>  
&nbsp;&nbsp;&nbsp; Text1.Text = Option2.Caption<br>  
    End If<br>  
<br>  
    If Option3.Value = True Then<br>  
&nbsp;&nbsp;&nbsp; Text1.Text = Option3.Caption<br>  
    End If<br> </p> 
    <p align="left" style="line-height: 150%; margin-top: 0; margin-bottom: 0"> 
 
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 检查</font>一下代码,保证<font color="#800000">缩进</font>、大小写都是正确的,<font color="#800000">保存一下</font>程序<br>
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 运行</font>一下程序,单击姓名组中的各个按钮,看看文本框中的显示是否正确?<br> 
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 关闭</font>程序回到代码窗口,我们再添加<font color="#800000">性别组</font>的内容,由于性别之前已经有姓名这一项,所以两个<font color="#800000">都得</font>加到文本框中,也就是姓名组添加的<font color="#800000">同时</font>还得看看性别是什么,所以在姓名检测完了以后还要<font color="#800000">检查</font>一下性别,然后<font color="#800000">一起</font>加到文本框里,修改代码为:<br>
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 把</font>第一个单选按钮的代码中的if语句<font color="#800000">中间</font>的一行<font color="#800000">Text1.Text=Option1.caption</font>删除掉改成:<br>
if Option4.value=true then<br>  
&nbsp;&nbsp;&nbsp; text1.text=option1.caption + option4.caption<br>
else<br>
&nbsp;&nbsp;&nbsp; text1.text=option1.caption + option5.caption<br>
end if<br>  
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 同样</font>在第二个单选按钮的if语句<font color="#800000">中间</font>一行代码中改成<br>
if option4.value=true then<br>  
&nbsp;&nbsp;&nbsp; text1.text=option2.caption + option4.caption<br>
else<br>
&nbsp;&nbsp;&nbsp; text1.text=option2.caption + option5.caption<br>
end if<br>  
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 同样</font>在第三个单选按钮的代码<font color="#800000">中间</font>一行改成<br>
if option4.value=true then<br>  
&nbsp;&nbsp;&nbsp; text1.text=option3.caption + option4.caption<br>
else<br>
&nbsp;&nbsp;&nbsp; text1.text=option3.caption + option5.caption<br>
end if<br>  
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 这段</font>代码的含义是<font color="#800000">如果</font>optino4选中了,<font color="#800000">那么</font>就添加option4的标题,<font color="#800000">否则</font>就添加option5的标题,因为两个里头<font color="#800000">只能</font>选中一个,4没有选中,那么肯定是5选中了。<br>
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 检查</font>一下代码是否符合含义,<font color="#800000">注意</font>缩进和对齐,<font color="#800000">保存一下</font>程序<br>
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 单击</font>“启动”按钮,在“姓名”和“性别”组中选好后单击“<font color="#800000">确定</font>”按钮,看一下文本框中是否能够<font color="#800000">正确</font>显示,如果有错误就会到代码窗口中仔细检查一下,是否是代码<font color="#800000">写重样</font>了。<br>
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 关闭</font>程序回到代码窗口,第三组的选项也很好<font color="#800000">添加</font>,也就是在检测完前两组后,把第三组的<font color="#800000">接在后面</font>加进去就可以了,代码是接着最后的End if之后打<font color="#800000">两个</font>回车,加入代码:<br> 
if check1.value=1 then&nbsp;<br> 
&nbsp;&nbsp;&nbsp; text1.text=text1.text + check1.caption<br>
end if<br>  
if check2.value=1 then<br>  
&nbsp;&nbsp;&nbsp; text1.text=text1.text + check2.caption<br>
end if<br>  
if check3.value=1 then<br>  
&nbsp;&nbsp;&nbsp; text1.text=text1.text + check3.caption<br>
end if<br>  
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 检查</font>一下代码,<font color="#800000">注意</font>错别字和中英文,<font color="#800000">保存一下</font>程序<br>
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 运行</font>程序,三个组都选好后,单击确定按钮,看看能否显示所有选项。<br> 
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 你</font>可能发现,几个选项挤得有点儿太紧,所以要加入<font color="#800000">空格</font>,在每个选项后面加上两个空格,注意空格也必须用<font color="#800000">双引号</font>括起来,举一个例子:<br>
if check1.value=1 then<br>  
&nbsp;&nbsp;&nbsp; text1.text=text1.text + <font color="#800000">&quot; &quot; </font>+ 
check2.caption<br> 
end if<br>  
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 其他</font>照着添加就可以了。让文本框清空的代码是Text1.text=<font color="#800000">&quot;&quot;</font>,两个<font color="#800000">紧挨着</font>的双引号,单引号表示<font color="#800000">注释语句</font>不会执行的代码,绿色的。</p>      
                    
                     
<p align="left" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font color="#0000FF">&nbsp;&nbsp;&nbsp;  
整个</font>代码是:</p>       
                     
                      
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0">    If Option1.Value = True Then<br>  
&nbsp;&nbsp;&nbsp; If Option4.Value = True Then<br>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text1.Text = Text1.Text + Option1.Caption + "  " + Option4.Caption<br>  
&nbsp;&nbsp;&nbsp; Else<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text1.Text = Text1.Text + Option1.Caption + "  " + Option5.Caption<br>  
&nbsp;&nbsp;&nbsp; End If<br>  
    End If<br>  
<font color="#008000">'第一个按钮检测完毕</font></p>       
                     
                      
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0">     
<br> 
    If Option2.Value = True Then<br>  
&nbsp;&nbsp;&nbsp; If Option4.Value = True Then<br>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text1.Text = Text1.Text + Option2.Caption + "  " + Option4.Caption<br>  
&nbsp;&nbsp;&nbsp; Else<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text1.Text = Text1.Text + Option2.Caption + "  " + Option5.Caption<br>  
&nbsp;&nbsp;&nbsp; End If<br>  
    End If<br>  
<font color="#008000">'第二个按钮检测完毕</font></p>       
                     
                      
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0">     
<br> 
    If Option3.Value = True Then<br>  
&nbsp;&nbsp;&nbsp; If Option4.Value = True Then<br>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text1.Text = Text1.Text + Option3.Caption + "  " + Option4.Caption<br>  
&nbsp;&nbsp;&nbsp; Else<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text1.Text = Text1.Text + Option3.Caption + "  " + Option5.Caption<br>  
&nbsp;&nbsp;&nbsp; End If<br>  
    End If<br>  
<font color="#008000">'第三个按钮检测完毕</font></p>       
                     
                      
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0">     
<br> 
    If Text1.Text = "" Then<br>  
&nbsp;&nbsp;&nbsp; msg = MsgBox(&quot;请选择姓名!", vbQuestion, "错误")<br>  
&nbsp;&nbsp;&nbsp; Exit Sub<br>  
    End If<br>  
<font color="#008000">'</font><font color="#008000">提示一下</font></p>       
                     
                      
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0">     
<br> 
    If Check1.Value = 1 Then<br>  
&nbsp;&nbsp;&nbsp; Text1.Text = Text1.Text + "  " + Check1.Caption<br>  
    End If</p>        
                      
                       
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font color="#008000">'检查第一个复选框</font></p>       
                     
                      
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><br> 
    If Check2.Value = 1 Then<br>  
&nbsp;&nbsp;&nbsp; Text1.Text = Text1.Text + "  " + Check2.Caption<br>  
    End If</p>        
                      
                       
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font color="#008000">'检查第二个复选框</font></p>       
                     
                      
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><br> 
    If Check3.Value = 1 Then<br>  
&nbsp;&nbsp;&nbsp; Text1.Text = Text1.Text + "  " + Check3.Caption<br>  
    End If</p>        
                      
                       
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font color="#008000">'检查第三个复选框</font></p>       
                     
                      
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>       
                     
                      
<p align="left" style="line-height: 150%; margin-top: 0; margin-bottom: 0">    Text1.Text = Text1.Text + Chr(13) + Chr(10)</p>        
                      
                       
<p align="left" style="line-height: 150%; margin-top: 0; margin-bottom: 0">    <font color="#008000">'回车换行</font><br> 
<font color="#0000FF">&nbsp;&nbsp;&nbsp; 本节</font>讲述了单选按钮的代码操作和多个if语句的<font color="#800000">嵌套</font>。&nbsp;&nbsp;&nbsp;<br>  
&nbsp;&nbsp;&nbsp; <a href=仙女星系.exe>仙女座星系</a> <font size="2">(在弹出的对话框中选择“<font color="#800000">在当前位置运行该程序</font>”和“<font color="#800000">是</font>”)</font></p>        
                      
                       
<font SIZE="1" COLOR="#000000">       
<p style="line-height: 200%; margin-top: 2; margin-bottom: 0">本教程由86团学校TeliuTe制作|著作权所有,不得用于商业用途</p>       
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0">美丽的校园……</p>       
</font>             
                    
                     
</body>      
      
</html>      

⌨️ 快捷键说明

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