查看: 5820|回复: 0

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

[复制链接]
发表于 2009-4-3 00:12:19 | 显示全部楼层 |阅读模式
关键词: NET , TextBox , 遍历 , 组件


    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
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表