Solved

Create command buttons during run time

Posted on 2014-12-08
4
98 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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now