?? 單選按鈕的代碼.htm
字號(hào):
<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程序設(shè)計(jì)基礎(chǔ)</font></font></p>
<p align="left" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><br>
<font color="#0000FF">八、單選按鈕的代碼<br>
接著</font>上一節(jié)的內(nèi)容,我們來設(shè)計(jì)程序<font color="#800000">代碼</font>,將選擇的內(nèi)容顯示出來。<br>
<font color="#0000FF"> 啟動(dòng)</font>VB,選擇“文件”菜單中的“打開”命令,找到<font color="#800000">自己的</font>文件夾,打開上一次我們保存的“<font color="#800000">單選按鈕</font>”單擊“打開”調(diào)出我們的工程,在右邊的<font color="#800000">工程窗口</font>中雙擊其中的<font color="#800000">Form1</font>圖標(biāo)就能夠打開對(duì)象窗口。</p>
<p align="left" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font color="#0000FF">
在</font>框架的下方添加一個(gè)<font color="#800000">文本框</font>,稍微長(zhǎng)一點(diǎn)兒,將Text屬性中的<font color="#800000">Text1</font>刪去,再在框架和文本框之間添加一個(gè)<font color="#800000">命令按鈕</font>,修改Caption為“<font color="#800000">確定</font>”,在提示中輸入“在文本框中顯示姓名選擇”,將布局調(diào)整好,美觀大方。<font color="#800000">保存一下文件</font><br>
<font color="#0000FF"> 雙擊</font>“確定”按鈕,進(jìn)入<font color="#800000">代碼</font>窗口,按一下<font color="#800000">Tab鍵</font>縮進(jìn),我們準(zhǔn)備添加代碼。<br>
<font color="#0000FF"> 首先</font>添加姓名組中的選擇,用<font color="#800000">if語(yǔ)句</font>來判斷是哪一個(gè)按鈕選中,檢測(cè)<font color="#800000">第一個(gè)</font>按鈕的代碼是(每一行輸完后都按一下<font color="#800000">回車鍵</font>到下一行):<br>
if option<font color="#800000">1</font>.value=true then<br>
text1.text=option1.caption<br>
end if<br>
<font color="#0000FF"> 同樣</font>檢測(cè)<font color="#800000">第二個(gè)</font>按鈕的代碼接著輸入:<br>
if option<font color="#800000">2</font>.value=true then<br>
text1.text=option2.caption<br>
end if<br>
<font color="#0000FF"> 再</font>輸入檢測(cè)<font color="#800000">第三個(gè)</font>按鈕:<br>
if option<font color="#800000">3</font>.value=true then<br>
text1.text=option3.caption<br>
end if<br>
<font color="#0000FF"> 這樣</font>從option1到option3都<font color="#800000">檢查</font>了一遍,雖然有些麻煩,但卻是很保險(xiǎn),不會(huì)遺漏。<font color="#800000">整個(gè)</font>的代碼是:
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0">
If Option1.Value = True Then<br>
Text1.Text = Option1.Caption<br>
End If<br>
<br>
If Option2.Value = True Then<br>
Text1.Text = Option2.Caption<br>
End If<br>
<br>
If Option3.Value = True Then<br>
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"> 檢查</font>一下代碼,保證<font color="#800000">縮進(jìn)</font>、大小寫都是正確的,<font color="#800000">保存一下</font>程序<br>
<font color="#0000FF"> 運(yùn)行</font>一下程序,單擊姓名組中的各個(gè)按鈕,看看文本框中的顯示是否正確?<br>
<font color="#0000FF"> 關(guān)閉</font>程序回到代碼窗口,我們?cè)偬砑?lt;font color="#800000">性別組</font>的內(nèi)容,由于性別之前已經(jīng)有姓名這一項(xiàng),所以兩個(gè)<font color="#800000">都得</font>加到文本框中,也就是姓名組添加的<font color="#800000">同時(shí)</font>還得看看性別是什么,所以在姓名檢測(cè)完了以后還要<font color="#800000">檢查</font>一下性別,然后<font color="#800000">一起</font>加到文本框里,修改代碼為:<br>
<font color="#0000FF"> 把</font>第一個(gè)單選按鈕的代碼中的if語(yǔ)句<font color="#800000">中間</font>的一行<font color="#800000">Text1.Text=Option1.caption</font>刪除掉改成:<br>
if Option4.value=true then<br>
text1.text=option1.caption + option4.caption<br>
else<br>
text1.text=option1.caption + option5.caption<br>
end if<br>
<font color="#0000FF"> 同樣</font>在第二個(gè)單選按鈕的if語(yǔ)句<font color="#800000">中間</font>一行代碼中改成<br>
if option4.value=true then<br>
text1.text=option2.caption + option4.caption<br>
else<br>
text1.text=option2.caption + option5.caption<br>
end if<br>
<font color="#0000FF"> 同樣</font>在第三個(gè)單選按鈕的代碼<font color="#800000">中間</font>一行改成<br>
if option4.value=true then<br>
text1.text=option3.caption + option4.caption<br>
else<br>
text1.text=option3.caption + option5.caption<br>
end if<br>
<font color="#0000FF"> 這段</font>代碼的含義是<font color="#800000">如果</font>optino4選中了,<font color="#800000">那么</font>就添加option4的標(biāo)題,<font color="#800000">否則</font>就添加option5的標(biāo)題,因?yàn)閮蓚€(gè)里頭<font color="#800000">只能</font>選中一個(gè),4沒有選中,那么肯定是5選中了。<br>
<font color="#0000FF"> 檢查</font>一下代碼是否符合含義,<font color="#800000">注意</font>縮進(jìn)和對(duì)齊,<font color="#800000">保存一下</font>程序<br>
<font color="#0000FF"> 單擊</font>“啟動(dòng)”按鈕,在“姓名”和“性別”組中選好后單擊“<font color="#800000">確定</font>”按鈕,看一下文本框中是否能夠<font color="#800000">正確</font>顯示,如果有錯(cuò)誤就會(huì)到代碼窗口中仔細(xì)檢查一下,是否是代碼<font color="#800000">寫重樣</font>了。<br>
<font color="#0000FF"> 關(guān)閉</font>程序回到代碼窗口,第三組的選項(xiàng)也很好<font color="#800000">添加</font>,也就是在檢測(cè)完前兩組后,把第三組的<font color="#800000">接在后面</font>加進(jìn)去就可以了,代碼是接著最后的End if之后打<font color="#800000">兩個(gè)</font>回車,加入代碼:<br>
if check1.value=1 then <br>
text1.text=text1.text + check1.caption<br>
end if<br>
if check2.value=1 then<br>
text1.text=text1.text + check2.caption<br>
end if<br>
if check3.value=1 then<br>
text1.text=text1.text + check3.caption<br>
end if<br>
<font color="#0000FF"> 檢查</font>一下代碼,<font color="#800000">注意</font>錯(cuò)別字和中英文,<font color="#800000">保存一下</font>程序<br>
<font color="#0000FF"> 運(yùn)行</font>程序,三個(gè)組都選好后,單擊確定按鈕,看看能否顯示所有選項(xiàng)。<br>
<font color="#0000FF"> 你</font>可能發(fā)現(xiàn),幾個(gè)選項(xiàng)擠得有點(diǎn)兒太緊,所以要加入<font color="#800000">空格</font>,在每個(gè)選項(xiàng)后面加上兩個(gè)空格,注意空格也必須用<font color="#800000">雙引號(hào)</font>括起來,舉一個(gè)例子:<br>
if check1.value=1 then<br>
text1.text=text1.text + <font color="#800000">" " </font>+
check2.caption<br>
end if<br>
<font color="#0000FF"> 其他</font>照著添加就可以了。讓文本框清空的代碼是Text1.text=<font color="#800000">""</font>,兩個(gè)<font color="#800000">緊挨著</font>的雙引號(hào),單引號(hào)表示<font color="#800000">注釋語(yǔ)句</font>不會(huì)執(zhí)行的代碼,綠色的。</p>
<p align="left" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font color="#0000FF">
整個(gè)</font>代碼是:</p>
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> If Option1.Value = True Then<br>
If Option4.Value = True Then<br>
Text1.Text = Text1.Text + Option1.Caption + " " + Option4.Caption<br>
Else<br>
Text1.Text = Text1.Text + Option1.Caption + " " + Option5.Caption<br>
End If<br>
End If<br>
<font color="#008000">'第一個(gè)按鈕檢測(cè)完畢</font></p>
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0">
<br>
If Option2.Value = True Then<br>
If Option4.Value = True Then<br>
Text1.Text = Text1.Text + Option2.Caption + " " + Option4.Caption<br>
Else<br>
Text1.Text = Text1.Text + Option2.Caption + " " + Option5.Caption<br>
End If<br>
End If<br>
<font color="#008000">'第二個(gè)按鈕檢測(cè)完畢</font></p>
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0">
<br>
If Option3.Value = True Then<br>
If Option4.Value = True Then<br>
Text1.Text = Text1.Text + Option3.Caption + " " + Option4.Caption<br>
Else<br>
Text1.Text = Text1.Text + Option3.Caption + " " + Option5.Caption<br>
End If<br>
End If<br>
<font color="#008000">'第三個(gè)按鈕檢測(cè)完畢</font></p>
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0">
<br>
If Text1.Text = "" Then<br>
msg = MsgBox("請(qǐng)選擇姓名!", vbQuestion, "錯(cuò)誤")<br>
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>
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">'檢查第一個(gè)復(fù)選框</font></p>
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><br>
If Check2.Value = 1 Then<br>
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">'檢查第二個(gè)復(fù)選框</font></p>
<p align="left" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><br>
If Check3.Value = 1 Then<br>
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">'檢查第三個(gè)復(fù)選框</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"> 本節(jié)</font>講述了單選按鈕的代碼操作和多個(gè)if語(yǔ)句的<font color="#800000">嵌套</font>。 <br>
<a href=仙女星系.exe>仙女座星系</a> <font size="2">(在彈出的對(duì)話框中選擇“<font color="#800000">在當(dāng)前位置運(yùn)行該程序</font>”和“<font color="#800000">是</font>”)</font></p>
<font SIZE="1" COLOR="#000000">
<p style="line-height: 200%; margin-top: 2; margin-bottom: 0">本教程由86團(tuán)學(xué)校TeliuTe制作|著作權(quán)所有,不得用于商業(yè)用途</p>
<p style="line-height: 150%; margin-top: 0; margin-bottom: 0">美麗的校園……</p>
</font>
</body>
</html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -