原创 VB.NET下遍历TextBox组件的方法

2008-11-14 19:14 5603 8 8 分类: 软件与OS

59d3f42d-cd7b-4bdc-aefd-4d0fa3432233.gif


 


    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim ch As Char = e.KeyChar
        If Not Regex.IsMatch(ch.ToString(), "^[\b\r0-9a-fA-F]$") Then
            e.KeyChar = "" '放弃输入的非法字符
            MessageBox.Show("请正确输入16进制数!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            e.KeyChar = Char.ToUpper(ch) '强制转换为大写字母,放过退格和回车符
        End If
    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim Addr As UInt16
        Dim Val As Byte
        Dim Index As Integer
        Try
            Addr = Convert.ToUInt16(ComboBox1.Text, 16) + 1 '取状态口地址
            Val = WinIoPort(Addr) '读出并口控制口数据
            TextBox26.Text = Val.ToString("X02") '显示字节数据
        Catch ex As Exception '异常捕捉
            MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        For Each c As Control In GroupBox2.Controls '遍历GroupBox2上的控件
            If TypeOf (c) Is TextBox Then '只遍历TextBox类型的控件
                If c.TabIndex <> TextBox26.TabIndex Then '排除TextBox26控件
                    Index = TextBox10.TabIndex - c.TabIndex '计算相对位D0~D7
                    c.Text = ((Val And (1 << Index)) >> Index).ToString("0") '填入数据位
                End If
            End If
        Next
    End Sub


    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim Addr As UInt16
        Dim Val As Byte
        Dim Index As Integer
        Try
            Addr = Convert.ToUInt16(ComboBox1.Text, 16) + 2 '取控制口地址
            Val = WinIoPort(Addr) '读出并口控制口数据
        Catch ex As Exception '异常捕捉
            MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        For Each c As Control In GroupBox3.Controls '遍历GroupBox3上的控件
            If TypeOf (c) Is TextBox Then '只遍历TextBox类型的控件
                Index = TextBox18.TabIndex - c.TabIndex '计算相对位D0~D7
                c.Text = ((Val And (1 << Index)) >> Index).ToString("0") '填入数据位
            End If
        Next
    End Sub


    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim Addr As UInt16
        Dim Val As Byte = 0
        Dim Index As Integer
        Try
            Addr = Convert.ToUInt16(ComboBox1.Text, 16) + 2 '取控制口地址
            For Each c As Control In GroupBox3.Controls '遍历GroupBox3上的控件
                If TypeOf (c) Is TextBox Then '只遍历TextBox类型的控件
                    Index = TextBox18.TabIndex - c.TabIndex '计算相对位D0~D7
                    If c.Text = "1" Then
                        Val = Val Or (1 << Index) '组装数据位
                    End If
                End If
            Next
            WinIoPort(Addr) = Val '写入并口字节数据
        Catch ex As Exception '异常捕捉
            MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

文章评论0条评论)

登录后参与讨论
我要评论
0
8
关闭 站长推荐上一条 /2 下一条