Hi Friends
Today we will see How to create your very own User Control ,
Just follow these few steps and in the end U will end up with your very own User Control With Your defined Properties and events
1.Create a new project by clicking on to
FILE --> NEW PROJECT
Now wait a second and read carefully here we are not going to create new Windows application we are here to create USER CONTROLS for your windows Application, hence select the option named WINDOWS CONTROL LIBRARY as shown below
2. As soon as you Click Ok you will see the Design Window will Look like this
This is the control on which we are going to put some controls to create a new Control
//*In this Example We will create Menu Control which will Contain buttons such as Add, Modify and more, But you can use any combination of controls u like.*//
3. Now put 6 Buttons on your Control and make your Control Look Like this
Note: I have put some Pictures on the Buttons, You can also have simple text
4. Now Dbl click on to your user control to go to Code window
Guys a bit of theory ,to your surprise at this moment when u haven’t written a single word in your code if you build your project (Before that U have to save it ) and register your component (Which we will see at the end of article) and when you use the component in your new product you will found that your control already have too many events to handle this is because this control inherits all the events and properties of the controls we used on it ,But since this is our Control So we want to have full control over it and that can be done by having our own properties and events .
a. How to have your own properties?
Properties can be given by simply writing that GET SET code a small example is given below
Public Property sanText() As String
Get
End Get
Set(ByVal Value As String)
End Set
End Property
Public Property sanLbl() As String
Get
End Get
Set(ByVal Value As String)
End Set
End Property
Note : I am not using any properties in this example ,this is just a example to show u that this is simple.
b. How to have your own Event?
First, the simplest way to create an event is to simply declare it in your class (in this case, your custom control class), like in the following code snippet
Public Class MenuControl
Public Event addclick()
End Class
As you can see, I have declared an event using the Event keyword-
Public Event addclick()
After adding a user control or modifying one, you have to rebuild your project in order to see the changes appear in the designer.
Adding Event Handlers
After rebuilding, you should see your user control appear in the "My User Controls" tab of the Toolbox. You can then drag an instance of this control to a form. Once you've done this, open the form in code view. In the upper left corner of the code screen, select the name of your control (in my case, it's MenuControl ) from the list. Then you'll be able to view all the events associated with that control in the drop-down list on the right.If you created an event called AddClick in your control, you'll see it in the list- you'll also see one called Click, which is a standard event associated with all Windows controls.
NOTE: You should not name your event "Click" or it will cause a conflict with the base class'
Click event.
If you want to create a handler for your custom AddClick event, you should select the name of the control from the code window, then the event, which will cause the IDE to automatically generate an event handler for you. You could also manually enter the event handler code, which would look like this:
Private Sub MenuControl_addclick() Handles MenuControl1.addclick
msgBox “Add click event is being Called.”
End Sub
The key portion to this is the "Handles" keyword. It tells VB.NET that this method will be invoked automatically by the runtime when your control fires its AddClick event. The method can have any name you like- it does not have to be "Menucontrol_Addclick ", although this is the usual convention. However, the method's "signature" must be the same as the event that it is handling- meaning that the number of arguments and types of arguments must be identical. If they are not, the IDE will tell you that the method signature does not match.
Raising (Firing) Events
At this point, we've created a User Control, added an event to that control, added that control to a form, and have implemented an event handler to handle our custom event. However, one thing is missing-currently , the event won't ever be fired! If you tried to test this,you may have noticed that the MessageBox call we placed in the method above is never invoked.In order for a control (or any class) to raise an event, it must use the "RaiseEvent" statement. Since we want this event to be raised when the button is clicked, we must add an event handler to the control which will handle the button's Click event and raise our event in turn.
The following code snippet demonstrates this:
Private Sub btnAdd_Click(ByVal sender
As System.Object, ByVal e As System.EventArgs)
Handles btnAdd.Click
RaiseEvent addclick()
End Sub
Remember that this method is added to the control, not the form! Note the Handles clause for this method- it's going to get the Click event for the button on the User Control and perform some operations if need be, then will fire (raise) our custom event. Don't forget to rebuild your project for the changes to appear to the forms using the control.
Declaring Events with Arguments
You can also pass arguments with events, just like with any method or function call. In order to do this, you would add the arguments you wanted to the Event declaration:
-----------------------
Public Event AddClickWithArgs(ByVal ClickTime as Date)
--------------------------
In order to raise this event, you would have to do something like this:
-----------------
RaiseEvent AddClickWithArgs(Now)
-----------------
5. How to register your Control so that u can use it on to your diff projects by creating instances
Writ Click on your Toolbox you will see the option Named CHOOSE ITEMS click on to it as shown below
A form named CHOOSE TOOL BOX will appear on to the screen as shown below
Click on to browse button to locate your directory where your Project is stored , in your Project
Just follow the link (I:\vbExampels\MenuControl\MenuControl\bin\Release)and you will found the dll file U created When you blid Your Project if it is not there it means it is you have not build your Component which can be done by clicking on to the Build Menu and selecting Build Component.Select this dll file and press ok the component come in the Chose toolbox item with it’s check box clicked.Now press Ok of this form And Your Control will be seen on to the tool box and is very much ready to use in diff projects as many times as required.
/*Hopefully this article will help u out in your journey to learn VB.NET*/





1 comment:
bet of 1xbet korean - legalbet.co.kr
Bet of 1xbet 바카라 korean. 1xbet. 1xbet. 1xbet. bet of 1xbet. 1xbet. Bet of 1xbet korean 1xbet. Bet of choegocasino 1xbet. 1xbet. 1xbet. 1xbet.
Post a Comment