Solved

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

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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

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 …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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