Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

Console Based Windows Form
By Prasad H.

This C# Windows Form example shows how one can create different form properties including
borderless windows, autoscroll windows, sized windows, uncloseable windows and windows
that are not included in the task bar.

using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

class FormDemo:Form
{
  static string[] strCaption={"BorderLess Window","AutoScroll Window","Sized Window","Uncloseable","Not in TaskBar"};
  Button[] btnShow=new Button[strCaption.Length];
  public FormDemo()
  {
    this.Text="Demonstration of Winform";
    this.MaximumSize=new Size(300,300);
    this.MinimumSize=new Size(300,300);
    this.StartPosition=FormStartPosition.CenterScreen;
    this.Load+=new EventHandler(Form_Load);
    this.Closing+=new CancelEventHandler(Form_Closing);

    for(int i=0 ; i < strCaption.Length ; i++)
    {
      btnShow[i]=new Button();
      btnShow[i].Text=strCaption[i];
      btnShow[i].Size=new Size(200,20);
      btnShow[i].Location=new Point(10,35*i+20);
      btnShow[i].Click+=new EventHandler(Button_Click);
      btnShow[i].Tag=i.ToString();
      this.Controls.Add(btnShow[i]);
    }

  }

  public void Button_Click(object sender,EventArgs eArgs)
  {
    string strTag=((Button)sender).Tag.ToString();

    Form frmSample=new Form();
    Label Info=new Label();
    switch(strTag)
    {
      case "0":
        Button btnClose=new Button();
        btnClose.Text="Close";
        btnClose.Location=new Point((frmSample.Width-btnClose.Width)/2,(frmSample.Height-btnClose.Height)/2);
        btnClose.Click+=new EventHandler(Close_Window);        
        frmSample.ControlBox=false;
        frmSample.MaximizeBox=false;
        frmSample.MinimizeBox=false;
        frmSample.FormBorderStyle=FormBorderStyle.None;
        frmSample.Controls.Add(btnClose);
        frmSample.Show();
        break;
      case "1":
        frmSample.AutoScroll=true;
        Button btnSample=new Button();
        btnSample.Text="Extreme";
        btnSample.Location=new Point(frmSample.Width+20,frmSample.Height+20);
        frmSample.Controls.Add(btnSample);
        frmSample.Show();
        break;
      case "2":
        frmSample.MaximumSize=new Size(300,300);
        frmSample.MinimumSize=new Size(300,300);
        Info.AutoSize=true;
        Info.Text="Try Maximizing/Resizing Window";
        frmSample.Controls.Add(Info);
        frmSample.Show();
        break;
      case "3":
        frmSample.Closing+=new CancelEventHandler(Form_Closing);
        frmSample.Show();
        break;
      case "4":
        frmSample.ShowInTaskbar=false;
        Info.AutoSize=true;
        Info.Text="Minimize the window and you won't find it in TaskBar";
        frmSample.Controls.Add(Info);
        frmSample.Show();
        break;
    }
  }

  public void Close_Window(object sender,EventArgs eArgs)
  {
    ((Form)((Button)sender).Parent).Close();
  }

  public void Form_Load(object sender,EventArgs eArgs)
  {
    MessageBox.Show("Loading Form.....");
  }

  public void Form_Closing(object sender,CancelEventArgs cArgs)
  {
    if(sender==this)
      MessageBox.Show("Form Closing Event....");
    if(sender!=this)
    {
      cArgs.Cancel=true;  
    }
  }

  public static void Main()
  {
    Application.Run(new FormDemo());
  }
}