Saturday, April 5, 2014

Working With Multi Form

Working With Multi Form

Adding a new Form to the Project:

  • In the Solution Explorer, right click on the project and choose Add --> Windows Form.
  • In the dialog box, enter the name of the new form.
  • Then the new form will be created.

Deleting a Form from the Project:

  • In the Solution Explorer, right click on the required form, which you want to delete and click on Delete option.
  • Click on OK for confirmation.

Changing Startup Form:

  • Even, if you add multiple forms to the project, when the project is started, the Form1will be opened by default. This is called as Startup form.
  • To  change  the  startup  form,  change  the  following  statement  with  the  required  class name in the Program class‘s Main() method.
              Syn: Application.Run(new <form name>());
              Ex: Application.Run(new Form2());

Invoke Forms at Run Time (Programmatically):

  • To open any form at run time programmatically, you require to create an object for the form class.
              FormName obj = new FormName();
              obj.Show()
Program for Multi Form Handling
/*Form1.cs
---------------------------------------*/
 private void Form1_DoubleClick(object sender, EventArgs e)
  {
   Form2 f = new Form2();
   f.Show();
  }
System.Windows.Forms.Form Class

The System.Windows.Forms.Form offers few properties, methods and events for each user-defined form class.
Method Description
Name Specifies the name of the form class.
Text Specifies the title bar text of the form.
ShowIcon Specifies whether the form icon is to be displayed or not.
ShowInTaskBar Specifies whether the task bar icon is to be displayed or not.
MinimizeBox Specifies whether the minimize button is to be displayed or not.
MaximizeBox Specifies whether the maximize button is to be displayed or not.
HelpButton Displays / hides the ? button in the form title bar.
ControlBox Specifies whether the control box is to be displayed or not. Here the control box means minimize, maximize and close buttons.
Enabled Activates / Deactivates the functionality of the entire form.
AutoScroll Enables / Disables automatic activation of scrollbars in the form.
TopMost Enables / Disables automatic activation of scrollbars in the form.
TopMost Activates / de-activates the nature of "Always on top"
IsMDIContainer When it is true, the form acts as Parent form.
BackColor Specifies the background color of the form.
ForeColor Specifies default foreground color for all the controls of this form.
Font Specifies default font for all the controls of this form.
BackgroundImage Specifies the background image of the form. It requires an image file of any image format.
BackgroundImageLayout Specifies mode of the background image. (None / Tile / Center / Stretch / Zoom)
Icon Specifies the icon of the form, displayed at left most side of the form title bar. It equires icon file with ".ico" file.
WindowState Specifies the status of the form window. (Normal / Minimized / Maximized)
Cursor Specifies the mouse cursor style. (Arrow, Hand etc.)
FormBorderStyle None / FixedSingle / Fixed3D / FixedDialog / Sizable / FixedToolWindow / SizableToolWindow
Size (Width and Heght) Specifies the size of the form (pixels format).
Location (X and Y) Specifies the position of the form (pixels format).
Opacity Specifies form graphics depth percentage. (1% to 100%)

Syntax to access the Properties at run time: this.Property = value;
While you are changing the property values, you remember and follow the following syntaxes:
Assign Property value at runtime in Window Form Class
Property Statement to assign the value at run time
Name Not possible to change at run time.
Text this.Text = "xxxxxxxxxx";
ShowIcon this.ShowIcon = true / false;
ShowInTaskBar this.ShowInTaskbar = true / false;
MinimizeBox this.MinimizeBox = true / false;
MaximizeBox this.MaximizeBox = true / false;
HelpButton this.HelpButton = true / false;
ControlBox this.ControlBox = true / false;
Enabled this.Enabled = true / false;
AutoScroll this.AutoScroll = true / false;
TopMost this.TopMost = true / false;
IsMDIContainer this.IsMdiContainer = true / false;
BackColor this.BackColor = System.Drawing.Color.xxxxxxx;
ForeColor this.ForeColor = System.Drawing.Color.xxxxxxx;
Font this.Font = new System.Drawing.Font("font name", size);
BackgroundImage this.BackgroundImage = System.Drawing.Image.FromFile("image file path");
BackgroundImageLayout this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.xxxxx
Icon this.Icon = new System.Drawing.Icon("icon file path");
WindowState this.WindowState = System.Windows.Forms.FormWindowState.Normal; (or) this.WindowState = System.Windows.Forms.FormWindowState.Minimized; (or) this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
Cursor this.Cursor = System.Windows.Forms.Cursors.xxxxx;
FormBorderStyle this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.xxxxx;
Size (Width and Heght) this.Size = new System.Drawing.Size(width, height);
Location (X and Y) this.Location = new System.Drawing.Point(x, y);
Opacity this.Opacity = n;

