creating addhandlers for runtime created labels

lukisf

Member
Joined
Apr 3, 2007
Messages
13
Programming Experience
1-3
I am currently working on my thesis and i came up with a problem. basically, my thesis is a simulation of an arena where you can change the positioning (by dragging) of the seating etc. Now i used labels as seats. I'm attaching the code i used to enable dragging
VB.NET:
[LEFT][COLOR=#0000ff]Dim[/COLOR] dragging [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Boolean[/COLOR][COLOR=#0000ff]Dim[/COLOR] startx, starty [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Integer[/COLOR][COLOR=#0000ff]Private[/COLOR] [COLOR=#0000ff]Sub[/COLOR] [/LEFT]
 
[LEFT]Form1_Load([COLOR=#0000ff]ByVal[/COLOR] sender [COLOR=#0000ff]As[/COLOR] System.Object, [COLOR=#0000ff]ByVal[/COLOR] e [COLOR=#0000ff]As[/COLOR] System.EventArgs) [COLOR=#0000ff]Handles[/COLOR] [COLOR=#0000ff]MyBase[/COLOR].Load[/LEFT]
 
[LEFT][COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Sub[/COLOR] [/LEFT]
 
[LEFT][COLOR=#0000ff]Private[/COLOR] [COLOR=#0000ff]Sub[/COLOR] lblseat_MouseDown([COLOR=#0000ff]ByVal[/COLOR] sender [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Object[/COLOR], [COLOR=#0000ff]ByVal[/COLOR] e [COLOR=#0000ff]As[/COLOR] System.Windows.Forms.MouseEventArgs) [COLOR=#0000ff]Handles[/COLOR] lblseat.MouseDown[/LEFT]
 
[LEFT]dragging = [COLOR=#0000ff]True[/COLOR]
startx = e.X
starty = e.Y
[COLOR=#0000ff]End[/COLOR] Sub[/LEFT]
 
[LEFT][COLOR=#0000ff]Private[/COLOR] [COLOR=#0000ff]Sub[/COLOR] lblseat_MouseMove([COLOR=#0000ff]ByVal[/COLOR] sender [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Object[/COLOR], [COLOR=#0000ff]ByVal[/COLOR] e [COLOR=#0000ff]As[/COLOR] System.Windows.Forms.MouseEventArgs) [COLOR=#0000ff]Handles[/COLOR] lblseat.MouseMove[/LEFT]
 
[LEFT][COLOR=#0000ff]If[/COLOR] dragging = [COLOR=#0000ff]True[/COLOR] [COLOR=#0000ff]Then[/COLOR]
lblseat.Left = (lblseat.Left + e.X) - startx
lblseat.Top = (lblseat.Top + e.Y) - starty
[COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]If[/COLOR][/LEFT]
 
[LEFT][COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Sub[/COLOR] [/LEFT]
 
[LEFT][COLOR=#0000ff]Private[/COLOR] [COLOR=#0000ff]Sub[/COLOR] lblseat_MouseUp([COLOR=#0000ff]ByVal[/COLOR] sender [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Object[/COLOR], [COLOR=#0000ff]ByVal[/COLOR] e [COLOR=#0000ff]As[/COLOR] System.Windows.Forms.MouseEventArgs) [COLOR=#0000ff]Handles[/COLOR] lblseat.MouseUp[/LEFT]
 
[LEFT]dragging = [COLOR=#0000ff]False[/COLOR][/LEFT]
 
[LEFT][COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Sub[/COLOR][/LEFT]

Now i need to create labels during runtime. I know how to do that with the me.labels.add(variable).
I need help with assigning the dragging mode to the labels. I presume i have to use addhandler but i have no idea with regards to coding....

any help very much appreciated.

thanks
Luke
 
VB.NET:
dim l as new label
addhandler l.mousedown, addressof lblseat_MouseDown
Here is an alternative approach where you can drag any control on form without using any of the controls event handlers, all done with only the keydown and mousemove events of the form: http://www.vbdotnetforums.com/showthread.php?t=16935 (post 4)
 
In the event handler the 'sender' parameter (Object) is the control that raised the event. Event handler:
VB.NET:
Dim l As Label = DirectCast(sender, Label)
 
Back
Top