VB.NET编程的几个小技巧

VB.NET编程的几个小技巧

1、遍历窗口里所有控件
当控件都放在容器里时,遍历窗口,只能检索到容器控件,下面代码就是检索窗口下所有的控件,检索二级控件(父控件和子控件):

            For Each AllControl As Control In Controls '检索所有控件,包括容器
                Console.WriteLine(AllControl.Name) '显示控件名称
                '根据控件,自行编写代码
                For Each AllSubControl As Control In AllControl.Controls '在容器组里检索所有子控件
                    Console.WriteLine(AllSubControl.Name) '显示子控件名称
                    '根据控件,自行编写代码
                Next
            Next

2、 多 TextBox 文本框共性编程技巧,只允许输入浮点数字,并具备复制、粘贴、剪切功能
当文本框自定义了 KeyPress 事件,只允许输入浮点数字,就失去了复制、粘贴、剪切功能,下面语句实现多个同类文本框只允许输入浮点数字,并具备复制、粘贴、剪切功能。 Handles 句柄指向所有具备共同需求的文本框。

    ''' 按下 Ctrl+C Ctrl+V Ctrl+X 实现拷贝、粘贴、剪切
    ''' 控件对象
    ''' 事件基类
    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown,
           TextBox2.KeyDown, TextBox3.KeyDown, TextBox4.KeyDown, TextBox5.KeyDown,
           TextBox6.KeyDown, TextBox7.KeyDown, TextBox8.KeyDown, TextBox9.KeyDown,
           TextBox10.KeyDown, TextBox11.KeyDown, TextBox12.KeyDown, TextBox13.KeyDown,
           TextBox14.KeyDown, TextBox15.KeyDown, TextBox16.KeyDown, TextBox17.KeyDown,
           TextBox22.KeyDown, TextBox23.KeyDown, TextBox24.KeyDown, TextBox25.KeyDown,
           TextBox26.KeyDown, TextBox27.KeyDown, TextBox29.KeyDown, TextBox30.KeyDown

        Dim TB As TextBox = CType(sender, Control)
        If e.KeyCode = Keys.C And e.Control Then 'Ctrl+C
             e.Handled = False
            TB.SelectAll() '选择所有
            TB.Copy() '拷贝
        ElseIf e.KeyCode = Keys.V And e.Control Then
             e.Handled = False
            TB.Clear()'清空
            TB.Paste()'粘贴
        ElseIf e.KeyCode = Keys.X And e.Control Then
             e.Handled = False
            TB.SelectAll()'选择所有
            TB.Cut()'剪切
        Else
            e.Handled = True
        End If
    End Sub
    ''' 文本框按任意键,只允许数字、小数点、回退键,且小数点只出现1次
    ''' 控件对象
    ''' 事件数据
    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress,
           TextBox2.KeyPress, TextBox3.KeyPress, TextBox4.KeyPress, TextBox5.KeyPress,
           TextBox6.KeyPress, TextBox7.KeyPress, TextBox8.KeyPress, TextBox9.KeyPress,
           TextBox10.KeyPress, TextBox11.KeyPress, TextBox12.KeyPress, TextBox13.KeyPress,
           TextBox14.KeyPress, TextBox15.KeyPress, TextBox16.KeyPress, TextBox17.KeyPress,
           TextBox22.KeyPress, TextBox23.KeyPress, TextBox24.KeyPress, TextBox25.KeyPress,
           TextBox26.KeyPress, TextBox27.KeyPress, TextBox29.KeyPress, TextBox30.KeyPress

        If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Or e.KeyChar = "." Then '只允许数字、退格键、小数点
            If e.KeyChar = "." And InStr(CType(sender, Control).Text, ".") > 0 Then '小数点只 1 个
                e.Handled = True  '事件已处理,忽略
            Else
                e.Handled = False '事件未处理,执行
            End If
        Else
            e.Handled = True    '事件已处理,忽略
        End If
    End Sub

你可能感兴趣的:(原创,VB,.net,c#,开发语言,VB.NET)