Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create command buttons during run time

Posted on 2014-12-08
4
Medium Priority
?
122 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 35

Accepted Solution

by:
it_saige earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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 …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

916 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