On May 11, 5:13 pm, "Rob" <r...@yahoo.comwrote:
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