Solved

Create command buttons during run time

Posted on 2014-12-08
4
115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 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