DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Fri, 20 Jul 2007 11:21:04 +0200,    posted on: microsoft.public.dotnet.languages.vb.controls        back       

Thread Index
  1    GAZ
          2    ClayB
          3    ClayB
          4    ClayB
          5    ClayB
          6    GAZ
          7    GAZ
          8    GAZ
          9    GAZ


Multiple AddHandler for multiple Controls on a form   
Hello all,

We have a bit of a problem. We have a form where we have to add controls 
dynamically during runtime. That part is quite simple. Now, we have to add a 
ButtonClick event for each of the controls. That's where it becoms tricky.

Here is the bit of code we use:

-------------------------------------------------------------------------------------
TextControl = New Asd.ControlLibrary.AsdTextControl

Me.TextControl.BackColor = System.Drawing.Color.Transparent
.....additional properties setup

If CheckStoredProcedure <> "" Then
    Me.TextControl.CheckStoredProcedure = CheckStoredProcedure
    Me.TextControl.ButtonBook = True
    Me.TextControl.ErrrorProvider = True
    Me.TextControl.CheckRequired = True
End If

AddHandler TextControl.ButtonBookClick, AddressOf TextControl_ButtonBook

Me.Controls.Add(TextControl)
--------------------------------------------------------------------------------------

Now, everything works fine and each control acts as a separate control, but 
the TextControl_ButtonBook fires only for the very last created control. I 
have tried adding 'sender' parameter to the procedure but the addressof 
won't accept it.

Any help and/or insight is much appreciated.

Thanks in advance.

GAZ
Date:Fri, 20 Jul 2007 11:21:04 +0200   Author:  

Re: Multiple AddHandler for multiple Controls on a form   
The code below works as expected for me. I create a Form , add a
button named Button1 to it and subscribe to its click event. Then in
the click handler I dynamically add another button every time Button1
is clicked.

Public Class Form1
    Dim testButton As Button
    Dim a As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
        Me.testButton = New Button()
        Me.testButton.Name = String.Format("test{0}", a)
        Me.testButton.Location = New Point(a * 100, 10)
        a += 1
        AddHandler testButton.Click, AddressOf Test_Click
        Me.Controls.Add(Me.testButton)
    End Sub

    Private Sub Test_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
        Dim c As Control = CType(sender, Control)
        MessageBox.Show(c.Name)
    End Sub
End Class


====================
Clay Burch
Syncfusion, Inc.
Date:Fri, 20 Jul 2007 02:53:58 -0700   Author:  

Re: Multiple AddHandler for multiple Controls on a form   
The code below works as expected for me. I create a Form , add a
button named Button1 to it and subscribe to its click event. Then in
the click handler I dynamically add another button every time Button1
is clicked.

Public Class Form1
    Dim testButton As Button
    Dim a As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
        Me.testButton = New Button()
        Me.testButton.Name = String.Format("test{0}", a)
        Me.testButton.Location = New Point(a * 100, 10)
        a += 1
        AddHandler testButton.Click, AddressOf Test_Click
        Me.Controls.Add(Me.testButton)
    End Sub

    Private Sub Test_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
        Dim c As Control = CType(sender, Control)
        MessageBox.Show(c.Name)
    End Sub
End Class


====================
Clay Burch
Syncfusion, Inc.
Date:Fri, 20 Jul 2007 02:53:58 -0700   Author:  

Re: Multiple AddHandler for multiple Controls on a form   
The code below works as expected for me. I create a Form , add a
button named Button1 to it and subscribe to its click event. Then in
the click handler I dynamically add another button every time Button1
is clicked.

Public Class Form1
    Dim testButton As Button
    Dim a As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
        Me.testButton = New Button()
        Me.testButton.Name = String.Format("test{0}", a)
        Me.testButton.Location = New Point(a * 100, 10)
        a += 1
        AddHandler testButton.Click, AddressOf Test_Click
        Me.Controls.Add(Me.testButton)
    End Sub

    Private Sub Test_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
        Dim c As Control = CType(sender, Control)
        MessageBox.Show(c.Name)
    End Sub
End Class


====================
Clay Burch
Syncfusion, Inc.
Date:Fri, 20 Jul 2007 02:53:58 -0700   Author:  

Re: Multiple AddHandler for multiple Controls on a form   
The code below works as expected for me. I create a Form , add a
button named Button1 to it and subscribe to its click event. Then in
the click handler I dynamically add another button every time Button1
is clicked.

Public Class Form1
    Dim testButton As Button
    Dim a As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
        Me.testButton = New Button()
        Me.testButton.Name = String.Format("test{0}", a)
        Me.testButton.Location = New Point(a * 100, 10)
        a += 1
        AddHandler testButton.Click, AddressOf Test_Click
        Me.Controls.Add(Me.testButton)
    End Sub

    Private Sub Test_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
        Dim c As Control = CType(sender, Control)
        MessageBox.Show(c.Name)
    End Sub
End Class


====================
Clay Burch
Syncfusion, Inc.
Date:Fri, 20 Jul 2007 02:53:58 -0700   Author:  

Re: Multiple AddHandler for multiple Controls on a form   
Thanks for the answer. Problem was not in the code, it was in the event 
declaration of the control. The event declaration was missing the ByVal 
sender as Object bit.

Thanks,

GAZ


"ClayB"  wrote in message 
news:1184925238.485138.24110@j4g2000prf.googlegroups.com...

> The code below works as expected for me. I create a Form , add a
> button named Button1 to it and subscribe to its click event. Then in
> the click handler I dynamically add another button every time Button1
> is clicked.
>
> Public Class Form1
>    Dim testButton As Button
>    Dim a As Integer = 0
>    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
> As System.EventArgs) Handles Button1.Click
>        Me.testButton = New Button()
>        Me.testButton.Name = String.Format("test{0}", a)
>        Me.testButton.Location = New Point(a * 100, 10)
>        a += 1
>        AddHandler testButton.Click, AddressOf Test_Click
>        Me.Controls.Add(Me.testButton)
>    End Sub
>
>    Private Sub Test_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
>        Dim c As Control = CType(sender, Control)
>        MessageBox.Show(c.Name)
>    End Sub
> End Class
>
>
> ====================
> Clay Burch
> Syncfusion, Inc.
> 
Date:Fri, 20 Jul 2007 12:06:57 +0200   Author:  

Re: Multiple AddHandler for multiple Controls on a form   
Thanks for the answer. Problem was not in the code, it was in the event 
declaration of the control. The event declaration was missing the ByVal 
sender as Object bit.

Thanks,

GAZ


"ClayB"  wrote in message 
news:1184925238.485138.24110@j4g2000prf.googlegroups.com...

> The code below works as expected for me. I create a Form , add a
> button named Button1 to it and subscribe to its click event. Then in
> the click handler I dynamically add another button every time Button1
> is clicked.
>
> Public Class Form1
>    Dim testButton As Button
>    Dim a As Integer = 0
>    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
> As System.EventArgs) Handles Button1.Click
>        Me.testButton = New Button()
>        Me.testButton.Name = String.Format("test{0}", a)
>        Me.testButton.Location = New Point(a * 100, 10)
>        a += 1
>        AddHandler testButton.Click, AddressOf Test_Click
>        Me.Controls.Add(Me.testButton)
>    End Sub
>
>    Private Sub Test_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
>        Dim c As Control = CType(sender, Control)
>        MessageBox.Show(c.Name)
>    End Sub
> End Class
>
>
> ====================
> Clay Burch
> Syncfusion, Inc.
> 
Date:Fri, 20 Jul 2007 12:06:57 +0200   Author:  

Re: Multiple AddHandler for multiple Controls on a form   
Thanks for the answer. Problem was not in the code, it was in the event 
declaration of the control. The event declaration was missing the ByVal 
sender as Object bit.

Thanks,

GAZ


"ClayB"  wrote in message 
news:1184925238.485138.24110@j4g2000prf.googlegroups.com...

> The code below works as expected for me. I create a Form , add a
> button named Button1 to it and subscribe to its click event. Then in
> the click handler I dynamically add another button every time Button1
> is clicked.
>
> Public Class Form1
>    Dim testButton As Button
>    Dim a As Integer = 0
>    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
> As System.EventArgs) Handles Button1.Click
>        Me.testButton = New Button()
>        Me.testButton.Name = String.Format("test{0}", a)
>        Me.testButton.Location = New Point(a * 100, 10)
>        a += 1
>        AddHandler testButton.Click, AddressOf Test_Click
>        Me.Controls.Add(Me.testButton)
>    End Sub
>
>    Private Sub Test_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
>        Dim c As Control = CType(sender, Control)
>        MessageBox.Show(c.Name)
>    End Sub
> End Class
>
>
> ====================
> Clay Burch
> Syncfusion, Inc.
> 
Date:Fri, 20 Jul 2007 12:06:57 +0200   Author:  

Re: Multiple AddHandler for multiple Controls on a form   
Thanks for the answer. Problem was not in the code, it was in the event 
declaration of the control. The event declaration was missing the ByVal 
sender as Object bit.

Thanks,

GAZ


"ClayB"  wrote in message 
news:1184925238.485138.24110@j4g2000prf.googlegroups.com...

> The code below works as expected for me. I create a Form , add a
> button named Button1 to it and subscribe to its click event. Then in
> the click handler I dynamically add another button every time Button1
> is clicked.
>
> Public Class Form1
>    Dim testButton As Button
>    Dim a As Integer = 0
>    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
> As System.EventArgs) Handles Button1.Click
>        Me.testButton = New Button()
>        Me.testButton.Name = String.Format("test{0}", a)
>        Me.testButton.Location = New Point(a * 100, 10)
>        a += 1
>        AddHandler testButton.Click, AddressOf Test_Click
>        Me.Controls.Add(Me.testButton)
>    End Sub
>
>    Private Sub Test_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
>        Dim c As Control = CType(sender, Control)
>        MessageBox.Show(c.Name)
>    End Sub
> End Class
>
>
> ====================
> Clay Burch
> Syncfusion, Inc.
> 
Date:Fri, 20 Jul 2007 12:06:57 +0200   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us