|
-
Jun 11th, 2005, 04:28 PM
#2
Re: usercontrols. something i have never touched
 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:
Option Explicit
Dim dBaseTime As Date
Private Sub Label1_DblClick()
Dim s As String
s = InputBox("Enter New Time (hh:mm:ss):", "Reset Time", "00:00:00")
If s = "" Then Exit Sub
If IsDate(s) Then
dBaseTime = s
Else
MsgBox "Time was not reset.", vbExclamation
End If
End Sub
Private Sub Timer1_Timer()
dBaseTime = DateAdd("n", 1, dBaseTime)
Label1 = Format(dBaseTime, "hh:mm:ss")
End Sub
Private Sub UserControl_Initialize()
With Label1
.Font.Name = "Courier New"
.Font.Size = 14
.Font.Bold = True
.Alignment = vbCenter
.BackColor = vbGreen
End With
Timer1.Interval = 1000 'One second
dBaseTime = Time
End Sub
Private Sub UserControl_Resize()
Label1.Move 0, 0, UserControl.Width, UserControl.Height
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
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
|