Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-27
5
Medium Priority
?
336 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
[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
  • 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
 
LVL 1

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 1200 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
 
LVL 1

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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