Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. dealing with control arrays

dealing with control arrays

Scheduled Pinned Locked Moved Visual Basic
tutorialquestiongraphicsdata-structureshelp
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    No e
    wrote on last edited by
    #1

    Any help in general would be appreciated, I need to build a control array at run time, so as an example I can do something like this

    Private Sub createlblSlotArray()
    Dim i As Short
    ReDim lblSlot(3)
    For i = 1 To 3
    lblSlot(i) = New System.Windows.Forms.Label
    With lblSlot(i)
    .Tag = i
    AddHandler .Click, AddressOf Me.lblSlot_click
    End With
    Next
    Me.Controls.AddRange(lblSlot)
    lblSlot(1).Location = New System.Drawing.Point(50, 50)
    lblSlot(2).Location = New System.Drawing.Point(100, 70)
    lblSlot(3).Location = New System.Drawing.Point(100, 90)
    End Sub

    I need to add a popup menu to each of these labels, when the user right clicks on a label, I need to know which one the right click was on, set a variable so it can be used later, then show the context menu. would I use the form1_click?

    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
    ' what to do here to get the label? this seems to be tied to only the first label?
    End Sub

    can you show me an example of how to handle events from a control array of this nature, also, if I do not know how many context menu items I will have until run time, so I am not sure how to do that, the popup menu click event appear to be tied to the exact item that was selected.

    Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
    ' this is OK for menu item 1, how do I deal with new items I may add at run time?
    End Sub

    thanks in advance No-e

    D D 2 Replies Last reply
    0
    • N No e

      Any help in general would be appreciated, I need to build a control array at run time, so as an example I can do something like this

      Private Sub createlblSlotArray()
      Dim i As Short
      ReDim lblSlot(3)
      For i = 1 To 3
      lblSlot(i) = New System.Windows.Forms.Label
      With lblSlot(i)
      .Tag = i
      AddHandler .Click, AddressOf Me.lblSlot_click
      End With
      Next
      Me.Controls.AddRange(lblSlot)
      lblSlot(1).Location = New System.Drawing.Point(50, 50)
      lblSlot(2).Location = New System.Drawing.Point(100, 70)
      lblSlot(3).Location = New System.Drawing.Point(100, 90)
      End Sub

      I need to add a popup menu to each of these labels, when the user right clicks on a label, I need to know which one the right click was on, set a variable so it can be used later, then show the context menu. would I use the form1_click?

      Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
      ' what to do here to get the label? this seems to be tied to only the first label?
      End Sub

      can you show me an example of how to handle events from a control array of this nature, also, if I do not know how many context menu items I will have until run time, so I am not sure how to do that, the popup menu click event appear to be tied to the exact item that was selected.

      Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
      ' this is OK for menu item 1, how do I deal with new items I may add at run time?
      End Sub

      thanks in advance No-e

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      If you are handling the MenuItem click event, then you can get the control name using MenuItemName.GetContextMenu().SourceControl. In order to attach a click event to the dynamic menuitem, use MenuitemName.Click += new EventHandler(EventHandlerName).

      जय हिंद

      N 1 Reply Last reply
      0
      • D dan sh

        If you are handling the MenuItem click event, then you can get the control name using MenuItemName.GetContextMenu().SourceControl. In order to attach a click event to the dynamic menuitem, use MenuitemName.Click += new EventHandler(EventHandlerName).

        जय हिंद

        N Offline
        N Offline
        No e
        wrote on last edited by
        #3

        Thanks, but I not quite following you. When the user selects a menu item from context menu, I how do I determine what item they selected? What event do I look for and how do I grab the item? No-e

        D 1 Reply Last reply
        0
        • N No e

          Thanks, but I not quite following you. When the user selects a menu item from context menu, I how do I determine what item they selected? What event do I look for and how do I grab the item? No-e

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          Each menuitem has a Click event. You will need to handle that. Object sender in the definition will tell you all about the clicked menuitem.

          जय हिंद

          1 Reply Last reply
          0
          • N No e

            Any help in general would be appreciated, I need to build a control array at run time, so as an example I can do something like this

            Private Sub createlblSlotArray()
            Dim i As Short
            ReDim lblSlot(3)
            For i = 1 To 3
            lblSlot(i) = New System.Windows.Forms.Label
            With lblSlot(i)
            .Tag = i
            AddHandler .Click, AddressOf Me.lblSlot_click
            End With
            Next
            Me.Controls.AddRange(lblSlot)
            lblSlot(1).Location = New System.Drawing.Point(50, 50)
            lblSlot(2).Location = New System.Drawing.Point(100, 70)
            lblSlot(3).Location = New System.Drawing.Point(100, 90)
            End Sub

            I need to add a popup menu to each of these labels, when the user right clicks on a label, I need to know which one the right click was on, set a variable so it can be used later, then show the context menu. would I use the form1_click?

            Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
            ' what to do here to get the label? this seems to be tied to only the first label?
            End Sub

            can you show me an example of how to handle events from a control array of this nature, also, if I do not know how many context menu items I will have until run time, so I am not sure how to do that, the popup menu click event appear to be tied to the exact item that was selected.

            Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
            ' this is OK for menu item 1, how do I deal with new items I may add at run time?
            End Sub

            thanks in advance No-e

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            There are no control arrays in .NET and no need for them. You handle the Click event of the label, not the form. The ending part of the function header of the event handler just needs to be modified so it can handle the Click event of all of your label controls. You can do that by adding the name of each label and it's event name to the Handles clause:

            Private Sub Labels\_Click(_blah, blah_) Handles Label1.Click, Label2.Click, Label3.Click
            

            Then, if the handler code, the label that was clicked will be in the sender argument passed to your event handler. Alternatively, you can even skip the Handles clause and wire up the event handlers yourself using AddHandler.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            J 1 Reply Last reply
            0
            • D Dave Kreskowiak

              There are no control arrays in .NET and no need for them. You handle the Click event of the label, not the form. The ending part of the function header of the event handler just needs to be modified so it can handle the Click event of all of your label controls. You can do that by adding the name of each label and it's event name to the Handles clause:

              Private Sub Labels\_Click(_blah, blah_) Handles Label1.Click, Label2.Click, Label3.Click
              

              Then, if the handler code, the label that was clicked will be in the sender argument passed to your event handler. Alternatively, you can even skip the Handles clause and wire up the event handlers yourself using AddHandler.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              J Offline
              J Offline
              Jon_Boy
              wrote on last edited by
              #6

              Dave's one of the brightest minds on the boards here, but I'm going to disagree with him slightly. You can have an array of an labels and setup everything dynamically. Is this the preferred way of doing things the majority of the time? NO. Most of the time you don't need to do this; however, sometimes forms or controls need to built and set dynamically (IE: dynamic menus, reporting forms, etc). If you really need this functionality, create the array of labels and link them with the addhandler. As Dave mentioned, you can have one event that handles all the click events for the controls. Parse on the control name or whatever else you need to do to perform the correct function from within the event that is shared amongst all the label controls.

              "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

              D 1 Reply Last reply
              0
              • J Jon_Boy

                Dave's one of the brightest minds on the boards here, but I'm going to disagree with him slightly. You can have an array of an labels and setup everything dynamically. Is this the preferred way of doing things the majority of the time? NO. Most of the time you don't need to do this; however, sometimes forms or controls need to built and set dynamically (IE: dynamic menus, reporting forms, etc). If you really need this functionality, create the array of labels and link them with the addhandler. As Dave mentioned, you can have one event that handles all the click events for the controls. Parse on the control name or whatever else you need to do to perform the correct function from within the event that is shared amongst all the label controls.

                "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Jon_Boy wrote:

                but I'm going to disagree with him slightly.

                All I said was there was no need for the control array, not the dynamic creation of controls. When the labels click events are all wired up to the same handler, he'll get the instance that was clicked in the sender argument in the handler. I think we're on the same page, but maybe not. I just can't see the difference.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                J 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Jon_Boy wrote:

                  but I'm going to disagree with him slightly.

                  All I said was there was no need for the control array, not the dynamic creation of controls. When the labels click events are all wired up to the same handler, he'll get the instance that was clicked in the sender argument in the handler. I think we're on the same page, but maybe not. I just can't see the difference.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008

                  J Offline
                  J Offline
                  Jon_Boy
                  wrote on last edited by
                  #8

                  Roger that gold leader. I misconstrued your reply.

                  "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups