Solved

How to create Command Buttons during  Runtime on a Form1 by Clicking a Button1 on Form1

Posted on 2010-11-27
5
331 Views
Last Modified: 2012-06-27

I need help with the click event of a Button1 on a Form1 in a   Visual Basic Project   in Visual Studio 2008. I would like to be able to create three command Buttons (with Click Events) on Form1 by clicking Button1 on Form1.
The names of the three Command Buttons created during Runtime on Form1 should be Button2, Button3 and Button4.  Clicking Button2 should show a message box saying “Button2” and clicking Button3 should show a message box saying “Button3”.
The text property of Button2 should be “Button Two” and the text property of Button3 should be “Button Three”.

Many thanks for your help in anticipation.
0
Comment
Question by:FaheemAhmadGul
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Pryrates
ID: 34221483
you can easily create new buttons via code on a specific event.
But you need to keep in mind you just have to create the new buttons once and you need to "hook up" all events you need f.e. "click" via code.

Have a look at the sample code.
I wrote a method that generates buttons, if they are not already on the form and attaches an event handler called ClickButton.

Hope that helps
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CreateNewButton("two", "Button two", New Point(100, 100), New Size(New Point(100, 32)))
        CreateNewButton("three", "Button three", New Point(100, 150), New Size(New Point(100, 32)))
        CreateNewButton("four", "Button four", New Point(100, 200), New Size(New Point(100, 32)))
    End Sub

    Private Sub CreateNewButton(ByVal id As String, ByVal button_text As String, ByVal positon As Point, ByVal dimension As Size)

        If (Me.Controls.Find(id, True).Count = 0) Then
            Dim myButton As New Button()
            myButton.Text = button_text
            myButton.Name = id
            myButton.Left = positon.X
            myButton.Top = positon.Y
            myButton.Width = dimension.Width
            myButton.Height = dimension.Height
            AddHandler myButton.Click, AddressOf ClickHandler
            Me.Controls.Add(myButton)
            myButton.Show()
        End If
    End Sub

    Private Sub ClickHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim but As Button
        but = CType(sender, Button)

        MessageBox.Show(but.Text)
    End Sub
End Class

Open in new window

0
 

Author Comment

by:FaheemAhmadGul
ID: 34221578
Thank you for your response to my request for help. I have put the code you have provided in my project. I see one message in the error window which underlines the
 line 9 ie  (If (Me.Controls.Find(id, True).Count = 0) Then) and the error message says
Error      42      'Count' is not a member of 'System.Array'.      

I would appreciate some further guidance
0
 
LVL 13

Accepted Solution

by:
Pryrates earned 300 total points
ID: 34221596
Oh,
I assumed you build for .net framework 3.5.

In 2.0 you have to replace:
If (Me.Controls.Find(id, True).Count = 0) Then

with
If (Me.Controls.Find(id, True).Length = 0) Then


and you will be fine :)
0
 

Author Closing Comment

by:FaheemAhmadGul
ID: 34221717
Many thanks. I am very grateful. Regards
0
 
LVL 13

Expert Comment

by:Pryrates
ID: 34221773
you are welcome :)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Call windows 10 virtual keyboard from windows forms app 2 36
VB.Net - Get ColumnNames of SQL Table 4 12
Expression Evaluater 3 26
RLDC Reporting in Visual studio 11 16
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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