mobileNavlogo
headerlogo

Blog

Working with user Defined controls, Properties, events

12 September, 2018

Related Blogs

Working with user Defined controls, user defined Properties and user defined events

EMS: Employee manager system     Screenshot_9 LMS: Library management system   Screenshot_30   Screenshot_31  
  1. When a predefined control is not providing users requirement, then user defined control need to be developed
  2. To develop user defined controls, NET Introduced
“Windows forms control library project template”
  1. User defined controls will be created in the format of DLL’S
  2. User defined controls contains application Logic along with GUI.
  3. User defined controls are reusable (write once ute many times)
  4. The controls developed in c# can be used in vb also. But not in ASP.NET
  Screenshot_32     IIS: Internet information service ASPX: Afire server pages extension Screenshot_33
  1. 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.   Screenshot_24User defined control is a class, which must be inherited either from user control class or form any exiting control Screenshot_24Based on above statement, user defined controls are divided into two types  

General UC                                     Inherited UC

User Control                                                      Text box Text                                                                       Text Screenshot_24àWhen the base class name is user control, then it is called as “General User control”. Screenshot_24When 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:

  Screenshot_34     Screenshot_24Open 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”); } Screenshot_24Adding an Icon Viewà Solution ExplorerScreenshot_24Rt click on login textScreenshot_24 Add Screenshot_24New item Screenshot_24Bit 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.  

Screenshot_24Build the project(build menuà build solution)

 

OBS:

Login text.dll will be created under F:/0194/ Login text/ Bin/debug folder Screenshot_24Open WFAP Screenshot_24Open tool boxàRt Click inside of tool boxàadd tapà gire the name hy controls Screenshot_24Open tool boxàRt Click inside of my controls tabàchoose itemsàbrowseàlogin text. Dll (or) Drag login text. Dll file into Tool box Screenshot_24 my control tab  

Obs:-

A New control will be added with the name login Screenshot_24Place a login control on to the form    

Obs:-

  1. 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
  2. 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
  1. Hence user defined propertied need to be developed
 

Working with user defined controls and user defined properties:-

A =10Screenshot_24 Set                      Property B=AScreenshot_24GET Property
  1. A Collection of and get functions is called as “property”.
  2. A property with get function is called as read – only property.
  3. 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:

  1. Q) Developing a scrollable user control with 30 Forms
    Screenshot_35  
  1. Q) Developing a scrolling user control with Ltext, Lfont ,LB color and L Seep properties
Screenshot_24Open windows forms control Library project with project name scrollable control Screenshot_24Place a label control. Screenshot_24Place a timer with enabled = true Screenshot_24Place 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;} } Screenshot_24Build 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

  Screenshot_36  
  1. Inherited user control is required to add extra functionality for an existing control.
  2. In the base class name is user control, then it is called as General user control
  3. If the base class name is any existing control, then it is called as inherited user control.
  4. 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   Screenshot_37 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

Screenshot_24Developing a user defined text box with a new property called as input type Screenshot_24If input type is A or a ,then text box need to accept only Alphabets Screenshot_24If input type is D or D, then text box need to be accept only digits. Screenshot_24 If input type is All then text box need to accept all characters. Screenshot_24open windows forms control library project with project name UDT. Screenshot_24Open 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’)
  1. Handled=false;
else
  1. Handled =true;//this statement
Cancels the Key stoke } If(S==”D”\\S==d) { If(e.key char>=’o’&& e.key char<=’9’)
  1. Handled = false;
else
  1. 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   Screenshot_38   Execute the project and check either text boxes are accepting proper input or not.  

Working with user controls and user defined events

Screenshot_39   Screenshot_24 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

  Screenshot_40   Screenshot_24open windows control library with project name UDE Screenshot_24Place button Screenshot_24Code 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.
About Author
Authorlogo
Name

TekSlate

Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .