Solved

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

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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