Solved

Create command buttons during run time

Posted on 2014-12-08
4
94 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 32

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

15 Experts available now in Live!

Get 1:1 Help Now