Tuesday, February 25, 2014

Designing a Form In VS

Que. How does a Form gets created?
Ans. When a form is added to the project internally following things takes place :-
-->     Creates a class inheriting from the predefined class Form, so that the new class is also a            form
eg.         public partial class Form1 :Form

-->   Sets some initialization properties like name, text etc., under the method initialize                          component
Eg: 
              this.Name = “Form1”;
              this.Text = “ Form1”;

Que. How does a control gets placed on the form?
Ans. When control is placed on a form following things take place:-
        I.            Create object of appropriate control class.
Button button1 = new Button();
TextBox textBox1 = new TextBox();

.            Set some initialization properties that are required like – name, text, side, location etc.

Eg:
button.Name = “button1”;
button.Text =  “button1”;
button1.Location = new Size(width,height);
            Now the control get added to form by calling Controls. Add a method on current form:

Eg:
this.Controls.Add(button1);
Note - VS will generate all the above code, under the Method Initialized component.

- Under a windows application code is of two types:-
              1. Designer code
              2. Business logic

- Code, which is responsible for construction of the form, known as Designer code, & code, which is responsible for execution of the form, known as Business form.

- Designer code is generated by VS under the method InitializedComponent and Business logic is defined by programmers in the form of Event procedure.

 - Before .net 2.0, Designer code and business logic were define in a class present under a file.
Before 2.0
Form1.cs :-

public class Form1 : Form
 {
     - Designer Code
     - Business logic
}
- From .net 2.0 with introduction of partial classes designer code and business logic are separated into 2 different files but of the same class only.
From 2.0
Form1.cs:-

public partial class Form1 : Form
   {
        - Business Logic
   }

Form1.Desiger.cs:
partial class form1
     {
          - Designer Code
     }

Default Events

As we are aware every controls has events under it but one event will be its default. To write code under default event double click on the control which take to its mapping event procedure.
Control
Default Event
Form
Load
Button
Click
TextBox
TextChanged
Check Box and RadioButton
CheckedChanged
TreeView
AfterSelect
Timer
Tick
ListView, ListBox, ComboBox & CheckedListBox
SelectedIndexChanged
Controls :- different types of controls :
  • Button – It is used for taking acceptance from user to perform an action.
  • Label – it is used for displacing static text on the UI (user interface)
  • TextBox – It is used for taking text input from the user. This control can be used in     Three different ways –

TextBox  (single line input)
TextArea (Multiline input)
Password Field

- By default the control takes input in single line. If we want multiline input , change the multiline property as true. In multiline format we required scroll bass to navigate up and down as well as left and right but it doesn’t provide you scroll bars by default you. To gets scroll bars set the property scrollbar with any value like vertical or horizontal or both.

Note – By default the word-wrap property of control in true. So we not get horizontal scrollbar until we make it false.

- To make the control in password field – set the password Char property with star(*).

No comments: