With the help of Windows Form Control Library user can create own control. In this demonstration creating user control that will perform mathematical operation on two numbers. |
How to create Windows Control Library |
Step:
- Select Window from Installed Templates in Visual C#
- Select Window From Control Library
- Click OK
After click OK button new Window will appear, on user control simply drag and drop four button on it and button will be “Add”,”Subtract”,”Multiply” and ”Division” as shown below:
To create user control
Code:
publicpartialclassUserControl1 : UserControl{publicUserControl1(){InitializeComponent();}intnum1=0, num2=0, res=0;//declaring three member variables of int typeControlctrl1, ctrl2, ctrl3;//declaring three member variables of Controlstringname1, name2;//declaring three member variables of string that will store textBox nameprivatevoid UserControl1_Load(objectsender, EventArgse){}publicstringTextBoxName1//creating property for textBox{get{returnname1;//this line will return textBox name}set{name1=value;//this line will set textBox name}}publicstringTextBoxName2//creating property for textBox{get{returnname2;//this line will return textBox name}set{name2=value;//this line will set textBox name}}privatevoidbtnAdd_Click(objectsender, EventArgse){ctrl1= (TextBox)FindForm().Controls[name1];//in this line FindForm() method will find textBox control from the Form where the name of textBox is store in name1ctrl2= (TextBox)FindForm().Controls[name2];//in this line FindForm() method will find textBox control from the Form where the name of textBox is store in name2if (ctrl1==null|| ctrl2==null)//checking condition{MessageBox.Show("TextBox can not blank");//it will show messagereturn;//return the execution}try//{MyControl();//this a functionres =num1+num2;//adding variable value and storing in resDisplay();//this a function}catch (Exceptionex){MessageBox.Show(ex.Message);//it will show message if exeception occurs}}privatevoidbtnSub_Click(objectsender, EventArgse){ctrl1= (TextBox)FindForm().Controls[name1];ctrl2= (TextBox)FindForm().Controls[name2];if (ctrl1==null|| ctrl2==null){MessageBox.Show("TextBox can not blank");return;}try{MyControl();res =num1-num2;//storing value in res after subtractionDisplay();}catch (Exceptionex){MessageBox.Show(ex.Message);}}privatevoidbtnDiv_Click(objectsender, EventArgse){ctrl1= (TextBox)FindForm().Controls[name1];ctrl2= (TextBox)FindForm().Controls[name2];if (ctrl1==null|| ctrl2==null){MessageBox.Show("TextBox can not blank");return;}try{MyControl();res =num1/num2;//storing value in res after divisionDisplay();}catch (Exceptionex){MessageBox.Show(ex.Message);}}privatevoid UserControl1_Load_1(objectsender, EventArgse){}publicvoidMyControl(){ctrl3= (TextBox)FindForm().Controls["textBox3"];//in this line FindForm() method will find textBox control from the Form where the name of textBox is textBox3num1=Convert.ToInt32(ctrl1.Text);//Converting in integer and storing in num1num2=Convert.ToInt32(ctrl2.Text);//Converting in integer and storing in num2}publicvoidDisplay(){ctrl3.Text= Convert.ToString(res);//in this line res is storing as a text in textBox3}privatevoidbtnMul_Click_1(objectsender, EventArgse){ctrl1= (TextBox)FindForm().Controls[name1];ctrl2= (TextBox)FindForm().Controls[name2];if (ctrl1==null|| ctrl2==null){MessageBox.Show("TextBox can not blank");return;}try{MyControl();res =num1*num2;//storing value in res after multiplicationDisplay();}catch (Exceptionex){MessageBox.Show(ex.Message);}}}
For adding user control in ToolBox:
Step:
- Right Click on the Toolbox
- Select Choose Items
After the “Choose Items” new toolbox “Choose Toolbox items” will be open click on the browse button and you have to specify where user control folder is.
After Select user control folder follow this step:
- Open folder
- Open sub folder (bin)
- Open bin sub folder (Debug)
- Open Debug folder
- In Debug folder open user control ( .dll)
- Then click on OK
After that we can see user control(UserControl1) has added in ToolBox, when we want to use this user control simply drag and drop on the form as shown below:
This demonstration control can perform mathematical operation (Add, Subtract, Multiply and Divide) and result will be show in result TextBox as shown below.
Code
publicpartialclassForm1 : Form{publicForm1(){InitializeComponent();}privatevoid textBox1_TextChanged(objectsender, EventArgse){userControl11.TextBoxName1=txtName1.Name;//this line will take textbox name and store in user control property TextBoxName1}privatevoid textBox2_TextChanged(objectsender, EventArgse){userControl11.TextBoxName2=txtName2.Name;//this line will take textbox name and store in user control property TextBoxName2}privatevoidForm1_Load(objectsender, EventArgse){}privatevoid textBox3_TextChanged(objectsender, EventArgse){}privatevoid userControl11_Load_3(objectsender, EventArgse){}}
Anonymous User
07-Mar-2019Thank You for the informative post.