FlowLayoutPanel question

R

Rob

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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim aUnit As New MyUserControl
FlowLayoutPanel1.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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim ctrl As Control

For Each ctrl In FlowLayoutPanel1.Controls
MsgBox(ctrl.Name)
MsgBox(ctrl.Location.Y)
Next
End Sub
 
C

Chris Dunaway

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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim aUnit As New MyUserControl
FlowLayoutPanel1.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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim ctrl As Control

For Each ctrl In FlowLayoutPanel1.Controls
MsgBox(ctrl.Name)
MsgBox(ctrl.Location.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
 
R

Rob

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 !
 
C

Chris Dunaway

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 SomethingHappened(ByVal sender As Object, ByVal e As
EventArgs)

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

RaiseEvent SomethingHappened(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.SomethingHappened, AddressOf
HandleSomethingHappenedEvent
End Sub

Private Sub HandleSomethingHappenedEvent(ByVal sender As Object, ByVal
e As EventArgs)
'Cast the sender as your user control type
Dim mycontrol As MyUserControl = DirectCast(sender,
MyUserControl)

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

'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
 
R

Rob

Thanks Chris !

Chris Dunaway said:
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.


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.


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 SomethingHappened(ByVal sender As Object, ByVal e As
EventArgs)

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

RaiseEvent SomethingHappened(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.SomethingHappened, AddressOf
HandleSomethingHappenedEvent
End Sub

Private Sub HandleSomethingHappenedEvent(ByVal sender As Object, ByVal
e As EventArgs)
'Cast the sender as your user control type
Dim mycontrol As MyUserControl = DirectCast(sender,
MyUserControl)

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

'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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top