How To Use Common Controls In Window Form?

INTRODUCTION

A Windows Forms Control is a class that derives directly or indirectly from System.Windows.Forms.Control. If we talk about the windows control then it is necessary to know that the base class for Windows Forms Controls is, System.Windows.Forms. Hence, these Controls provide the various control required for visual display in client-side Windows applications. Basically, Controls provide a window handle and also provide mouse and keyboard events as well as many other user interface events. It has the facility of advanced layout and has properties specific to a visual display, such as ForeColor, BackColor, Height, Width and many others. Additionally, it provides security, threading support and interoperability with ActiveX controls. the main reason behind this is so much of the infrastructure is provided by the base class, it is relatively easy to develop your own Windows Forms Controls. The following list describes common scenarios for developing Windows Forms Controls.

ToolBox

Basically, the Windows Forms controls are listed here according to general function. you can also find all of these controls in the ToolBox.

FUNCTION

CONTROL

DESCRIPTION

Text Edit

>Text Box

>Displays text entered at design time that can be edited by users at runtime, or changed programmatically.

>Rich TextBox

>Enable text to be displayed with formatting in plain text or Rich Text Format(RTF).

Text Display
(Read Only)

>Lable

>Display Text that user can not directly Edit

>LinkLable

>Display Text as a Web-Style link and triggers an event when the user clicks the special text. Usually, the text is a link to another window or a WebSite.

>Status Bar

>Display information about the application’s current state using a framed window, usually at the bottom of a parent form.

The section from a List

>CheckedListBox

>Display a scrollable list of items, each accompanied by a checkbox.

>ListBox

>Display a list of text and graphical item(Icons).

>ListView

>Displays items in one of four different views. Views include text-only, Text with small icons, Text with large icons and a report view.

>NumericUpDown

>Display a list of numerical that user can scroll through with Up and Down buttons.

>TreeView

>Displays a hierarchical collection of node objects which can consist of text with optional check boxes and icons.

Graphics Display

>PictureBox

>Display graphical files, such as bitmaps and icons, in a frame.

Graphics Storage

>ImageList

>Generally, Serves as a repository for images. ImageList controls and the image they contain can be reused from one application to the next.

Value setting

>CheckBox

>Display a checkbox and a label for a text. Generally, used to set options.

>CheckedListBox

>Display a scrollable list of items, each accompanied by a checkbox.

>RadioButton

>Display a button that can be turned on or off.

>TrackBar

>Generally, Allows a user to set values on a scale by moving a ‘thumb’ along with a scale.

Date Setting

>DateTimePicker

>Display a graphical calendar to allow a user to select a date or a time.

>MonthCalendar

>Display a graphical calendar to allow a user to select a range of dates.

Dialog boxes

>ColorDialog

>Displays the color picker dialog box that allows a user to set the color of an interface element.

>OpenFileDialog

>Display a dialog box that allows a user to navigate and select a file.

>PrintDialog

>Display a dialog box that allows a user to select the printer and set its attributes.

>PrintPreview

>Display a dialog box that displays how a Print.

>Dialog

>Document object will appear when printed

>SaveFileDialog

>Display a dialog box that allows a user to save a file.

Menu Controls

>MainMenu

>Provides a design time interface for creating menu.

>ContextMenu

>Implements a menu that appears when the user right-clicks an object.

Commands

>Button

>Used to start, stop, and interrupt process

linkable

>Displays Text as Web-style link and triggers when the user clicks the special text. Basically, The text is a link to another window or website.

>NotifyIcon

>Display an icon in the status notification areas of the taskbar that represents an application running in the background.

Grouping Other

>ToolBar

>Contain a collection of button controls.

>PannelControls

>Groups a set of controls on an unlabelled, scrollable frame.

>GroupBox

>Group a set of controls(such as RadioButton, labeled, non-scrollable frame.)

>TabControl

>Provide a tabbed page for organizing grouped objects efficiently.

 

In conclusion, Explaining all of the controls present in the  visual studio is impossible but some most important controls are described as follows:

1. Label

Windows Forms Label controls basically used to display text or images that cannot be edit by the user. They are used to identify the object on a form – to provide a description of what to certain control will do if clicked, for example, or to display information in response to the time event or process in your application. For example, you can use labels to add describes captions to text boxes, list boxes, combo boxes, and so on. You can also write code and changes the text displayed by a label in response to the event at runtime. For example, an application takes a few minutes to process a change, you can display a processing message in a label.
So Let’s take an example to understand the working of a label, Create a new project.

Label

2. TextBox

