First start a new project in vb then select Project Explorer , normally top of the properties.Right click on the Project 1 and select Add - User control.Here you can create new or open existing Controls. Now click new User-control.
It is just like a vb form ,you can add buttons , labels , anything you need like a normal form.
When you added a new user-control then you can see it in tools.
To get your control then close the User-control then open the first form , now add your control form the tools.Please remember that it will not work if you open the User-control.Here i made normal References , nothing more ...
Create Own User Control :
Close all forms and controls , open the new User-control ..
Add one image control ,change its properties to :
Name = img
Height = 500
Width = 2000
Stretch = True
Add one Label
Changing name of user-control:
Just change the name property like a form.Set it to MyImage.If you add your controlto your form then it will appear like MyImage1.
The first and important part of a user-control is its properties , events and Methods.It is easy to create all.But the first thing to change is its Initialize and Resize.To do this Double click on the user-control then in code add this :
Private Sub UserControl_Initialize() UserControl.Height = 500 UserControl.Width = 2000 End Sub Private Sub UserControl_Resize() img.Width = UserControl.Width img.Height = UserControl.Height lbl.Width = UserControl.Width lbl.Height = UserControl.Height End Sub
Here Initialize is like form load.Every code in Initialize works on when you create a new user control.
Resize will work when you change the size of user-control.Here i used code to adjust the height and width of image control and label control when the user change the size of the control.Else if the user change the size of the control then it will become a disorder.
After that you can set its properties.Properties are visible in the properties window , it will allow users to access any part of the control like setting the control enable or disable or changing the caption.You can access the property of a control in coding.
To create a new Property :
In code view In menu bar : Take tools and click Add Procedures , then select Property [ it will create a new property ] and type " Caption " in that box.
Then you will get this
Public Property Get Caption() As Variant End Property Public Property Let Caption(ByVal vNewValue As Variant) End Property
Here it will set a Property called caption like Label caption.Using this property you can use it to do what you think.Here i am using this property to display the caption of the Label - lbl and load that image file id to the image control - img.
For this add these lines :
Public Property Get Caption() As Variant Caption = lbl.Caption End Property Public Property Let Caption(ByVal vNewValue As Variant) lbl.Caption = vNewValue PropertyChanged "Caption" End Property
Here it will change the caption of the label when you typed a text in the caption property of the user control. [do it in form view , after closing the control]You can use the property like normal label : UserControl11.Caption = "Button Click"
But the new property Caption is a temporary , if you run the program the caption will change back.To stop this just change the user-control 's WriteProperties & ReadProperties.Now in code view add this :
Private Sub UserControl_WriteProperties(PropBag As PropertyBag) PropBag.WriteProperty "Caption", lbl.Caption End Sub Private Sub UserControl_ReadProperties(PropBag As PropertyBag) lbl.Caption = PropBag.ReadProperty("Caption", lbl.Caption) End Sub
Now the caption will become permanent.If you want to add image to your image control then Change the Caption and ReadProperties code to this
Public Property Get Caption() As Variant Caption = lbl.Caption End Property Public Property Let Caption(ByVal vNewValue As Variant) On Error Resume Next lbl.Caption = vNewValue PropertyChanged "Caption" img.Picture = LoadPicture(lbl.Caption) End Property Private Sub UserControl_ReadProperties(PropBag As PropertyBag) On Error Resume Next lbl.Caption = PropBag.ReadProperty("Caption", lbl.Caption) Set img.Picture = LoadPicture(lbl.Caption) End Sub
Now it will load image when you give the file id to your caption.If it is a wrong file name then nothing will happen , for that i am used On Error Resume Next.Now you can change the image of the control at run time just call the Caption property and give a value.
Adding a method will help you to do more works in user-control in form or at run-time.If you want to add a new method then use this codes:
Take tools and click Add Procedures , then select Sub [ it will create a new method ]Type " check " in that box.The result will be :
Public Sub check() If img.Picture = False Then lbl.Caption = "No Image" End Sub
Here i used a new method to check that the image contains any image or not.The real use of such methods appear when you need to check it at run time.For calling one method open form add a button and in code view just add one line like this : UserControl11.check , the result will be " No Image " . When you run the program and click the button then it will call the method.
Adding an event
Events appear when you double click them or selecting from a combo [ top side in the code view].Events are used to do action when the user did something. for example Click event , it will activate the code in that event when you click that control.Adding an event is like adding a new method .To Add a new event :
Take tools and click Add Procedures , then select Event [ it will create a new method ]
Then you will get : Public Event Click()
Add this to your code.It will call the click event when user clicks the label or control.
Private Sub lbl_Click() RaiseEvent imgClick End Sub Private Sub UserControl_Click() RaiseEvent imgClick End Sub
To access the new event , close all forms and controls , open the first form, and add one new control and double click it then you can see the new event imgClick.
Like click you can add any event for your control.To make your control more useful it is necessary to create more properties like Enable , setting font ....You can add any properties you like but the important thing is you must change it or add codes in ReadProperties & WriteProperties.
Enable \ Disable : Allow you to make a control enable or disable.
Public Property Get Enabled() As Boolean Enabled = m_bEnabled 'declare m_bEnabled before Private m_bEnabled As Boolean End Property Public Property Let Enabled(ByVal New_Enabled As Boolean) m_bEnabled = New_Enabled UserControl.Enabled = m_bEnabled PropertyChanged "Enabled" End Property
Font : will change the font of the caption.
Public Property Get Font() As StdFont Set Font = UserControl.Font End Property Public Property Set Font(ByVal New_Font As StdFont) Set UserControl.Font = New_Font Set lbl.Font = New_Font Refresh PropertyChanged "Font" End Property
Change the ReadProperties & WriteProperties to make the property working.
Private Sub UserControl_ReadProperties(PropBag As PropertyBag) On Error Resume Next lbl.Caption = PropBag.ReadProperty("Caption", lbl.Caption) Set img.Picture = LoadPicture(lbl.Caption) With PropBag Set UserControl.Font = .ReadProperty("Font", Ambient.Font) Set lbl.Font = .ReadProperty("Font", Ambient.Font) m_bEnabled = .ReadProperty("Enabled", True) UserControl.Enabled = m_bEnabled End With End Sub Private Sub UserControl_WriteProperties(PropBag As PropertyBag) PropBag.WriteProperty "Caption", lbl.Caption PropBag.WriteProperty "Caption", lbl.Caption With PropBag .WriteProperty "Enabled", m_bEnabled, True .WriteProperty "Font", UserControl.Font, Ambient.Font End With End Sub
You can make the control enable or disable and change the font of the caption
The full code i used to create this control is given. vbtutorial_control.txt (2.08K)
Number of downloads: 5401
Best Of Luck ...