vb.net动态生成控件并排版
来源:互联网 发布:广东省干部网络学校 编辑:程序博客网 时间:2024/05/16 01:39
''' <summary>
''' 动态生成控件并调整控件位置;
''' </summary>
Public Sub CreateControl(ByVal TextBoxTagContent As Dictionary(Of String, Integer), ByVal TextBoxType As List(Of Boolean))
Dim keyvalue As KeyValuePair(Of String, Integer)
TextBoxIndex.Clear()
'生成LabelBox和Textbox
'文本框区分为数字和文本两种
If TextBoxType.Count = TextBoxNum Then
For i As Integer = 0 To TextBoxNum - 1
Me.Controls.Add(New Label())
Me.Controls.Add(New TextBox())
Next
For j As Integer = 1 To TextBoxType.Count
If TextBoxType.Item(j - 1) = True Then
'添加默认值
If CType(Me.Controls.Item(2 * j - 1), TextBox).Text = "" OrElse IsNumeric(CType(Me.Controls.Item(2 * j - 1), TextBox).Text) = False Then
CType(Me.Controls.Item(2 * j - 1), TextBox).Text = "1.00"
End If
'是数字文本框时时添加事件处理
AddHandler CType(Me.Controls.Item(2 * j - 1), TextBox).LostFocus, AddressOf ActiveEvent
End If
Next
End If
'设置Label的text属性,且设置textboxtag属性;
If TextBoxTagContent.Count > 0 And TextBoxTagContent.Count = TextBoxNum Then
Dim L As Integer = 0
For Each keyvalue In TextBoxTagContent
Me.Controls.Item(L).Text = keyvalue.Key
Me.Controls.Item(L).BackColor = LabelBackColor
Me.Controls.Item(L).Tag = "NoTag"
Me.Controls.Item(L).Width = LabelWidth
Me.Controls.Item(L).Height = LabelHeight
L = L + 1
Me.Controls.Item(L).Tag = keyvalue.Value
'便于根据参数编号取出文本框中的值
TextBoxIndex.Add(keyvalue.Value, L)
Me.Controls.Item(L).Width = TextBoxWidth
Me.Controls.Item(L).Height = TextBoxHeight
L = L + 1
Next
End If
'定义控件的位置
'Dim TotalWidth As Integer = me.Width
'Dim TotalHeight As Integer = me.Height
Dim current As New Point()
'控件上下左右之间的间隔固定为6;每四个控件一行,开始给控件定位
Dim Columns As Integer = 1
Dim Rows As Integer = 0
'textbox '定义第一个控件的位置
current.X = JZ_X
current.Y = JZ_Y
Me.Controls.Item(0).Location = current
For m As Integer = 1 To (TextBoxNum * 2) - 1
If Columns < PerRowTS * 2 Then
current.X = Me.Controls.Item(m - 1).Location.X + 6 + (Me.Controls.Item(m - 1).Width)
current.Y = Me.Controls.Item(m - 1).Location.Y
Me.Controls.Item(m).Location = current
Columns = Columns + 1
Else
Rows = Rows + 1
current.X = JZ_X
current.Y = JZ_Y + 6 * Rows + Me.Controls.Item(0).Height * Rows
Me.Controls.Item(m).Location = current
Columns = 1
End If
Next
'调整面板高度
ActiveGb()
'enter转换为Tab
TextBox_init()
End Sub
''' <summary>
''' 数字文本框输入不是数字时,失去焦点后变为0
''' </summary>
Public Sub ActiveEvent(ByVal sender As Object, ByVal e As System.EventArgs)
If CType(sender, TextBox).Text = "" Then
CType(sender, TextBox).Text = "1.00"
ElseIf IsNumeric(CType(sender, TextBox).Text) = False Then
CType(sender, TextBox).Text = "1.00"
ElseIf Int(CType(sender, TextBox).Text) < 0 Then
CType(sender, TextBox).Text = "1.00"
Else
'若是全角数字转化为半角数字
Dim c As Char() = Trim(CType(sender, TextBox).Text).ToCharArray()
For i As Integer = 0 To c.Length - 1
Dim b As Byte() = System.Text.Encoding.Unicode.GetBytes(c, i, 1)
If b.Length = 2 Then
If b(1) = 255 Then
b(0) = CType((b(0) + 32), Byte)
b(1) = 0
c(i) = System.Text.Encoding.Unicode.GetChars(b)(0)
End If
End If
Next
Dim returnString As String = New String(c)
CType(sender, TextBox).Text = returnString
End If
End Sub
''' <summary>
''' 文本框的Enter转为Tab
''' </summary>
''' 按钮添加事件
Private Sub TextBox_init()
Dim oneTextBox As Windows.Forms.TextBox
For i As Integer = 0 To Me.Controls.Count - 1
If Me.Controls.Item(i).GetType.Name = "TextBox" Then
oneTextBox = Me.Controls.Item(i)
RemoveHandler oneTextBox.KeyPress, AddressOf Check_PTT_btn
AddHandler oneTextBox.KeyPress, AddressOf Check_PTT_btn
RemoveHandler oneTextBox.GotFocus, AddressOf Check_PTT_Focus
AddHandler oneTextBox.GotFocus, AddressOf Check_PTT_Focus
End If
Next
End Sub
''' <summary>
''' 用tab()代替回车
''' </summary>
Public Sub Check_PTT_btn(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
'用tab()代替回车
Dim oneTextBox As System.Windows.Forms.TextBox = sender
If e.KeyChar = Chr(13) Then
If oneTextBox.Text.Trim() <> "" Then
e.Handled = True
keybd_event(9, 0, 0, 0)
Else
oneTextBox.Focus()
End If
End If
End Sub
''' 动态生成控件并调整控件位置;
''' </summary>
Public Sub CreateControl(ByVal TextBoxTagContent As Dictionary(Of String, Integer), ByVal TextBoxType As List(Of Boolean))
Dim keyvalue As KeyValuePair(Of String, Integer)
TextBoxIndex.Clear()
'生成LabelBox和Textbox
'文本框区分为数字和文本两种
If TextBoxType.Count = TextBoxNum Then
For i As Integer = 0 To TextBoxNum - 1
Me.Controls.Add(New Label())
Me.Controls.Add(New TextBox())
Next
For j As Integer = 1 To TextBoxType.Count
If TextBoxType.Item(j - 1) = True Then
'添加默认值
If CType(Me.Controls.Item(2 * j - 1), TextBox).Text = "" OrElse IsNumeric(CType(Me.Controls.Item(2 * j - 1), TextBox).Text) = False Then
CType(Me.Controls.Item(2 * j - 1), TextBox).Text = "1.00"
End If
'是数字文本框时时添加事件处理
AddHandler CType(Me.Controls.Item(2 * j - 1), TextBox).LostFocus, AddressOf ActiveEvent
End If
Next
End If
'设置Label的text属性,且设置textboxtag属性;
If TextBoxTagContent.Count > 0 And TextBoxTagContent.Count = TextBoxNum Then
Dim L As Integer = 0
For Each keyvalue In TextBoxTagContent
Me.Controls.Item(L).Text = keyvalue.Key
Me.Controls.Item(L).BackColor = LabelBackColor
Me.Controls.Item(L).Tag = "NoTag"
Me.Controls.Item(L).Width = LabelWidth
Me.Controls.Item(L).Height = LabelHeight
L = L + 1
Me.Controls.Item(L).Tag = keyvalue.Value
'便于根据参数编号取出文本框中的值
TextBoxIndex.Add(keyvalue.Value, L)
Me.Controls.Item(L).Width = TextBoxWidth
Me.Controls.Item(L).Height = TextBoxHeight
L = L + 1
Next
End If
'定义控件的位置
'Dim TotalWidth As Integer = me.Width
'Dim TotalHeight As Integer = me.Height
Dim current As New Point()
'控件上下左右之间的间隔固定为6;每四个控件一行,开始给控件定位
Dim Columns As Integer = 1
Dim Rows As Integer = 0
'textbox '定义第一个控件的位置
current.X = JZ_X
current.Y = JZ_Y
Me.Controls.Item(0).Location = current
For m As Integer = 1 To (TextBoxNum * 2) - 1
If Columns < PerRowTS * 2 Then
current.X = Me.Controls.Item(m - 1).Location.X + 6 + (Me.Controls.Item(m - 1).Width)
current.Y = Me.Controls.Item(m - 1).Location.Y
Me.Controls.Item(m).Location = current
Columns = Columns + 1
Else
Rows = Rows + 1
current.X = JZ_X
current.Y = JZ_Y + 6 * Rows + Me.Controls.Item(0).Height * Rows
Me.Controls.Item(m).Location = current
Columns = 1
End If
Next
'调整面板高度
ActiveGb()
'enter转换为Tab
TextBox_init()
End Sub
''' <summary>
''' 数字文本框输入不是数字时,失去焦点后变为0
''' </summary>
Public Sub ActiveEvent(ByVal sender As Object, ByVal e As System.EventArgs)
If CType(sender, TextBox).Text = "" Then
CType(sender, TextBox).Text = "1.00"
ElseIf IsNumeric(CType(sender, TextBox).Text) = False Then
CType(sender, TextBox).Text = "1.00"
ElseIf Int(CType(sender, TextBox).Text) < 0 Then
CType(sender, TextBox).Text = "1.00"
Else
'若是全角数字转化为半角数字
Dim c As Char() = Trim(CType(sender, TextBox).Text).ToCharArray()
For i As Integer = 0 To c.Length - 1
Dim b As Byte() = System.Text.Encoding.Unicode.GetBytes(c, i, 1)
If b.Length = 2 Then
If b(1) = 255 Then
b(0) = CType((b(0) + 32), Byte)
b(1) = 0
c(i) = System.Text.Encoding.Unicode.GetChars(b)(0)
End If
End If
Next
Dim returnString As String = New String(c)
CType(sender, TextBox).Text = returnString
End If
End Sub
''' <summary>
''' 文本框的Enter转为Tab
''' </summary>
''' 按钮添加事件
Private Sub TextBox_init()
Dim oneTextBox As Windows.Forms.TextBox
For i As Integer = 0 To Me.Controls.Count - 1
If Me.Controls.Item(i).GetType.Name = "TextBox" Then
oneTextBox = Me.Controls.Item(i)
RemoveHandler oneTextBox.KeyPress, AddressOf Check_PTT_btn
AddHandler oneTextBox.KeyPress, AddressOf Check_PTT_btn
RemoveHandler oneTextBox.GotFocus, AddressOf Check_PTT_Focus
AddHandler oneTextBox.GotFocus, AddressOf Check_PTT_Focus
End If
Next
End Sub
''' <summary>
''' 用tab()代替回车
''' </summary>
Public Sub Check_PTT_btn(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
'用tab()代替回车
Dim oneTextBox As System.Windows.Forms.TextBox = sender
If e.KeyChar = Chr(13) Then
If oneTextBox.Text.Trim() <> "" Then
e.Handled = True
keybd_event(9, 0, 0, 0)
Else
oneTextBox.Focus()
End If
End If
End Sub
- vb.net动态生成控件并排版
- VB 动态生成控件
- 动态生成控件-绑定事件(VB.NET/C#)
- asp.net动态生成控件,并为控件绑定事件
- vb.net动态控件数组
- 如何动态生成页面控件,并取到值?(.net)
- [VB.NET]控件:动态增加,动态创建?
- 动态生成控件并定位
- Silverlight 在Grid中动态增加控件并排版
- 动态生成asp.net控件
- 动态生成asp.net控件
- 动态生成asp.net控件
- .net中动态生成控件
- VB.NET中动态添加控件数组
- VB.NET实现动态加载控件数组
- [VB.NET]动态加载自定义控件!!
- 【原创】vb.net 动态控件 事件添加
- vb.net winform动态生成菜单(源码)
- c++学习路线
- oracle批量修改表空间
- debian改crontab的编辑器
- Chrome浏览器稳定版第16版发布,增加多用户同步功能
- 数字证书简介
- vb.net动态生成控件并排版
- TStringList的delphi常见用法,BCD可借鉴
- 两个例子分析 9章课后选择7题
- 在浏览器中通过Javascript执行本地应用程序
- Xercesc C++ 学习笔记
- 编译android内核
- 读取JAVA系统属性
- Mysql中的数据用 json_decode 解析返回null的一则处理方法
- windows平台上使用jnative注意事项