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


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 教程 目录

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...