Methods of “System.Windows.Forms.Form” Class

Property Description
Hide() Makes the form invisible at run time.
Show() Makes the form visible at run time.
Close() Closes the form.

Syntax to access the Methods in the code: this.Method();

Events of “System.Windows.Forms.Form” Class

Method Description
Load Executes whenever the form is loaded in the memory at run time, before the form is displayed on the screen.
Shown Executes after the form is displayed on the screen.
FormClosing Executes when the form is about to be closed.
FormClosed Executes after the form is closed.
Click Executes when the user clicks on the form at run time
DoubleClick Executes when the user double-clicks on the form at run time.
MouseMove Executes when the mouse pointer is moves across the form.
MouseEnter Executes when the mouse pointer is focused on to the form.
MouseLeave Executes when the mouse pointer is out of the form.
Move Executes when the form is moved at run time, using keyboard or mouse.
Resize Executes when the form is resized at run time.
KeyPress Executes when any key is pressed on the keyboard, while running on the form.
Enter Executes when the focus is got into the form.
Leave Executes when the focus is out of the form.

Program for Windows Form Events
private void Form1_Loadundefinedobject sender, EventArgs e)
  {
  this.Text = "This is load event.";
  }
  private void Form1_Moveundefinedobject sender, EventArgs e)
  {
   this.Text = "This is move event.";
  }
  private void Form1_Clickundefinedobject sender, EventArgs e)
   {
    this.Text = "This is click event.";
   }
  private void Form1_DoubleClickundefinedobject sender, EventArgs e)
   {
    this.Text = "This is double click event.";
   }
  private void Form1_FormClosedundefinedobject sender, FormClosedEventArgs e)
   {
    MessageBox.Showundefined"Bye. Thank you.");
   }
  private void Form1_Resizeundefinedobject sender, EventArgs e)
   {
    this.Text = "This is resize event.";
   }
  private void Form1_KeyPressundefinedobject sender, KeyPressEventArgs e)
   {
    this.Text = "This is key press event.";
   } 
Program for Window Form Properties
private void Form1_Clickundefinedobject sender, EventArgs e)
{
 this.Text = "Thanks for clicking";
 this.BackColor = Color.Green;
 this.WindowState = FormWindowState.Maximized;
}
Program Demo for Windows Form Properties
 
private void btnRed_Clickundefinedobject sender, EventArgs e)
 {
 this.BackColor = Color.Red;
 }
 private void btnGreen_Clickundefinedobject sender, EventArgs e)
 {
 this.BackColor = Color.Green;
 }
 private void btnBlue_Clickundefinedobject sender, EventArgs e)
 {
  this.BackColor = Color.Blue;
 }
Program Demo for Windows Form Properties
private void btnNormal_Clickundefinedobject sender, EventArgs e)
 {
 this.WindowState = FormWindowState.Normal;
 }
 private void btnMinimize_Clickundefinedobject sender, EventArgs e)
 {
 this.WindowState = FormWindowState.Minimized;
 }
 private void btnMaximize_Clickundefinedobject sender, EventArgs e)
 {
 this.WindowState = FormWindowState.Maximized;
 }
 private void btnExit_Clickundefinedobject sender, EventArgs e)
 {
 this.Closeundefined);
 }
Program Demo for Windows Form Properties
private void btnShowBackgroundImage_Clickundefinedobject sender, EventArgs e)
 {
  this.BackgroundImage = Image.FromFileundefined"c:\\globe.jpg");
  this.BackgroundImageLayout = ImageLayout.Zoom;
 }
 private void btnClearBackgroundImage_Clickundefinedobject sender, EventArgs e)
 {
  this.BackgroundImage = null;
 }

No comments:

Post a Comment

Flag Counter