Another Window form control is TextBox. Basically, TextBox is used to get input from the user or to display text. Basically, It is used for editable text, although it can also be made read-only. Text Boxes can display multiple lines as well as it wraps text to the size of the control, and add basic formatting. So it has the facility of providing a single format style for displaying text or enter into the control. In conclusion for displaying multiple types of formatted text, we should use the RichTextBox control.

TextBox

The code below sets the text in the control at runtime. The InitializeMyControl procedure will not execute automatically, it must be called.

Private void InitializeMyControl()

{

//put some text into the control first.

textBox1.Text=”This is a TextBox control.”;

}

To create a Password text box

With the help of PasswordTextBox we can display placeholder characters while a user types a string.

  • Set the PasswordChar property of the TextBox control to a specific character. The PasswordChar property specifies the character displayed in the text box.
  • (Optional) set the MaxLength property. The property determines how many characters can be typed in the text box.

The code below also Initializes a text box that will accept a string up to 14 characters long and display an asterisk in place of the string. Basically, The InitializeMyControl procedure will note execute automatically; it must be called.

private void InitializeMyControl()

{

//set to no text

textBox1.text=””;

//The password character is an asterisk.

textBox1.passwordChar=’*’;

//The control will allow no more than 14 characters.

textBox1.MaxLength=14;

}

3. Button

If we talk about Button Control in Windows form than it allows the user to click it to perform an action. It can display both text and images. when the button is clicked, it looks as if it is being pushed in and released.

While of clicking a Button, The Click event handler is invoked. you can place the code in the click event handler to perform any action as you want. you can display the text on the button. This text also contained in the text property. If your text exceeds the width of the button, then it will wrap to the next line. you also can contain access key with the help of text property, which allows a user to “click” the control by pressing the ALT key with the access key. The appearance of the text is controlled by the font property and the TextAlign property.

Button Control

Example

When you click on a button control it generates a number of other events. So these events can be the MouseEnter, MouseDown, and MouseUp events. When you intend to attach event handlers for these related events, you have to be sure that their actions do not conflict. The click event on a button clears all the information which is typed by a user in a text box. After that pausing, the mouse pointer over the button should not display a tooltip with that information.

Hence, If a user attempts to double-click the Button control, each click will be processed separately. Finally, This mean is that the control does not support the double click event.

How To respond to a Button click

For performing an action on the button control you have to write the following code in the button’s click event handler or double-click on the button in design mode.

Example-

private void button1_Click(object sender, System.EventArgs e)

{

MessageBox.Show(“Button1 was Clicked”);

}

4. COMBOBOX

This control is basically used for displaying data in a drop-down manner. Generally, This control appears in two parts: the top part is a text box that allows the user to type a list item and the second part is a list box that displays a list of items in which the user can select one.

Combo Box

The SelectedIndex property returns an integer value that corresponds to the selected list item. You can programmatically change the selected item by changing the SelectedIndex value in code; The corresponding item in the list will appear in the text box portion of the combo box. If no item is selected, the SelectIndex Value is -1. If the first item in the list is selected, then the SelectedIndex value is 0. If we talk about the SelectedItem property, It is similar to SelectedIndex but returns the item itself as usually a string value. The ItemsCount Property reflects the number of items in the list and the value of the Items. Count property is always one more than the largest possible SelectedIndex value because SelectedIndex is value based.

6. LISTBOX

Basically, A ListBox is a useful tool that helps you to display a list of several items from which you can select one or more items. A scrollbar is automatically added to the list box when the number of items in the list box increases.

ListBox
  • To a new Form, must add a list box from the toolbox.
  • Also, Locate the property items in the property sheet.
  • Click on the collections on the other side.
  • Finally, A new dialog box is opened.
  • Add the items to this Dialog box.
  • You must type one item per line.
  • On closing the dialog box the items added to the list box are available.
  • Enter the following lines of code in the code.

private void button1_Click(object sender, System.EventArgs e)

{

MessageBox.Show(“Button1 was Clicked”);

}

Now if you execute the program by pressing F5, You will see the following output:

ListBox1.Items.Add(“Item10”)

In the same way, you can also remove the items from the list box:

ListBox1.Items.Remove(ListBox1_SelectedItem)

7. Checkbox

Basically, one of the most commonly used window control is CheckBox control. It indicates whether a particular condition is On or Off. This window control is commonly used to present a Yes/No as well as True/False selection to the user. While using this window control, You can also use checkbox controls in groups to display multiple choices from which the user can select one or more.

CheckBox

Hence, This control is similar to radio button control. In that case, each is used to indicate a selection that is made by the user. With the checkbox control, However, any number of checkboxes may be selected.

How to respond to CheckBox clicks

To assign an action on the CheckBox control write the following code in the checkbox’s click event handler. Finally, the code written in a handler of checkbox control will change text property each time the controlled is clicked, indicating a checked or unchecked state.

