?
Solved

Creating Controls Dymanically

Posted on 2009-04-16
10
Medium Priority
?
194 Views
Last Modified: 2012-05-06
Ok how do I set a reference to a control that i create dynamically. Basically I create some a bunch of button dynamically on a screen. I have them populated on the screen how can use button1 so that if button 1 is clicked it handles some action. If button 2 is clicked it handles a different action and so on.
0
Comment
Question by:cmdolcet
  • 5
  • 4
10 Comments
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24156963
Use AddHandler....

    AddHandler btn.Click, AddressOf Button_Click

....where 'btn' is you button, and 'Button_Click' is the routine you wish to run when the user clicks the button. The routine needs the same signature as the Click event of the Button.Click event, so it should look something like below.

More info available here -> http://msdn.microsoft.com/en-us/library/7taxzxka(VS.71).aspx

Wayne
Private Sub Button_Click(ByVal sender As Object, e As System.EventArgs)
    'your code
End Sub

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24156975
You have to add event handlers to the particular events you want functionality for:
Dim b1 As New Button
 
AddHandler b1.Click, AddressOf b1_Click
 
'Define the handler function b1_Click
 
Public Sub b1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Do something
End Sub

Open in new window

0
 

Author Comment

by:cmdolcet
ID: 24157089
how do i fit it in here?
 'creates the active buttons
        For intloop = 0 To ActiveCOM.Count - 1
            positionindex = 1
            btnbuttons = New System.Windows.Forms.Button
            If intloop = 0 Then
                intx = 460
                inty = 85
            End If
            With btnbuttons
                .Name = "btnbuttons" & positionindex
                .Size = New System.Drawing.Size(100, 30)
                .Location = New System.Drawing.Point(intx, inty)
                .Text = "View Information"
                .Visible = True
            End With
 
            Me.Controls.Add(btnbuttons)
            inty = inty + 50
            positionindex = positionindex + 1
        Next

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24157103
       For intloop = 0 To ActiveCOM.Count - 1
            positionindex = 1
            btnbuttons = New System.Windows.Forms.Button
            If intloop = 0 Then
                intx = 460
                inty = 85
            End If
            With btnbuttons
                .Name = "btnbuttons" & positionindex
                .Size = New System.Drawing.Size(100, 30)
                .Location = New System.Drawing.Point(intx, inty)
                .Text = "View Information"
                .Visible = True
            End With
            AddHandler btnbuttons.Click, AddressOf Button_Click
            Me.Controls.Add(btnbuttons)
            inty = inty + 50
            positionindex = positionindex + 1
        Next
0
 

Author Comment

by:cmdolcet
ID: 24157306
its giving me an error on the Button_click saying it not declared. So if i understand it correctly i will be able to reference the  each button to do a separate action?
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24157334
As I said in my first comment....

    "'Button_Click' is the routine you wish to run when the user clicks the button"

Wayne
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24157376
...and, yes, you can assign a different routine to each button. Alternatively, you can use the one routine, and dermine what action to run depending on the calling controls name....

    Private Sub Button_Click(ByVal sender As Object, e As System.EventArgs)
        Select Case CType(sender, Button).Name
            Case "btnbuttons1"
                'code to run when the user clicks btnbuttons1
            Case "btnbuttons2"
                'code to run when the user clicks btnbuttons2
            Case "btnbuttons3"
                'code to run when the user clicks btnbuttons3
            'etc
    End Sub

0
 

Author Comment

by:cmdolcet
ID: 24157392
what do I need to do to solve this error?
0
 

Author Comment

by:cmdolcet
ID: 24157406
NM got it....sorry
0
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 24157409
Add a routine called Button_Click, or change "Button_Click" to the routine you wish to call.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Integration Management Part 2
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Suggested Courses

621 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