Solved

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

Posted on 2010-11-27
5
324 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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

19 Experts available now in Live!

Get 1:1 Help Now