Solved

Create command buttons during run time

Posted on 2014-12-08
4
107 Views
Last Modified: 2014-12-08
Hi Experts,

How can I create group of command buttons dynamically  in a group box . For example I want to create 50 command buttons, 10 in each row and 5 in each column.

Thank you.
0
Comment
Question by:RadhaKrishnaKiJaya
4 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40487636
Dim btn As New Button
panel1.Add(btn)
btn.Name = "Save"
btn.Width = 120
btn.Height = 64
You need a panel control to add your button.
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40487704
Something like this:
Public Class Form1
	Private groupBox1 As GroupBox = New GroupBox()

	Private Sub OnLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
		Dim x As Integer = 3
		Dim y As Integer = 12
		For i As Integer = 1 To 50
			Dim button As New Button() With {.Location = New Point(x, y), .Name = String.Format("btnDynamic{0}", i), .Text = String.Format("Button {0}", i)}
			If i Mod 10 = 0 Then
				x = 3
				y += button.Height + 6
			Else
				x += button.Width + 6
			End If
			AddHandler button.Click, AddressOf OnClick
			groupBox1.Controls.Add(button)
		Next
		groupBox1.AutoSize = True
		groupBox1.Name = "gbDynamic"
		groupBox1.Text = "Dynamic Group Box"
		Me.Controls.Add(groupBox1)
		Me.Size = New Size(groupBox1.Width + 20, groupBox1.Height + 60)
	End Sub

	Private Sub OnClick(ByVal sender As Object, ByVal e As EventArgs)
		If TypeOf sender Is Button Then
			Dim btn As Button = DirectCast(sender, Button)
			MessageBox.Show(String.Format("Button - {0}; Name - {1} was clicked", btn.Text, btn.Name))
		End If
	End Sub
End Class

Open in new window


Produces the following output -Capture.JPG
And clicking on one of the buttons -Capture.JPG
-saige-
0
 
LVL 40
ID: 40487877
I would use a FlowLayoutPanel instead of a GroupBox. It has being designed exactly for the purpose that you asked for, and its big advantage over the GroupBox is that it will automatically place the controls for your, either in line or in columns, with specifications about the distance between the controls and a few other things defined in its properties.

You use code similar to the one suggested by it_sauge, but can forget agout the whole x,y stuff. Simply add the buttons to the FlowLayoutPanel instead of the GroupBox.
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40487932
Thank you for ur suggestion.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question