|
-
Jan 3rd, 2010, 05:41 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Simple User Control Question
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?
-
Jan 3rd, 2010, 05:52 PM
#2
Re: Simple User Control Question
The ideal way would be through a property in the user control. Or through some kind of public method.
-tg
-
Jan 3rd, 2010, 06:16 PM
#3
Re: Simple User Control Question
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
-
Jan 3rd, 2010, 06:50 PM
#4
-
Jan 3rd, 2010, 07:09 PM
#5
Re: Simple User Control Question
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
-
Jan 3rd, 2010, 07:33 PM
#6
Thread Starter
Fanatic Member
Re: Simple User Control Question
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.
-
Jan 3rd, 2010, 07:41 PM
#7
Re: Simple User Control Question
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
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jan 3rd, 2010, 09:20 PM
#8
PowerPoster
Re: Simple User Control Question
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:
Code:
Form1.Label13(0).Caption
For instance...
I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...
|Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Love Shack | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University | Ambra Cheese |
Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...
-
Jan 3rd, 2010, 09:51 PM
#9
Re: Simple User Control Question
 Originally Posted by GDOG34
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.
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....
Code:
Public Property Let UserName (ByVal NewVal As String)
Label1.Caption = NewVal
End Property
In your form you can call it like so,
Code:
bob.UserName = "GDOG34"
, if the UC is on a different form then of course you would just prefix the form name.
Code:
SomeOtherForm.bob.UserName = "GDOG34"
Last edited by Edgemeal; Jan 3rd, 2010 at 10:03 PM.
-
Jan 3rd, 2010, 10:00 PM
#10
Re: Simple User Control Question
 Originally Posted by techgnome
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
I assume you're saying that this should have been done when creating the control?
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Jan 3rd, 2010, 10:11 PM
#11
Re: Simple User Control Question
 Originally Posted by Edgemeal
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....
Oh! This must have been what I misunderstood from tg.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Jan 4th, 2010, 03:55 AM
#12
Re: Simple User Control Question
 Originally Posted by CDRIVE
Oh! This must have been what I misunderstood from tg.
VB6 also has a UserControl Wizard (ActiveX Control Interface Wizard ) in the Add-In manger that you can load and use to do a lot of that coding work for you.
-
Jan 4th, 2010, 04:31 AM
#13
PowerPoster
Re: Simple User Control Question
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!!
I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...
|Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Love Shack | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University | Ambra Cheese |
Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...
-
Jan 5th, 2010, 06:16 PM
#14
Thread Starter
Fanatic Member
Re: Simple User Control Question
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?
-
Jan 5th, 2010, 06:27 PM
#15
Re: Simple User Control Question
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:
Code:
Public Property Get UserName () as String
UserName = Label1.Caption
End Property
-tg
-
Jan 5th, 2010, 07:26 PM
#16
Thread Starter
Fanatic Member
Re: Simple User Control Question
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.
-
Jan 5th, 2010, 07:50 PM
#17
Re: Simple User Control Question
 Originally Posted by GDOG34
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
Last edited by Edgemeal; Jan 5th, 2010 at 07:54 PM.
-
Jan 5th, 2010, 08:13 PM
#18
Re: Simple User Control Question
just another example, Add Click event and a mousemove event,
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
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.
-
Jan 5th, 2010, 09:04 PM
#19
Thread Starter
Fanatic Member
Re: Simple User Control Question
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?
-
Jan 5th, 2010, 09:17 PM
#20
Re: Simple User Control Question
Well for the code posted above...techgnome mentioned in post 15... you need to add a "Get" property for the same "Let" name.....
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
' Form with UC named bob on it,,,
Code:
If Bob.UserName = "0" Then
Bob.UserName = "1"
End If
You really should try the UC Wizard in VB!
-
Jan 5th, 2010, 10:58 PM
#21
Thread Starter
Fanatic Member
Re: Simple User Control Question
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|