private void CheckBox1_Click(object sender, System.EventArgs e)

{

if (checkbox1.Checked)

{

CheckBox1.Text + =”Checked”;

}

else

{

CheckBox.Text + =”Unchecked”;

}

}

8. RADIO BUTTON

Radio buttons are also checkboxes and the difference lies in the following areas:

  • Basically, RadioButtons are round as against the checkboxes which are square.
  • Therefore, Radio buttons are used mostly in groups.

While checkboxes are used individually, the radio buttons are used in groups. In case of the radio buttons, if you check button3 then clicking button1 is selected automatically.

Radio Button

Hence, create a new project add three radio buttons, one command button , and two labels
Then add the following code to the Form:

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

// For Radio Button1

private void RadioButton1_CheckedChanged(object sender, EventArgs e)

{

Label2.Text=”RadioButton1 Selected !”;

}

// For Radio Button2

private void RadioButton2_CheckedChanged(object sender, EventArgs e)

  {

Label2.Text=”RadioButton2 Selected !”;

  }

// For Radio Button3

private void RadioButton3_CheckedChanged(object sender, EventArgs e)

{

Label2.Text=”RadioButton3 Selected !”;

}

//For Exit Button

private void button2_Click(object sender, EventArgs e)

{

this.Close();

}

}

9. Picturebox

There is another window control namely as PictureBox. With the help of System.Windows.Forms.PictureBox control, It is possible to load as well as display a picture on a form at design time by setting the System.Windows.Forms.PictureBox.image property to a valid picture. Acceptable file types which can be any of the following.

Type File Extention
Bitmap .bmp
Icon .ico
GIF .gif
MetaFile .wmf
JPEG .jpg

To display a picture at design time:

  • Drag and drop a PictureBox control on a form with the help of ToolBox.
  • On the properties window, select the image property, then click the ellipsis button to display the open dialog box.
  • If you are looking a specific file type (for example, .gif files), select it in the files of type box.
  • Finally, Select the file you want to display.
PictureBox

To clear the picture at design time

Create a new project add a PictureBox and two command buttons, then add the following code to the Form:

public partial class Form1: Form

{

public Form1()

 {

InitializeComponent();

 }

private void Form1_Load(object sender, EventArgs e)

 {

Bitmap img = new Bitmap(“C:\discs.bmp”);

PictureBox1.Image = img;

 }

// For Exit Button

private void Button2_click(object sender, EventArgs e)

{

this.Close();

}

10. ProgressBar

ProgressBar basically used for showing the progress of some operations by displaying rectangles in the horizontal bar. ProgressBar also has some main properties which are value, minimum and maximum. You can also use minimum and maximum properties to set the minimum and maximum values the progress bar can display. To change the display, you can write code as well as set the value property. As a result, If the maximum property is set to 100, the minimum property is set to 10.

ProgressBar

Hence, Create a new project add a progress bar and two command buttons and two Labels. Then add the following code to the form:

public partial class Form1: Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

ProgressBar1.Maximum = 10000;

ProgressBar1.Minimum = 1;

ProgressBar1.Step = 100;

}

11. TIMER

Timer Class is a component that is used to create periodic Tick events that to execute at a specific interval. Another properties, methods, and events are as follows:

Property / Method / Event Description
Enabled Gets/Sets whether the timer is running
Interval Gets/Sets the time in milliseconds between timer ticks
Starts Start the Timer
Stop Stop the Timer
Tick Occurs the Timer interval has elapsed

As a result, we can start and stop timer using the method Start() and Stop(). While the Tick event handler will be used to add codes that we need to manipulate the activity of the timer. Hence, each Tick event takes place at a default time of 100 milliseconds and this default value can be changed.

Timer

Hence, Create a new project in visual studio and add a timer component to the Form1.
Therefore Add two buttons. Add the following code to the page.

public partial class Form1 : Form

{

int t;

public Form1()

{

InitializeComponent();

}

private void timer1_Tick(object sender, EventArgs e)

{

t + =100;

}

// For Timer Button

private void Button1_Click(object sender, EventArgs e)

{

int t2;

timer1.Start();

MessageBox.Show(“The timer has just been started”);

timer1.Stop();

t2 = t / 1000;

MessageBox.Show(t2 + “Seconds have elapsed !”, “Time Taken”);

}

// For Exit Button

private void Button2_click(object sender, EventArgs e)

{

this.Close();

}

Conclusion

In conclusion, I just want to add some points that windows forms control plays a very important role to develop a window application. Basically, These controls are basic things in developing a window form.  Windows control has the facility of the advanced layout as well as it has some properties which help in giving the look and feel of a user. Last but not the least, Without knowing about Windows Forms Control,  we can not develop a window application.

 

Leave a Comment