Tuesday, October 6, 2009

Windows Forms : C# Console Way

This is continuation of the blog C# Language : The Console Way

Creating a Form and Inheritance
using System;
using System.Windows.Forms;


class frmHello : Form
{
public static void Main()
{
Application.Run(new frmHello());
}
}




















We can create a windows application by inheriting the class form System.Windows.Forms.Form class. In C# : (colon) is the inheritance operator. The major difference between C++ and C# when it come to inheritance is C# doesn't support multiple inheritance.

Class Constructor and this

using System;
using System.Windows.Forms;


class frmHello : Form
{
public static void Main()
{
Application.Run(new frmHello());
}
public frmHello()
{
this.Text = "shalvin.com";
}
}



















Constructor is used for initializing a class. Usually a constructor is a special method having the same name that of a class. this refers to the current instance of Form. Inside the constructor we are setting the Text of form.

Working with Controls Collection
using System;
using System.Windows.Forms;


class frmHello : Form
{
Button btn;
public static void Main()
{
Application.Run(new frmHello());
}
public frmHello()
{
this.Text = "shalvin.com";
btn = new Button();
btn.Text = "Hello";
this.Controls.Add(btn);
}
}


















In this example we are creating an object of Button class, setting the Text property and adding the button to the Form's controls collection.

System.Drawing Namespace and Setting the Location of Button

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


class frmHello : Form
{
Button btn;
public static void Main()
{
Application.Run(new frmHello());
}
public frmHello()
{
this.Text = "shalvin.com";
btn = new Button();
btn.Location = new Point(50, 50);
btn.Text = "Hello";
this.Controls.Add(btn);
}
}













For setting the Location of a control there is the Location property. Setting the location requires Point struct which is inside System.Drawing namespace.

Event Handling

In C#, a delegate is a way of telling which method to call when an event is triggered. For example, if you click a Button on a form, the program would call a specific method. It is this pointer which is a delegate. Delegates are good because you can notify several methods that an event has occurred, if you so wish

using System;
using System.Windows.Forms;
using System.Drawing;
class frmHello : Form
{
Button btn;
public static void Main()
{
Application.Run(new frmHello());
}
public frmHello()
{
this.Text = "shalvin.com";
btn = new Button();
btn.Location = new Point(50, 50);
btn.Text = "Hello";
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);
}
private void btn_Click(Object sender, EventArgs e)
{
MessageBox.Show("Welcome to C#");
}
}













MenuStrip
using System;
using System.Windows.Forms;
using System.Drawing;

class frmHello : Form
{

MenuStrip menuStrip1;
ToolStripMenuItem fileToolStripMenuItem;
ToolStripMenuItem examplesToolStripMenuItem;
ToolStripMenuItem exitToolStripMenuItem;

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

}

public frmHello()
{
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
this.menuStrip1 = new MenuStrip();
this.Controls.Add(menuStrip1);
fileToolStripMenuItem = new ToolStripMenuItem();
fileToolStripMenuItem.Text = "File";
menuStrip1.Items.Add(fileToolStripMenuItem);

examplesToolStripMenuItem = new ToolStripMenuItem();
examplesToolStripMenuItem.Text = "Examples";
menuStrip1.Items.Add(examplesToolStripMenuItem);

exitToolStripMenuItem = new ToolStripMenuItem();
exitToolStripMenuItem.Text = "Exit";
fileToolStripMenuItem.DropDownItems.Add("Exit");
}

}

ListBox

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

namespace ConsoleApplication1
{
class Program : Form 
{
    Label lbl, lblStudents;
    TextBox txtName;
    Button btn;
    ListBox lstStudents;

    static void Main(string[] args)
    {
        Application.Run(new Program());
    }
    public Program()
    {
        this.Text = "Shavin.net";

        lblStudents = new Label { Text = "2012", Location = new Point(50, 50) };
        this.Controls.Add(lblStudents);

        lstStudents = new ListBox { Location = new Point(150, 50) };
        this.Controls.Add(lstStudents);

        lbl = new Label { Text = "Name", Location = new Point(50, 150) };
        this.Controls.Add(lbl);

        txtName = new TextBox{Location = new Point(150, 150)};
        this.Controls.Add(txtName);
       

        btn = new Button();
        btn.Text = "Add";
        btn.Location = new Point(100, 200);
        btn.Click +=new EventHandler(btn_Click);
        this.Controls.Add(btn);
    }
    private void btn_Click(object sender, EventArgs e)
    {
        lstStudents.Items.Add(txtName.Text);
        txtName.Text = "";
        txtName.Focus();
    }
}
}


Mono

No comments:

Post a Comment