vb.net中的运行时按钮数组 [英] runtime button array in vb.net
问题描述
大家好,
我需要你的帮助来提高我的发展技能。
我有来自数据库的数据。当我运行表单时,我需要创建一个名称存储在db数据中的按钮。
考试 - 我存储了1,2,3,4,5
当我跑步时,我需要用这个名字创建一个按钮;我也需要进行分组。
http://www.ezeeburrp.com/ images / screenshots / qsr.jpg [ ^ ]
请浏览此链接。在那里,您将了解我的要求是什么。
在那里你可以看到按钮是按组排列的。
谢谢&问候,
reji
Hi Guys,
I need your help to improve my developing skills.
I have data from database. When I run the form I need to create a button with name which is stored in db data.
Examble- I stored 1,2,3,4,5
When I run, I need to create a button in this name; also I need to make grouping.
http://www.ezeeburrp.com/images/screenshots/qsr.jpg[^]
Please go through this link. There you will get an idea what is my requirement.
There you can see button is arranged group-wise.
Thanks & regards,
reji
推荐答案
除了Maciej Los提供的答案之外,你可以通过将按钮添加到FlowLayoutPanel来简化分组,而不是Forms ControlCollection,然后根据您的需要自动排列控件。
Additionally to the answer provided by Maciej Los you can simplify the grouping by adding the buttons to a FlowLayoutPanel instead of the Forms ControlCollection, which will then automatically arrange the controls according to your needs.
FlowLayoutPanel1.Controls.Add(btn)
看看下面的代码。这只是一个想法。
Have a look at below code. This is only an idea.
Public Class Form1
'to catch Click event ;)
WithEvents oBtn As Button
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim oDt As DataTable = Nothing
Dim i As Integer = 0, btn As Button = Nothing
'GetData() function reurns DataTable
oDt = GetData()
'enumerate through the collection of rows
For i = 0 To oDt.Rows.Count - 1
'create new button
btn = New Button
With btn
.Parent = Me
.Left = 8
.Top = i * 24 + 8
.Text = oDt.Rows(i).Item("MyIntegerColumn").ToString
.Name = "Button" & i.ToString
AddHandler btn.Click, AddressOf oBtn_Click
End With
Next
End Sub
'change to your needs ;)
Private Function GetData() As DataTable
Dim oDT As DataTable = Nothing, oRow As DataRow = Nothing
Dim i As Integer = 0
Try
oDT = New DataTable("myIntegerData")
oDT.Columns.Add(New DataColumn("MyIntegerColumn", GetType(System.Int32)))
For i = 1 To 10
oRow = oDT.NewRow()
oRow.Item("MyIntegerColumn") = i
oDT.Rows.Add(oRow)
Next
Catch ex As Data.DataException
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
End Try
Return oDT
End Function
Private Sub oBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles oBtn.Click
MsgBox(sender.Name, MsgBoxStyle.Information, "Message")
End Sub
End Class
这篇关于vb.net中的运行时按钮数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!