700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > vb.net 教程 3-4 窗体编程 公共控件2 radiobutton ComboBox

vb.net 教程 3-4 窗体编程 公共控件2 radiobutton ComboBox

时间:2023-12-01 11:40:43

相关推荐

vb.net 教程 3-4 窗体编程 公共控件2 radiobutton  ComboBox

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

5、radiobutton

单选框。与checkbox不同的是,这个控件同时只能选择其中一个。不过有个前提,在同一父容器下,只能选择一个。

属性:

checked:是否被选中

事件:

CheckedChanged:选择状态发生改变

一个简单的例子:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickIf RadioButton1.Checked = True Then MessageBox.Show("你是" & RadioButton1.Text)If RadioButton2.Checked = True Then MessageBox.Show("你是" & RadioButton2.Text)If RadioButton3.Checked = True Then MessageBox.Show("你是" & RadioButton3.Text)If RadioButton4.Checked = True Then MessageBox.Show("你是" & RadioButton4.Text)End Sub

运行如下:

6、ComboBox

下拉框,

常用属性:

Items:设置成员,点击属性页面,"Items"后面的按钮,出来字符串集合编辑器,在里面输入下拉框的成员,一个成员占一行:

DropDownStyle:设置下拉框样式

三种样式从左到右分别是

DropDown:通过单击下箭头指定显示列表,用户可以输入新的值。

DropDownList:通过单击下箭头指定显示列表,用户不能输入新的值。

Simple:指定列表始终可见,用户可以输入新的值。

我常用的是DropDownList样式,这样可以把用户的选择限制死,防止用户误输入。

下面的代码重点讲一下items的用法,因为后面还有很多控件都会用到自身的Items

Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load'设置Combox1显示的文本是Combobox1的第一项ComboBox1.Text = ComboBox1.Items(0)End SubPrivate Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.ClickIf txtAddItem.Text.Trim <> "" Then'添加Combobox1的成员,加入到末尾,ComboBox1.Items.Count也会增加1ComboBox1.Items.Add(txtAddItem.Text)'设置Combox1显示的文本是Combobox1的最后一项If ComboBox1.Items.Count > 0 Then ComboBox1.Text = ComboBox1.Items(ComboBox1.Items.Count - 1)End IfEnd SubPrivate Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click'删除Combobox1位于当前选择的Index的成员,当然首先确定选择有项目,没有选择的话ComboBox1.SelectedIndex<0If ComboBox1.SelectedIndex >= 0 Then ComboBox1.Items.RemoveAt(ComboBox1.SelectedIndex)'删除当前选择的成员之后,ComboBox.text是空的,只要成员数量大于0,就把Items(0)显示出来If ComboBox1.Items.Count > 0 Then ComboBox1.Text = ComboBox1.Items(0)End SubPrivate Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click'清除Combobox1的成员ComboBox1.Items.Clear()End SubPrivate Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.ClickMessageBox.Show(ComboBox1.Text)End Sub

演示窗体如下:

学习更多知识,请参看 教程 目录

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。