vb.net动态生成控件并排版

本文详细介绍了如何使用代码动态生成指定数量的Label和TextBox控件,并进行位置调整,包括添加默认值、设置文本、定义控件布局及事件处理等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

''' <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
.该类的作用: 该类可以帮大家自动布局界面控件,不需要开发人员每个控件的设置属性,只需要调用方法,自动会设置该控件的布局,控件的宽度随着窗体的变化而变化,该方法调用很简单 二.原理:使用TableLayOutPanle的功能,然后设定里面每个控件的样式 三.使用方法: 1)首先在录入数据的地方用GroupBox或者Panle作为容器(目前里面配置了这2中数据信息用户可以在ParentControlHeader类中进行相应配置) 2)然后在该容器中加入TableLayOutPanle控件,设定行列(例如:设定6列,奇数列的宽度都是绝对值:例如100px ,偶数列的宽度都设定为33%) 3)大家可以把相应的控件放入到TableLayOutPanle的相应单元格子中,(奇数列是标题列,偶数列是输入列) 4)在Load事件中这样调用就OK了 TableFormat tf = new TableFormat(tableLayoutPanel1); //此方法可以适用于父级控件是GroupBox或者Panel,您也可一修改 ParentControlHeader类中的配置文件,加入新的值,或者是修改已经设定的值 tf.SetTableFormat(true, PControlType.GroupControl); 这样,大家不需要设定每个TableLayout控件中的子控件的任何属性,TableFormat类帮自动布局界面上的控件,且随着窗体的变化,控件是自动变化的,当然里面有些参数,是可以设定父级控件(GroupPanle/Panle的高度=里面行高(自动计算)+用户配置高度(目前配置了GroupBoxPanle) 详细可见Demo,代码注释写的比较详细,大家可以参考下. 谢谢..^_^.. (鼓励0资源分上传)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值