Working with user Defined controls, user defined Properties and user defined events
EMS: Employee manager system
LMS: Library management system
- When a predefined control is not providing users requirement, then user defined control need to be developed
- To develop user defined controls, NET Introduced
“Windows forms control library project template”
- User defined controls will be created in the format of DLL’S
- User defined controls contains application Logic along with GUI.
- User defined controls are reusable (write once ute many times)
- The controls developed in c# can be used in vb also. But not in ASP.NET
IIS: Internet information service
ASPX: Afire server pages extension
- User defined controls are Not. Self executable; there are form dependants.
1 Steps for developing user defined controls:
Step1
open windows forms control library project, then design the control as required and write the logic
Step2
Add an Jcon(optional),Build the project to get a DLL file.
Step3
open windows forms application project and add the DLL file into the Tool Box.
Obs:-
A new control will be added into the Tool Box
Step4
place a new control on the form add use it as per the requirement.
User defined control is a class, which must be inherited either from user control class or form any exiting control
Based on above statement, user defined controls are divided into two types
General UC Inherited UC
User Control Text box
Text Text
àWhen the base class name is user control, then it is called as “General User control”.
When the base class name is any existing control name like text box, button etc, then it is called as “Inherited” user control”.
Developing login user control:
Open windows forms control Library project with project name login text.
(Out put file is login text.Dll)
Choose properties and set name = login (User defined control)
(This name will be displayed in the tool box after adding the control)
Lace two labels, two text boxes and a button as shown code for button1_click
If (text box1.text ==”teja”&&text box2. Text==”abc”)
Message box. show(“valid user”);
Else
Message box. show(“not a valid user”);
}
Adding an Icon
Viewà Solution ExplorerRt click on login text
Add
New item
Bit map and provide the name as login.bmp
Note:
user control name and BMP file names must be same.
Choose properties and ht width =16 and height=16
Design the bitmap as required
Choose the properties of login.bmp file and set build action= embedded resource.
Build the project(build menuà build solution)
OBS:
Login text.dll will be created under
F:/0194/ Login text/ Bin/debug folder
Open WFAP
Open tool boxàRt Click inside of tool boxàadd tapà gire the name hy controls
Open tool boxàRt Click inside of my controls tabàchoose itemsàbrowseàlogin text. Dll
(or)
Drag login text. Dll file into
Tool box my control tab
Obs:-
A New control will be added with the name login
Place a login control on to the form
Obs:-
- In above user control, two text boxes and two labels and a button controls are integrated together and a new control is formed with the name login
- in general user control. Individual controls properties will not be inherited.
I.e, In above example it is not possible to change the text of labels, text boxes or button
- Hence user defined propertied need to be developed
Working with user defined controls and user defined properties:-
A =10 Set Property
B=AGET
Property
- A Collection of and get functions is called as “property”.
- A property with get function is called as read – only property.
- Syntax to write a property
Data type variable;
Public data type pName
{
Set
{
Variable = value;
}
Get
{
Return variable;
}
}
Example on user defined properties and user defined controls:
- Q) Developing a scrollable user control with 30 Forms
- Q) Developing a scrolling user control with Ltext, Lfont ,LB color and L Seep properties
Open windows forms control Library project with project name scrollable control
Place a label control.
Place a timer with enabled = true
Place one more timer with enabled= false .
Code for hmer1_tick
{
Label1.Top= Label1.Top-5;(It will scroll upwards)
If(Label1.Top<0)
{
timer 1.Enabled = false;
timer 2.Enabled = True;
}
}
àCode for timer2-Tick
{
Label1.Top=Label1.top+5);
If(Label1.top>350)
{
Timer1.enabled=true;
Timer2.enabled=false;
}
}
àcode in GD(Initialize component)
//LtextàLabel1.textàString
Public string L Text
{
Set{Label1.Text=value;}
Get{return Label1.Text;}
}
//LFontàLabel1.FontàFont
Public Font LFont
(or)
Public Font1 Font
{
Set{Label1.Font=value;}
Get{return Label1.Font;}
}
//LBcoloràLabel1.Back Coloràcolor
Public color LB color
{
Set{Label1.Back clor=value;}
Get{return Label1.back color;}
}
//LSpeedàTimer. Intervalàint
Public int LSpeed
{
Set
{
Timer1.Interval=value;
Timer2.Interval=value;
}
Get
{return timer1.interval;}
}
Build the project(Build menuàbuild solution)
Bs:-
Scrollable control.dll is created under
D:\c194\Scrollable control\Bin\Debug Folder
With a new control named “User control1”.
Open WFAP
àDrag scrollable control.DLL into tool boxàMy controls tab
àThen a new control with name user control 1 will be added into the tool box place this control on the form and check Ltext, Lfont, LB color and Lspeed properties.
Working with Inherited user controls
- Inherited user control is required to add extra functionality for an existing control.
- In the base class name is user control, then it is called as General user control
- If the base class name is any existing control, then it is called as inherited user control.
- In inherited user control, all the properties will be inherited from base control
IN UC
Text box-----40 properties
Text_______1 properties
41 properties
Genera LU Control
User control
Test
Test =” ABC”
Generally inherited user control’s class declaration will be as follows
Public partial class test: Text box
{
}
In inherited user controls only application logic allowed but placing some other controls are not allowed
Example on in H User control
Developing a user defined text box with a new property called as input type
If input type is A or a ,then text box need to accept only Alphabets
If input type is D or D, then text box need to be accept only digits.
If input type is All then text box need to accept all characters.
open windows forms control library project with project name UDT.
Open code window by pressing f7(don’t double click on the form to open code window)
Change the class declaration as follows
Public partial class user defined control: Text box code in GD
String S =”ALL”;//This is the default value for input type property.
Public user control()
Public String Input type
{
Set{S=Value;}
Get{return S}
}
Code for user control – Key press Event
Private void user control – key press(--)
{
If(s==”A”// S ==”a”)
{
If(e.kdy char>=’A’&& e. key char=’Z’)
//e.key char>=’a’ && e. key char=’Z’)
- Handled=false;
else
- Handled =true;//this statement
Cancels the
Key stoke
}
If(S==”D”\\S==d)
{
If(e.key char>=’o’&& e.key char<=’9’)
- Handled = false;
else
- Handled = true;
}
Build the project(Build menuà Build solution)
Obs:-
UDT.DLL will be created under
E:\c194\UDT\bin\debug folder
Open windows forms application project
àDrag UDT.DLL into tool boxàMy controls tab, then a new control will be added with the name user control
Design the form As shown
Execute the project and check either text boxes are accepting proper input or not.
Working with user controls and user defined events
A call back procedure is also called user defined events
- When part of user controls logic and part of form’s logic need to be executed at one, shot, then user defined events are required.
- User defined events are also called as call back procedures.
- User defined events can be created with in 4 steps
Step1: Create a Delegate
Syntax:
Public delegate void delegate name(or) Dname()
Step2:- Create an event with the help of the delegate
Syntax
Public event D Nam Event name;
Step3 :Raise event or call the event
Syntax:
Erent name()
Step4: Define the event(Write the body)
Prirate void control name enent name()
{
Logic;
}
- Step1,2 and 3 will need to be followed while developing the control(windows forms control library project) These steps are followed by Microsoft
- Step4 need to be followed while using the control (windows forms application project)
Example on user defined event
open windows control library with project name UDE
Place button
Code in GD
Step1:
Public d delegate void D name();
Step2:
Public event D name Abc;
Code for Button1-click
Message box. show(“from uses control”)
Stpe3:
Abc();
Build the project(Build menuà build solution)
UDE.Dll will be crated under
E:\c194\UDE\bin\debug
àopen windows forms application project
- Drag UDE.DLL into tool boxàMy controls
Tabà place an added control on the form
(user control 1)
àCode for user control ABC event
Message Box. show(“from form”);
- Execute the project and click on the button, then two message boxes need to be displayed.