Toolbox in visual studio
The toolbox is a window or bar which contains different tools which are called controls which you can add to your projects. Toolbox is not used in console applications projects, however it is used in projects like Windows Form Application, Windows Presentation Foundation (WPF) , ASP.NET Web Forms, Windows Universal apps et. The set of controls which display on toolbox, depend on the project which is created, and the version .Net Framework selected. Mostly the toolbox will display in conjunction with designer windows and display only those controls which can be used in current designer. Such as if the designer window, we are working with is in Asp.net web forms application, the toolbox contains all the controls which can be used in web forms designer window, if the designer window, is in windows form application, then the toolbox contains all tools that can be used in windows form application. In come cases toolbox doesn't contain any control, mostly when application is running or while working with code window.Toolbox in C# widows form application project |
Controls in C#
All the tools available in toolbox are called controls. Most of the controls derive form System.Windows.Forms.Control class, The Control class define the basic functionality of controls, hence many of the properties and events of controls are same. Many classes which derive from control class are base classes themselves for other controls such as TextBoxBase class is base class of TextBox class and RicthTextBox class.
Some controls are custom controls called user controls which derive from another class System.Windows.Forms.UserControl. The System.Windows.Form.UserControl provide functionality to create our own controls and it itself derives from Control class. All the controls have properties and events , and some controls have methods also.
Some controls are custom controls called user controls which derive from another class System.Windows.Forms.UserControl. The System.Windows.Form.UserControl provide functionality to create our own controls and it itself derives from Control class. All the controls have properties and events , and some controls have methods also.
Categories of controls in toolbox in windows form application project
By default toolbox contains all necessary builtin controls, We can add other controls to toolbox, delete existing controls from toolbox, copy control, cut controls and paste controls in toolbox by right clicking any where on tool box. We can also create our own controls and then they can be added to toolbox. The controls in toolbox are organized into different categories, each category contains group of related controls. The categories of controls in toolbox is in the form of various tabs in toolbox. We can add new tabs, we can remove tabs and also we can rename tabs in toolbox. We can also change the order of items in toolbox by right clicking on toolbox and then choose sort items alphabetically. The default categories in C# windows form application projects as shown below:- All Windows Forms
- Common Controls
- Containers
- Menus and Toolbars
- Data
- Components
- Printing
- Dialogues
- WPF Interoperability
- General
All Windows Forms
The category tab shows all the controls which can be used in Windows form application.
All windows forms control in C# windows form application project |
Common controls
This category tab contains commonly used controls such as label control, textbox control, button control, RadioButton control, CheckBox control, ComboBox Control etc.
Common Controls in windows form application project |
Containers
This category tab contains those controls which are used to hold other controls, such as Panel control, GroupBox control , Tab Control etc.
Container controls in C# windows form application project |
Menus and Toolbars
The category tab contains the tools which are used to create menu bars, menus and tool bars. such as MenuStrip control, ToolStrip control, ContextMenuStrip control etc.
Menus and Toolbars controls in C# windows form application project |
Data
This category contains controls, used to work with data and databases. such as DataGridView, DataSet, BindingSource etc.
Data Controls in C# windows form application project |
Components
This category tab contains component controls, which will not display on designer window at design time, but they produce result after execution. Some controls of this category shows result in foreground and some shows result in background. The examples of components available in this category are, BackgroundWorker Component, Timer Component, ImageList component, Service component etc.
Components controls in C# windows form application project |
Printing
This category tab, contains the tools which are used for print functions, such as PrintPreview Dialog, PrintPreviewControl, PageSetup dialogue , PrintDialogue etc.
Printing controls in C# windows form application project |
Dialogues
This category tab contains the controls which are used to work with dialogue boxes in the applications, such as ColorDialogue Control, OpenFielDialog, SaveFileDialog etc.
dialogs controls in C# windows form application |
WPF Interoperability
This category tab, contains the ElementHost control, which is used to use WPF control in Windows Form Application.
wpf interoperability controls in C# windows form application |
General
By default this category is empty, you can add general items to this category.Managing toolbox and its controls
1. If you want to mange any tab, such as All Windows Forms, right click on it.
right click short cut menu to manage toolbox tab |
2. If you want to manage any control, such as Button control , right click on it
shortcut menu to mange toolbox controls |
3. If you want to manage tool box right click on empty area of toolbox
Shortcut menu to manage visual studio toolbox |
All the above shortcut menus of toolbox contains following options,which you can use to mange toolbox, toolbox tab and toolbox controls.
- Cut: Used to cut control
- Copy: Used to copy control
- Paste: Used to paste control which have cut or copied.
- Delete: Used to delete control from toolbox.
- Rename Item: Used to rename control of toolbox.
- List View: If you check this option, toolbox controls will be show in list view, if you un-check this option, controls will be displayed in a grid view in toolbox/
- Show All: Show all controls and tabs in toolbox
- Choose Items...: Used to add controls to toolbox
- Sort Items Alphabetically : used to change order of controls
- Reset toolbox: Used to reset toolbox.
- Add tab: Used to add a new tab in toolbox
- Delete tab: used to delete selected tab from toolbox.
- rename tab: Used to change name of selected tab.
- Move up: Used to move control up
- Move down: Used to move control down.
Adding controls in visual studio toolbox
Right click any where on toolbox than click option "Choose Items", which shows following dialogoue box , it loads all controls in few seconds. Choose controls which you want to add in toolbox then click OK. I am selecting ToolStripDropDownMenu and ToolStripMenuItemArray, as shown in following figure.
dialog box used to add controls in visual studio toolbox |
Now you can notice selected controls has added in toolbox
controls added in visual studio toolbox |
Using controls in windows form project
There are two ways to use controls in windows form project.- Creating Controls at Design Time
- Creating Controls at Run Time
Creating Controls at Design Time
To create controls at design time, just double click required control or drag and drop it onto from designer window from toolbox. Form Example , first make sure you are in form designer view, than from "All Windows Forms" category double click Button control , and Label control, or you can do the same thing by dragging these controls from toolbox and drop to form designer window at any position. The difference between adding controls with double click and adding controls by drag and drop is that double click will create all controls at same position, then we can change the position ourselves and with drag and drop, controls can be placed any where onto form designer window. You can see Button and Label controls are added onto form designer window, as shown in following figure.
creating controls at design time |
Changing Controls Position
Move Mouse over to control, now the shape of cursor will be four headed arrow, as show in following figure
button with four headed cursor |
Now Press and hold down, mouse left button and move cursor to other position where you want your control to be placed, then release the mouse left button.
Changing Controls Size
Some controls will not allow change their size by default, such as label control, if its AutoSize property is true, then we can't to change its size. To change the position of such controls is two step process. For Example if you want to change the size of label , First click label and then from properties window, set its AutoSize property to false as shown in following figure.
AutoSize property of label is false to change size of label in windows forms |
Now if you click label , small circles will be appeared around label, which are used to change its potions.
If any control allow you to change size , when you click on it small circles will be appeared around that control, which are used to change its size.
Changing Controls Text
We can change text which appear on control, through "text" property in properties window. For Example if you want to set the text of button as "Click Me", follow the following steps:
- Click on button
- Goto Properties window
- In Appearance category , Change Text , of Text Property which you want, in my case I am specifying "Click Me"
text property in properties window |
- Now You can see Text of Button has changed.
Button Text Changed |
Changing Control Background Color
You can use BackColor property in properties window to change background color of any control. The BackColor property is available in Appearance section of properties window
Change Controls Foreground Color
You can use ForeColor propety in properties window to change foreground color of any control. The ForeColor property is available in Appearance section
Creating Controls at Run Time
1. Creating a new windows form project
2. To Open Code window, from view menu click Code option or press F7 from keyboard.
3. Code window will open as shown below
Code window in windows form application project |
Button btn1 = new Button();
btn1.Text = "This is button1";
btn1.SetBounds(20, 50, 200, 40);
btn1.BackColor = Color.PowderBlue;
this.Controls.Add(btn1);
Label lb1 = new Label();
lb1.Text = "This is label 1";
lb1.SetBounds(20, 100, 200, 40);
lb1.BackColor = Color.SeaGreen;
this.Controls.Add(lb1);
public Form1()
{
InitializeComponent();
Button btn1 = new Button();
btn1.Text = "This is button1";
btn1.SetBounds(20, 50, 200, 40);
btn1.BackColor = Color.PowderBlue;
this.Controls.Add(btn1);
Label lb1 = new Label();
lb1.Text = "This is label 1";
lb1.SetBounds(20, 100, 200, 40);
lb1.BackColor = Color.SeaGreen;
this.Controls.Add(lb1);
}
6. You can see a button and label controls are created onto form at run time.Description of above written code:
Button btn1 = new Button();
This line create an object 'btn1" of type Button, We have already discussed that every control has corresponding class such as Button control has Button class, TextBox control has TextBox class, Label control has Label class and so on. Whenever we want to create any control at runtime, we must create object of its class.
btn1.Text = "This is button1";
This line sets text , which will display on button through text property. Text property is used to specify text which appears on control.
btn1.SetBounds(20, 50, 200, 40);
SetBounds function is used to specify horizontal position, vertical position , width and hight of control. In our case the 20 is X value for horizontal position, 50 is Y value for vertical position, 200 is width and 40 is height of button.
btn1.BackColor = Color.PowderBlue;
This line sets background color of button to PowerBlue
this.Controls.Add(btn1);
This line uses Add method of Control class, to add control onto the current instance of form, this indicate that btn1 will be added to the current form.
0 Comments:
Post a Comment