您的位置:首页 > 编程语言 > VB

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

2017-05-04 22:10 579 查看
5、radiobutton

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

属性:

checked:是否被选中

事件:

CheckedChanged:选择状态发生改变

一个简单的例子:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If 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 Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If txtAddItem.Text.Trim <> "" Then
'添加Combobox1的成员,加入到末尾,ComboBox1.Items.Count也会增加1
ComboBox1.Items.Add(txtAddItem.Text)
'设置Combox1显示的文本是Combobox1的最后一项
If ComboBox1.Items.Count > 0 Then ComboBox1.Text = ComboBox1.Items(ComboBox1.Items.Count - 1)
End If
End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
'删除Combobox1位于当前选择的Index的成员,当然首先确定选择有项目,没有选择的话ComboBox1.SelectedIndex<0
If 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 Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
'清除Combobox1的成员
ComboBox1.Items.Clear()
End Sub

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
MessageBox.Show(ComboBox1.Text)
End Sub

演示窗体如下:



学习更多vb.net知识,请参看 vb.net 教程 目录
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: