I have a usercontrol and it has a label on it. From a form how do I change the label? I tired USERCONTROLNAME.Object.LABEL1.Caption = ":)" but it didnt work. How do I access the objects in the usercontrol?
Printable View
I have a usercontrol and it has a label on it. From a form how do I change the label? I tired USERCONTROLNAME.Object.LABEL1.Caption = ":)" but it didnt work. How do I access the objects in the usercontrol?
The ideal way would be through a property in the user control. Or through some kind of public method.
-tg
GDO
I'm a little confused. Are you simply trying to change the
Caption of a control named LABEL1?
If so, does ..
LABEL1.Caption = "New Caption"
.. work?
If that works, could it have something to do with your trying
to use a graphic (the ":)") instead of actual text in the
Caption property?
Spoo
I'm confused. The Caption property sets & returns a String :confused:, but I think you know that?
But when that label is on a USER CONTROL.... changing it from the FORM isn't so straight forward. Idealy you create & expose a property of the user control that, when set, would then pass the value to the label itself.
-tg
Sorry if I confused anybody let me reexplain this. I made a usercontrol lets call it bob. So bob had a label and its called label1. I want to change label1 that is inside bob. Bob is in form1. On the form load property I want to change the caption of label1. How ca I do this? I tried Bob.Object.Label1.Caption = "WHATEVER" but it dosen't work.
You need a property to tell Label1 inside the User control to rename the Label..
So ... Bob needs John to tell the Label to change its caption...
Bob cannot do it by himself...
So have a property to your customCaption
then... UserControl1.CustomCaption = "Whoala..."
then in the UserControl CustomCaption property you have
Label1.caption = customCaption
Go into design-time. And then select where the Label property is in the properties window. Finally: Select Form1.Label1.Name, and then change that to what ever you wish to make as the name of it. Remember don't add a number, because when you make it a control array, if you do. Then it will have very many errors, in the numbering of the controls.
It may look like this:
For instance...Code:Form1.Label13(0).Caption
If all you want to do is change the caption of label1 inside your usercontrol then just add a Let property and use a name you can identify it with, lets name the property "UserName", now add this to your UC....
In your form you can call it like so,Code:Public Property Let UserName (ByVal NewVal As String)
Label1.Caption = NewVal
End Property
, if the UC is on a different form then of course you would just prefix the form name.Code:bob.UserName = "GDOG34"
Code:SomeOtherForm.bob.UserName = "GDOG34"
What they have. And I was doing all of the hard work myself!! To think about that they have a wizard to do so, now that was pretty darn good, for them!!
Thanks for all the replies. Edgemeal, what you suggested worked perfectly! But I have a question, why can't I create an if statement? I have:
vb Code:
If bob.UserName = "GDOG34" Then 'Do Something Else 'DO Something Else End If
I get the error Invaild use of property. Why?
If you used the sample from above and only created a Let function for your property, then it's write-only. You'll need to create a Get version that returns the value of the property.
I'm a little rusty in VB6, but I think it looks like this:
-tgCode:Public Property Get UserName () as String
UserName = Label1.Caption
End Property
Ok, That works. I have another quick question, how can I add a click state to it? There are only a few options availible like dragdrop, dragover, gotfocus, lost focus, and validate.
You use Event Name(Param,param,param) and RaiseEvent to raise an event and pass the params if any.
Simple Click event,
Code:' UC code
Option Explicit
Public Event Click()
Private Sub Label1_Click()
RaiseEvent Click
End Sub
' Form code
' Note: when you add an event to the UC it will show up in the UC on the form, so you should now see a Private Sub bob_Click()
Private Sub bob_Click()
MsgBox "You clicked label1 in UC bob", vbInformation
End Sub
just another example, Add Click event and a mousemove event,
BTW, If UCs are new to you then you should probably try the ActiveX Control Wizard, just have create a couple properties and events, then look at the code it creates and see how it works, it can be very tedious to all this stuff by hand unless its in your blood.Code:' UC code
Option Explicit
Public Event Click()
Public Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Private Sub Label1_Click()
RaiseEvent Click
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
RaiseEvent MouseMove(Button, Shift, X, Y)
End Sub
How does a usercontrol work? I got the lick state working but it dosent work right. When the form loads it sets Usercontrol.Bob = "0" and when the user clicks the usercontrol it works like so:
vb Code:
If UserControl1.Bob = "0" Then UserControl1.Bob = "1" End If
But this dosen't work at all. It acts like Bob dosent even equal "0". Why is that?
Well for the code posted above...techgnome mentioned in post 15... you need to add a "Get" property for the same "Let" name.....
' Form with UC named bob on it,,,Code:Public Property Get UserName () as String
UserName = Label1.Caption
End Property
Public Property Let UserName (ByVal NewVal As String)
Label1.Caption = NewVal
End Property
You really should try the UC Wizard in VB! :DCode:If Bob.UserName = "0" Then
Bob.UserName = "1"
End If
I know I read post 15 and I have both properties! But I found a solution I just deleted the control and recreated it again and it worked. Thanks for all the help!