adding controls

  • 15 years ago


    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:


    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

               x += 1
           End While

       End Sub

  • 15 years ago

    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:

    AddHandler myCheckBox.CheckChanged, AddressOf myProcedure

  • 15 years ago


    i have added handler, but the problem know is....

    handler manages only events for control that is last added to the form.

    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

  • 15 years ago
    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.


    you could RemoveHandler for every event you have previously added (works the same way as AddHandler) before adding the new control's events.
  • 15 years ago

    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:

    CHKA.Name = String.Format("CHKA" & x)
    does exactly the same as this:Code:
    CHKA.Name = "CHKA" & x
    except less efficiently. If you actullay want to make use of the functionality of String.Format you would have to do this:Code:
    CHKA.Name = String.Format("CHKA{0}", x)

  • 15 years ago

    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?

    Dim i As Integer =1
    While i <5
    Dim name & i As String

    so i have variables : name1, name2,...

  • 15 years ago

    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.

  • 15 years ago

    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 ?

  • 15 years ago

    If you want to refer to these CheckBoxes in code then you need an existing member variable, e.g.

       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
       End Sub

  • 15 years ago


       I have been working on a 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
    btn.location=20,30 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

  • 15 years ago
    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.
  • 15 years ago

    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.
    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 cbDesignTime
    AddHandler cb.Mousedown, AddressOf cbDesignTimeMouseDown
    AddHandler cb.DoubleClick, AddressOf cbDesignTime

    If I get the solution for it. i am done

    Thank you,

  • 15 years ago

    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.

  • 15 years ago

    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,

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan