473,473 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Create event at run time

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

Nov 20 '05 #1
6 1245
* "=?Utf-8?B?Sm9hY2hpbSBKZW4=?=" <an*******@discussions.microsoft.com> scripsit:
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
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
Cor
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
After the process, the 4 buttons and their events must be remove.
Thanks for helping.

Nov 20 '05 #3
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
------------------------------------------------
Nov 20 '05 #4
Cor
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
///

Nov 20 '05 #5
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
Nov 20 '05 #6
Thanks Cor,
Your code does work.

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sean Byrne | last post by:
We have a Microsoft Access 2000 database consisting of 20 tables covering 20 different events. In each table, there are 3 Team members, a date of the event and several unique fields for the event,...
3
by: leon | last post by:
hello friends, i am writing a page aspx and creating controls dinamicaly and then i must to create for each control the events as well. Anybody to know how????? happy day lion
3
by: RSB | last post by:
Hi Every one Having tuff time creating web controls Dynamically. All i am trying to do is read a table and generate a list of ASP TEXT Box. So how do i create this Control dynamically and where...
2
by: Emil | last post by:
Hello, Two days ago I tried to create my first asp project but when I tried to create a new asp project I got the following error message: 'The web server reported the following error when...
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
3
by: MarcJoseph | last post by:
I have a database that is shared my multiple users who enter and update records on a weekly basis. Is there a way I can add a field to my main data table that will automatically generate the...
11
by: cmdolcet69 | last post by:
I have the code below that will evaluate a string of characters. If its less then 12 long it will trigger a message box if its equal it will save the information and close the form. I have this...
3
by: Alexnb | last post by:
I am wondering what is the best way to create a timer, like an alarm, once it reaches a time, it triggers an event. I have a way of doing this but it seems like it isn't good at all. If it helps at...
1
by: Joe, G.I. | last post by:
I'm new to boost and what I'm doing seems very simple, but I can't get it. I want to store 3 events onto a priority_queue and have them execute n seconds from the time they were created, n is a...
10
Plater
by: Plater | last post by:
I'm a bit boggled by this since I am rather new to using reflection. I want to attach an event handler via Reflection (which I can do), but I want to NOT have to know the exact event delegate...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.