adding controls
-
hi,
i have a problem with adding control handler to the controls added runtime.
exactly i dont know how to do it.
in my code i create chackboxes runtime...
i want to add event handler to them.
so when i change chack state that event is called and executed.
code i created till now:
Code:
Dim posV As New Point(448, 36)
Dim posA As New Point(478, 36)
Dim posU As New Point(508, 36)
Dim posD As New Point(538, 36)
Dim CHKV As System.Windows.Forms.CheckBox
Dim CHKA As System.Windows.Forms.CheckBox
Dim CHKU As System.Windows.Forms.CheckBox
Dim CHKD As System.Windows.Forms.CheckBox
Private Sub create_chk(ByVal i As Integer)
Dim x As Integer = 1
While x <= i
CHKV = New System.Windows.Forms.CheckBox
CHKA = New System.Windows.Forms.CheckBox
CHKU = New System.Windows.Forms.CheckBox
CHKD = New System.Windows.Forms.CheckBox
CHKV.Size = New System.Drawing.Size(16, 16)
CHKV.Location = posV
CHKV.BackColor = System.Drawing.Color.DimGray
CHKV.CheckAlign = System.Drawing.ContentAlignment.MiddleCenter
CHKV.Location = posV
CHKV.Name = String.Format("CHKV" & x)
CHKV.TabStop = False
CHKA.BackColor = System.Drawing.Color.DimGray
CHKA.CheckAlign = System.Drawing.ContentAlignment.MiddleCenter
CHKA.Location = posA
CHKA.Name = String.Format("CHKA" & x)
CHKA.Size = New System.Drawing.Size(16, 16)
CHKA.TabStop = False
CHKU.BackColor = System.Drawing.Color.DimGray
CHKU.CheckAlign = System.Drawing.ContentAlignment.MiddleCenter
CHKU.Location = posU
CHKU.Name = String.Format("CHKU" & x)
CHKU.Size = New System.Drawing.Size(16, 16)
CHKU.TabStop = False
CHKD.BackColor = System.Drawing.Color.DimGray
CHKD.CheckAlign = System.Drawing.ContentAlignment.MiddleCenter
CHKD.Location = posD
CHKD.Name = String.Format("CHKD" & x)
CHKD.Size = New System.Drawing.Size(16, 16)
CHKD.TabStop = False
posV.Y += 20
posA.Y += 20
posU.Y += 20
posD.Y += 20
Me.Controls.Add(CHKV)
Me.Controls.Add(CHKA)
Me.Controls.Add(CHKU)
Me.Controls.Add(CHKD)
x += 1
End While
End Sub
-
You have to define a procedure that has the same signature as a CheckBox.CheckChanged event handler and then connect the two using the AddHandler statement:
Code: AddHandler myCheckBox.CheckChanged, AddressOf myProcedure -
ty,
i have added handler, but the problem know is....
handler manages only events for control that is last added to the form.
ex.
if i have called sub byVal i=3, only controls with name CHKV3, CHKA3, CHKU3, CHKN3 are managed by handlers.
controls look like this on the form:
chkV1 chkA1 chkU1 chkD1
chkV2 chkA2 chkU2 chkD2
chkV3 chkA3 chkU3 chkD3 -
i think you are going to need to clarify. do you want the events to work only for the controls just added, or does this already happen (which is undesired)?
if you want that to happen then you could, in the event handler sub routine, check the last character in the .Name() property of the sender and only execute further if it matches the number you want.
or
you could RemoveHandler for every event you have previously added (works the same way as AddHandler) before adding the new control's events. -
The event handler will handle every event that you tell it to. Until you call RemoveHandler, every event that you have called AddHandler for will be handled by the specified procedure. If the procedure is not handling the event then you haven't called AddHandler for that event. My guess is that you are calling AddHandler outside your While loop, so the value of "x" is already equal to the value of "i", so obviously only those controls that end in that value will have an event handler defined. You need to call AddHandler inside the While loop.
Can I also mention that your use of String.Format when setting the Name of the CheckBoxes is pointless. This:Code: does exactly the same as this:CHKA.Name = String.Format("CHKA" & x)Code: except less efficiently. If you actullay want to make use of the functionality of String.Format you would have to do this:CHKA.Name = "CHKA" & xCode: CHKA.Name = String.Format("CHKA{0}", x) -
ty John!
you have helped me a lot,
i wanted to add an EventHandler for every control that is added runtime.
things are working now fine, i have put AddHandler in While loop after control is added.
events are catched for every control
another thing,
is there a way to could declare a variables like i have defined a name for controls added runtime?
ex.
Dim i As Integer =1
While i <5
Dim name & i As String
i+=1
Loop
so i have variables : name1, name2,... -
You can set the Name property of your controls like that but that doesn't allow you to refer to them in code by those names. When you create a control at design time it sets the Name property to be the same as the name of the member variable you will use to refer to it. It's the variable names that you're using in code, not the Name property. In your case I'd probably suggest a 2D array as a member variable. Then you can ReDim it as needed and assign your controls to its elements, which allows you to refer to them by their coordinates.
-
i know that in code i'm using variable name, not the name property.
but i'm not shure what are you suggesting exactly.
if you could help me a bit more ? -
If you want to refer to these CheckBoxes in code then you need an existing member variable, e.g.
Code: Private checkBoxes As CheckBox(,)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ReDim Me.checkBoxes(3, 3)
For i As Integer = 0 To Me.checkBoxes.GetUpperBound(0) Step 1
For j As Integer = 0 To Me.checkBoxes.GetUpperBound(1) Step 1
Me.checkBoxes(i, j) = New CheckBox
Next
Next
End Sub -
Hi,
I have been working on a vb.net project. where I want to use drag and drop controls. Right now, I can able to create the controls on runtime and i can able to place them on the form with mouseup,down events.. The only problem is how to move the controls (command buttons) on the form to place them right place on the form.
i tried mouse down and mouse up methods to do it. But, the button created in one event handling sub routine is not available in other sub routines.(I need a global runtime button to be created in a event method so that other event procedures also share that button).do u understand?
let's me explain you briefly
Let's say i have creted a runtime button with mouse click event by useing following code
Dim btn as new button
me.controls.add(btn)
btn.location=20,30
btn.show..like
addhandler.click addressof.btnruntimeprocedure
the button created here is only accesable with in the mouseclick sub routine.If i want to use same button(btn) in the mousedown and mouseup events ?
i cann't use ,becasue btn is local to mouseclick event sub.There is no access for it.
How do i create a runtime global button?
Please help me in this regard.
Thank you
Ramesh -
I've already told you that you need an existing member variable by which you can refer to these controls. You can use a single object, an array or a collection. A good option is to use a HashTable, which then allows you to refer to each control by name rather than just by index.
-
Thank you for your reply,
Can you please show me some sample code for it? please use command button as example, instead of other controls.
The best example that suites my problem is.
http://www.developerfusion.co.uk/show/2499/
In the above example author wrote code for only click event handling method.
AddHandler cb.Click, AddressOf cbDesignTime_Click
How to write code for other event handling methods(mouseup,mousedown,doubleclick)?
I need examples for
AddHandler cb.Click, AddressOf cbDesignTimedoubleclick
AddHandler cb.MouseUp, AddressOf cbDesignTimeMouseUp
AddHandler cb.Mousedown, AddressOf cbDesignTimeMouseDown
AddHandler cb.DoubleClick, AddressOf cbDesignTimeDoubleClick
If I get the solution for it. i am done
Thank you,
Ramesh -
I just realised that I'm now dealing with someone new and not the thread starter. You write the event handlers for controls created at run time in EXACTLY the same way as you do for those created at design time EXCEPT that they don't have a Handles clause. If you want to know what the method signature should be then either add the appropriate control to your form and have the IDE create the event handler for you, then just remove the control and the Handles clause, or else just look it up in the help/MSDN. It is quite simple to look this sort of thing up yourself.
-
Thanks for you reply
I got the solution for my problem
I placed below code in the "Windows Form Designer generated code"
Friend WithEvents btn As System.Windows.Forms.Button
and i created a new instance of "btn" in the mousedown event there by i created a new button with following code
btn=new Button
this created a global runtime button.
great ….
Thank you,
rannabat
Post a reply
Quick links
Recent activity
- arif ahmad replied to How to receive data in web ...
- William Thompson replied to What is the name of the Win...
- Sameera Piyadigamage replied to Point of Sale Developers: H...
- Scott Carline replied to 4 x C# Developers for large...
- Rajendra Dhakal replied to Restore SQL Server text dat...
- cloud rainda replied to How to convert between TS f...
Enter your message below
Sign in or Join us (it's free).