Results 1 to 21 of 21

Thread: [RESOLVED] Simple User Control Question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    Resolved [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?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  4. #4
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Simple User Control Question

    I'm confused. The Caption property sets & returns a String , but I think you know that?
    <--- 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??

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    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.

  7. #7
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    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.



  8. #8
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    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...

  9. #9
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Simple User Control Question

    Quote Originally Posted by GDOG34 View Post
    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.

  10. #10
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Simple User Control Question

    Quote Originally Posted by techgnome View Post
    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??

  11. #11
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Simple User Control Question

    Quote Originally Posted by Edgemeal View Post
    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??

  12. #12
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Simple User Control Question

    Quote Originally Posted by CDRIVE View Post
    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.

  13. #13
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    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...

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    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:
    1. If bob.UserName = "GDOG34" Then
    2. 'Do Something
    3. Else
    4. 'DO Something Else
    5. End If

    I get the error Invaild use of property. Why?

  15. #15
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    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.

  17. #17
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Simple User Control Question

    Quote Originally Posted by GDOG34 View Post
    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.

  18. #18
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    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:
    1. If UserControl1.Bob = "0" Then
    2. UserControl1.Bob = "1"
    3. End If

    But this dosen't work at all. It acts like Bob dosent even equal "0". Why is that?

  20. #20
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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!

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    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
  •  



Click Here to Expand Forum to Full Width