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