473,545 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
+ 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 1248
* "=?Utf-8?B?Sm9hY2hpbSB KZW4=?=" <an*******@disc ussions.microso ft.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.EventArg s _
)
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.rem ove(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(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
Dim i, j As Short
Dim x As String
j = 20
For i = 1 To 4
x = "Knop" + Convert.ToStrin g(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.rem ove(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(ByVa l sender As Object, _
ByVal e As System.EventArg s) 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.DaysI nMonth _
(nowdate.Year, nowdate.Month)) As Button
For i = 0 To System.DateTime .DaysInMonth _
(nowdate.Year, nowdate.Month) - 1
mybutton(i) = New Button
mybutton(i).Tex tAlign = ContentAlignmen t.MiddleCenter
mybutton(i).Wid th = 40
mybutton(i).Hei ght = 20
mybutton(i).Fla tStyle = FlatStyle.Flat
mybutton(i).Bac kColor = Drawing.Color.A ntiqueWhite
mybutton(i).Loc ation = New System.Drawing. Point(start, top)
mybutton(i).Tex t = (i + 1).ToString
mybutton(i).Cur sor = Cursors.Hand
Me.Controls.Add (mybutton(i))
AddHandler mybutton(i).Cli ck, AddressOf mybutton_Click
AddHandler mybutton(i).Mou seHover, AddressOf mybutton_Hoover
AddHandler mybutton(i).Mou seLeave, 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.EventArg s)
Dim thisbutton As Button = DirectCast(send er, Button)
MessageBox.Show ("The day is: " & thisbutton.Text )
End Sub
Private Sub mybutton_Hoover _
(ByVal sender As Object, ByVal e As System.EventArg s)
Dim thisbutton As Button = DirectCast(send er, Button)
thisbutton.Back Color = Drawing.Color.A liceBlue
End Sub
Private Sub mybutton_Leave _
(ByVal sender As Object, ByVal e As System.EventArg s)
Dim thisbutton As Button = DirectCast(send er, Button)
thisbutton.Back Color = Drawing.Color.A ntiqueWhite
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).Cli ck(), 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
1826
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, which vary from table to table. There is also another table that holds the team members' details. There is a one-to-many relation between the...
3
1791
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
2963
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 do i add the EventHandler to it. Thanks RSB
2
1676
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 attempting to create or open the Web project located at the following URL: 'http://localhost/WebApplication1'. 'HTTP/1.1 500 Internal Server Error'. I...
15
26490
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
6265
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 create date or last modification date for each record?
11
1602
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 currectly on a button event, how could i place this same logic say once i enter in my last character it will trigger the logic written in the...
3
1626
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 all I am using a Tkinter, but that probably doesn't mean much. The way I was doing it was using a while loop, and just saying while current time is...
1
2916
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 randomly generated amount of time between 0 and 10 seconds. // create container that holds function objects priority_queue<boost::function<void...
10
9551
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 signature. For an exmaple, I have a .DLL with the following: public delegate CustomEnum CustomEventHandler(Socket s, CustomClass c); public class...
0
7467
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7419
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7756
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5326
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1879
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 we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.