Results 1 to 5 of 5

Thread: usercontrols. something i have never touched

  1. #1

    Thread Starter
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    usercontrols. something i have never touched

    Hello friends.

    Can anybody direct me to a good tutorial on creating a usercontrol. Something you have wrote yourself maybe ?, I am predominantly visual when it comes to learning so any simple examples would be great

    thank you.
    casey.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: usercontrols. something i have never touched

    Quote Originally Posted by vbasicgirl
    Hello friends.

    Can anybody direct me to a good tutorial on creating a usercontrol. Something you have wrote yourself maybe ?, I am predominantly visual when it comes to learning so any simple examples would be great

    thank you.
    casey.
    Hi,

    This is how I started off. I hope this will help u too.

    I could have obviously posted the whole project, but I think that won't help you much, how to start off. So here's what to do setp-by step.


    Not much of functionality here, but this will demonstrate how to just create a simple OCX control.

    This OCX just emulates a clock. Additionaly the User can reset it whenever required.

    First start a New Project. Choos the project type to be Activex Control.
    On the form, add a Label and a Timer. Then open teh code window and add this code.
    VB Code:
    1. Option Explicit
    2. Dim dBaseTime As Date
    3.  
    4. Private Sub Label1_DblClick()
    5.     Dim s As String
    6.     s = InputBox("Enter New Time (hh:mm:ss):", "Reset Time", "00:00:00")
    7.     If s = "" Then Exit Sub
    8.     If IsDate(s) Then
    9.         dBaseTime = s
    10.     Else
    11.         MsgBox "Time was not reset.", vbExclamation
    12.     End If
    13.  
    14. End Sub
    15.  
    16. Private Sub Timer1_Timer()
    17.     dBaseTime = DateAdd("n", 1, dBaseTime)
    18.     Label1 = Format(dBaseTime, "hh:mm:ss")
    19. End Sub
    20.  
    21. Private Sub UserControl_Initialize()
    22.     With Label1
    23.         .Font.Name = "Courier New"
    24.         .Font.Size = 14
    25.         .Font.Bold = True
    26.         .Alignment = vbCenter
    27.         .BackColor = vbGreen
    28.     End With
    29.     Timer1.Interval = 1000  'One second
    30.     dBaseTime = Time
    31. End Sub
    32.  
    33. Private Sub UserControl_Resize()
    34.     Label1.Move 0, 0, UserControl.Width, UserControl.Height
    35. End Sub

    Now save the project somewhere. Then click File > Make Project1.ocx

    That's all. the OCX is created.

    Now how do you test this.
    Click File > Add Project... Then Choose to add a Standard EXE. The last control you see here is your control. (If it is not available, first close the Usercontrol's object window.)
    Add this control to your Form.
    U r almost done!

    In the Project Explorer Window, right click your EXE Project (Project2) and choose "Set as Start Up"

    Save everything and Press F5 to see the result.

    Double click your control and it will give you option to reset the time.


    This is just a simple demo, but just sufficient to start off.
    After this step by step you can start defining more events, methods, properties, classes, add more forms and functionality...

    Hope this helps.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: usercontrols. something i have never touched

    Pradeep, you have taken some considerable time to write that out and i would like to thank you for that. i will look into it.

    anybody else like to add to this.

    thank you.
    casey.

  4. #4
    Addicted Member
    Join Date
    May 2005
    Posts
    168

    Re: usercontrols. something i have never touched

    Hi,

    Another source is if you have VB6 Enterprise along with the code Samples installed they have a user control project that you can use.

    Another option is to use the VB6 ActiveX Control Wizard and see how a user control is "built".

    Have a good one!
    BK

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: usercontrols. something i have never touched



    Has someone helped you? Then you can Rate their helpful post.

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