Advertisement

Responsive Advertisement

Lesson 5 Windows Form in c# ,What are Objects?, Toolbox Controls

 

I-Introduction

Windows Forms is a Graphical User Interface(GUI) class library which is bundled in .Net Framework. Its main purpose is to provide an easier interface to develop the applications for desktop, tablet, PCs. It is also termed as the WinForms. The applications which are developed by using Windows Forms or WinForms are known as the Windows Forms Applications that runs on the desktop computer. WinForms can be used only to develop the Windows Forms Applications not web applications. WinForms applications can contain the different type of controls like labels, list boxes, tooltip etc.

II - What are Objects?

You can create one or more objects of a class. Each object can have different values of properties and field but methods and events behaves the same.

- Property:

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they're public data members, but they're special methods called accessors. This feature enables data to be accessed easily and still helps promote the safety and flexibility of methods.


- A method is a block of code which only runs when it is called.

You can pass data, known as parameters, into a method.

Methods are used to perform certain actions, and they are also known as functions.

- Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts. Events are used for inter-process communication.


The following is the Visual C # events window:

Use this window to view and change the design-time properties and events of selected objects that are located in editors and designers. You can also use the Properties window to edit and view file, project, and solution properties. You can find Properties Window on the View menu. You can also open it by pressing F4 or by typing Properties in the search box.


The Properties window displays different types of editing fields, depending on the needs of a particular property. These edit fields include edit boxes, drop-down lists, and links to custom editor dialog boxes. Properties shown in gray are read-only.


III. Toolbox Controls

The Toolbox window displays controls that you can add to Visual Studio projects. To open Toolbox, choose View > Toolbox from the menu bar, or press Ctrl+Alt+X.

You can drag and drop different controls onto the surface of the designer you are using, and resize and position the controls.

Toolbox appears in conjunction with designer views, such as the designer view of a XAML file or a Windows Forms App project. Toolbox displays only those controls that can be used in the current designer. You can search within Toolbox to further filter the items that appear.

The .NET version that your project targets also affects the set of controls visible in Toolbox. You can change the target framework version from the project's property pages, if necessary. Select the project node in Solution Explorer, and then on the menu bar, choose Project > projectname Properties. On the Application tab, use the Target framework drop-down.



III.1- Using Combo Box

In Windows Forms, ComboBox provides two different features in a single control, it means ComboBox works as both TextBox and ListBox. In ComboBox, only one item is displayed at a time and the rest of the items are present in the drop-down menu. The ComboBox is a class in C# and defined under System.Windows.Forms Namespace. You can create ComboBox using the two different ways: 1. Design-Time: It is the easiest method to create a ComboBox control using the following steps:

 

Step 1: Create a windows form as shown in the below image: Visual Studio -> File -> New -> Project -> WindowsFormApp

Step 2: Drag the ComboBox control from the ToolBox and drop it on the windows form. You are allowed to place a ComboBox control anywhere on the windows

Step 3: After drag and drop you will go to the properties of the ComboBox control to set the properties of the ComboBox according to your need.

Run-Time: It is a little bit trickier than the above method. In this method, you can set create your own ComboBox control using the ComboBox class. Steps to create a dynamic ComboBox:

  • Step 1: Create a combobox using the ComboBox() constructor is provided by the ComboBox class.

 

The following is a list of key properties and methods that need to be used:






SourceCode:

private void Form1_Load(object sender, EventArgs e)

        {

            for (int i = 0; i < 6; i++)

            {

                cboItem.Items.Add("Item" + i.ToString());

            }

        }

 

        private void cboItem_SelectedIndexChanged(object sender, EventArgs e)

        {

            txtMessage.Text = "Selected at position"+ cboItem.SelectedIndex.ToString();

        }


Result:






Post a Comment

0 Comments