FlowLayoutPanel question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob

    #1

    FlowLayoutPanel question

    I have created a user control that has several buttons on it.....

    The code below appears to add an instance of this user control to the Flow
    Layout Panel just fine...

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    Dim aUnit As New MyUserControl
    FlowLayoutPanel 1.Controls.Add( aUnit)
    End Sub

    However when I run the following code... it tells me that all the user
    controls have the same name in the same container (even though the location
    is changing).
    I know that this should not be possible. What am I doing wrong ? How can
    I access a specific instance of my user control within the Flow?

    Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button2.Click
    Dim ctrl As Control

    For Each ctrl In FlowLayoutPanel 1.Controls
    MsgBox(ctrl.Nam e)
    MsgBox(ctrl.Loc ation.Y)
    Next
    End Sub


  • Chris Dunaway

    #2
    Re: FlowLayoutPanel question

    On May 11, 1:32 pm, "Rob" <r...@yahoo.com wrote:
    I have created a user control that has several buttons on it.....
    >
    The code below appears to add an instance of this user control to the Flow
    Layout Panel just fine...
    >
    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    Dim aUnit As New MyUserControl
    FlowLayoutPanel 1.Controls.Add( aUnit)
    End Sub
    >
    However when I run the following code... it tells me that all the user
    controls have the same name in the same container (even though the location
    is changing).
    I know that this should not be possible. What am I doing wrong ? How can
    I access a specific instance of my user control within the Flow?
    >
    Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button2.Click
    Dim ctrl As Control
    >
    For Each ctrl In FlowLayoutPanel 1.Controls
    MsgBox(ctrl.Nam e)
    MsgBox(ctrl.Loc ation.Y)
    Next
    End Sub
    Controls don't really have names. You can use the Tag property to put
    a string so you can identify a particular control. In what context do
    you need to identify the control? Does your control raise events? If
    so, the sender argument of the event handler will tell you which
    control raised the event.

    Perhaps if you provided a little information on why you need to
    identify the control, a better method could be suggested.

    Chris

    Comment

    • Rob

      #3
      Re: FlowLayoutPanel question

      Hmmm... I thought all controls had names... and no 2 controls may have the
      same name within the same container or form...

      When you add a Button to a form and give it a name of Button1... then add a
      second Button2... try to change the Button2 name to Button1 and you get an
      error.

      Anyway..., basically I will be tracking time against jobs....

      The user control which includes some text boxes and butttons held vs a job
      gets added to the FlowLayout Panel as need by the person... then the person
      fill in some data related to the job...

      When the person is finished they click a button within that specific user
      control and I populate a database... thus I need to know the name of the
      Textbox on the specific user control "X" in order to obtain its value and
      populate the db.

      Hope this makes sense...

      Thanks !


      "Chris Dunaway" <dunawayc@gmail .comwrote in message
      news:1178917299 .828330.61700@e 65g2000hsc.goog legroups.com...
      On May 11, 1:32 pm, "Rob" <r...@yahoo.com wrote:
      >I have created a user control that has several buttons on it.....
      >>
      >The code below appears to add an instance of this user control to the
      >Flow
      >Layout Panel just fine...
      >>
      >Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
      >System.EventAr gs) Handles Button1.Click
      > Dim aUnit As New MyUserControl
      > FlowLayoutPanel 1.Controls.Add( aUnit)
      > End Sub
      >>
      >However when I run the following code... it tells me that all the user
      >controls have the same name in the same container (even though the
      >location
      >is changing).
      >I know that this should not be possible. What am I doing wrong ? How
      >can
      >I access a specific instance of my user control within the Flow?
      >>
      > Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
      >System.EventAr gs) Handles Button2.Click
      > Dim ctrl As Control
      >>
      > For Each ctrl In FlowLayoutPanel 1.Controls
      > MsgBox(ctrl.Nam e)
      > MsgBox(ctrl.Loc ation.Y)
      > Next
      > End Sub
      >
      Controls don't really have names. You can use the Tag property to put
      a string so you can identify a particular control. In what context do
      you need to identify the control? Does your control raise events? If
      so, the sender argument of the event handler will tell you which
      control raised the event.
      >
      Perhaps if you provided a little information on why you need to
      identify the control, a better method could be suggested.
      >
      Chris
      >

      Comment

      • Chris Dunaway

        #4
        Re: FlowLayoutPanel question

        On May 11, 5:13 pm, "Rob" <r...@yahoo.com wrote:
        Hmmm... I thought all controls had names... and no 2 controls may have the
        same name within the same container or form...
        >
        When you add a Button to a form and give it a name of Button1... then add a
        second Button2... try to change the Button2 name to Button1 and you get an
        error.
        Pardon my brain malfunction, you are right. I was thinking about
        variable names (not properties). When you add Button1 and Button2 to
        your forms, the names "Button1" and "Button2" are the names of the
        Form's public fields for those buttons.
        >
        Anyway..., basically I will be tracking time against jobs....
        >
        The user control which includes some text boxes and butttons held vs a job
        gets added to the FlowLayout Panel as need by the person... then the person
        fill in some data related to the job...
        So the FlowLayoutPanel is a container for one or more of these user
        controls. And they get added dynamically at runtime rather than at
        design time.
        >
        When the person is finished they click a button within that specific user
        control and I populate a database... thus I need to know the name of the
        Textbox on the specific user control "X" in order to obtain its value and
        populate the db.
        The user of the application interacts with the user controls in some
        manner and then clicks a button on that control and at that point you
        want to take the data in the user control and update the database.

        I would suggest adding a public property to the user control which
        returns the desired information (or business object). Then add an
        event to the user control and raise that event when the button is
        clicked. When you add the user control to the FlowLayoutPanel , wire
        up this event to a common event handler. In the event handler, cast
        the "sender" argument of the event to your user control class and then
        access the property you added. In this way, you don't have to know
        the name of the control.

        'In the User Control
        Public Class MyUserControl
        Public Property TheInformation As String 'or whatever
        End Property

        Public Event SomethingHappen ed(ByVal sender As Object, ByVal e As
        EventArgs)

        Private Sub Button1_Click(. ..) Handles Button1.Click

        RaiseEvent SomethingHappen ed(Me, EventArgs.Empty )
        End Sub
        End Class


        Private Sub AddUserControl( )
        'Somewhere when you add an instance of the User Control to the
        FlowLayoutPanel
        Dim c As New MyUserControl()

        'Every instance of the user control that you create, wires up the
        same event handler
        AddHandler c.SomethingHapp ened, AddressOf
        HandleSomething HappenedEvent
        End Sub

        Private Sub HandleSomething HappenedEvent(B yVal sender As Object, ByVal
        e As EventArgs)
        'Cast the sender as your user control type
        Dim mycontrol As MyUserControl = DirectCast(send er,
        MyUserControl)

        'Now access the string property
        Dim s As String = mycontrol.TheIn formation

        'And if you need the Name property, you can access it
        MsgBox("Control name is " & mycontrol.Name)

        End Sub

        Watch for typos, but I hope this gives you some ideas.

        Chris

        Comment

        • Rob

          #5
          Re: FlowLayoutPanel question

          Thanks Chris !

          "Chris Dunaway" <dunawayc@gmail .comwrote in message
          news:1179154414 .124177.320260@ o5g2000hsb.goog legroups.com...
          On May 11, 5:13 pm, "Rob" <r...@yahoo.com wrote:
          >Hmmm... I thought all controls had names... and no 2 controls may have
          >the
          >same name within the same container or form...
          >>
          >When you add a Button to a form and give it a name of Button1... then
          >add a
          >second Button2... try to change the Button2 name to Button1 and you get
          >an
          >error.
          >
          Pardon my brain malfunction, you are right. I was thinking about
          variable names (not properties). When you add Button1 and Button2 to
          your forms, the names "Button1" and "Button2" are the names of the
          Form's public fields for those buttons.
          >
          >>
          >Anyway..., basically I will be tracking time against jobs....
          >>
          >The user control which includes some text boxes and butttons held vs a
          >job
          >gets added to the FlowLayout Panel as need by the person... then the
          >person
          >fill in some data related to the job...
          >
          So the FlowLayoutPanel is a container for one or more of these user
          controls. And they get added dynamically at runtime rather than at
          design time.
          >
          >>
          >When the person is finished they click a button within that specific user
          >control and I populate a database... thus I need to know the name of the
          >Textbox on the specific user control "X" in order to obtain its value and
          >populate the db.
          >
          The user of the application interacts with the user controls in some
          manner and then clicks a button on that control and at that point you
          want to take the data in the user control and update the database.
          >
          I would suggest adding a public property to the user control which
          returns the desired information (or business object). Then add an
          event to the user control and raise that event when the button is
          clicked. When you add the user control to the FlowLayoutPanel , wire
          up this event to a common event handler. In the event handler, cast
          the "sender" argument of the event to your user control class and then
          access the property you added. In this way, you don't have to know
          the name of the control.
          >
          'In the User Control
          Public Class MyUserControl
          Public Property TheInformation As String 'or whatever
          End Property
          >
          Public Event SomethingHappen ed(ByVal sender As Object, ByVal e As
          EventArgs)
          >
          Private Sub Button1_Click(. ..) Handles Button1.Click
          >
          RaiseEvent SomethingHappen ed(Me, EventArgs.Empty )
          End Sub
          End Class
          >
          >
          Private Sub AddUserControl( )
          'Somewhere when you add an instance of the User Control to the
          FlowLayoutPanel
          Dim c As New MyUserControl()
          >
          'Every instance of the user control that you create, wires up the
          same event handler
          AddHandler c.SomethingHapp ened, AddressOf
          HandleSomething HappenedEvent
          End Sub
          >
          Private Sub HandleSomething HappenedEvent(B yVal sender As Object, ByVal
          e As EventArgs)
          'Cast the sender as your user control type
          Dim mycontrol As MyUserControl = DirectCast(send er,
          MyUserControl)
          >
          'Now access the string property
          Dim s As String = mycontrol.TheIn formation
          >
          'And if you need the Name property, you can access it
          MsgBox("Control name is " & mycontrol.Name)
          >
          End Sub
          >
          Watch for typos, but I hope this gives you some ideas.
          >
          Chris
          >

          Comment

          Working...