Create event at run time

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I am new in VB .NET and would like to ask a question
I've create 4 buttons at run time, let's say MyButton1, MyButton2, MyButton3 and MyButton4
Also I want to create a click event for each of them. How can I do it
After the process, the 4 buttons and their events must be remove
Thanks for helping
 
* "=?Utf-8?B?Sm9hY2hpbSBKZW4=?= said:
I am new in VB .NET and would like to ask a question.
I've create 4 buttons at run time, let's say MyButton1, MyButton2, MyButton3 and MyButton4.
Also I want to create a click event for each of them. How can I do it ?
After the process, the 4 buttons and their events must be remove.

Have a look at 'AddHandler' and 'RemoveHandler':

\\\
Dim btn As New Button()
AddHandler btn.Click, AddressOf Me.btn_Click
Me.Controls.Add(btn)
///

Shared Event handler:

\\\
Private Sub btn_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
)
MsgBox("Clicked!")
End Sub
///
 
Hi Joachim,

A little addition to Herfried,

If a button is direct placed on a form and you want to remove it

\\\
me.controls.remove(button1)
///

I hope this helps,

Cor
 
Thanks for your replys.

Here under is my code in creating 4 buttons.
But actually, the 4 buttons are not fix. It can be from 5 to 12 depend on the input.
So, should I make minimum 5 and maximum 12 click events ?
After I remove the buttons and the events from controls, the code still there, right ?

Thanks again !


----------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, j As Short
Dim x As String
j = 20
For i = 1 To 4
x = "Knop" + Convert.ToString(i)
Adding(j, x)
j += 80
Next
End Sub
Public Sub Adding(ByVal posi As Short, ByVal nama As String)
Dim knop As New Button
knop.Text = nama
knop.Location = New Point(posi, 120)
knop.Name = nama
Me.Controls.Add(knop)
End Sub
------------------------------------------------
 
Hi Joachim,

Look what this sample can do for you, (I have sended it yesterday to Will
but it is no problem to send it again) Think that when you want to do
something with the button as remove you have to place the button global and
not inside the sample I send you.

and than the remove can be something as

me.controls.remove(mybutton(x))

I have more solutions to fix this problem, but this is for me the easiest
one accoording to your own problem to show you.

I hope this helps?

Cor

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim start As Integer = 4
Dim top As Integer = 25
Dim i As Integer
Dim nowdate As DateTime = DateTime.Now
Dim mybutton(System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month)) As Button
For i = 0 To System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month) - 1
mybutton(i) = New Button
mybutton(i).TextAlign = ContentAlignment.MiddleCenter
mybutton(i).Width = 40
mybutton(i).Height = 20
mybutton(i).FlatStyle = FlatStyle.Flat
mybutton(i).BackColor = Drawing.Color.AntiqueWhite
mybutton(i).Location = New System.Drawing.Point(start, top)
mybutton(i).Text = (i + 1).ToString
mybutton(i).Cursor = Cursors.Hand
Me.Controls.Add(mybutton(i))
AddHandler mybutton(i).Click, AddressOf mybutton_Click
AddHandler mybutton(i).MouseHover, AddressOf mybutton_Hoover
AddHandler mybutton(i).MouseLeave, AddressOf mybutton_Leave
start = start + 40
If (i + 1) Mod 5 = 0 Then
top = top + 20
start = 4
End If
Next
End Sub
Private Sub mybutton_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
MessageBox.Show("The day is: " & thisbutton.Text)
End Sub
Private Sub mybutton_Hoover _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AliceBlue
End Sub
Private Sub mybutton_Leave _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AntiqueWhite
End Sub
///
 
Thanks Cor,
The code works perfect, but I have another problem.
How to Remove the handler which I have added with AddHandler ?
I have tried to do like this :
RemoveHandler mybutton(i).Click(), AddressOf mybutton_Click
but I got an error.

Regards,
JJ
 
Back
Top