using System;
using System.Windows.Forms;
class MainForm : Form
{
private Label label1;
private TextBox textBox1;
private Button button1;
public MainForm()
{
this.label1 = new Label();
this.textBox1 = new TextBox();
this.button1 = new Button();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(16, 36);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(128, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Please enter your name:";
this.textBox1.Location = new System.Drawing.Point(152, 32);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
this.button1.Location = new System.Drawing.Point(109, 80);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "Enter";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.ClientSize = new System.Drawing.Size(292, 126);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.ResumeLayout(false);
}
private void button1_Click(object sender, System.EventArgs e)
{
System.Console.WriteLine("User entered: " + textBox1.Text);
MessageBox.Show("Welcome, " + textBox1.Text, "Visual C#");
}
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
}
Month: February 2011
TextBox location
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
public class frmLogin : System.Windows.Forms.Form {
System.Windows.Forms.TextBox txtUser;
System.Windows.Forms.Button btnOK;
System.Windows.Forms.Button btnCancel;
public frmLogin() {
txtUser = new System.Windows.Forms.TextBox();
txtUser.Location = new Point(30, 15);
txtUser.Size = new Size(250, 20);
txtUser.Text = "";
txtUser.Name = "txtUser";
this.Controls.Add(txtUser);
btnOK = new System.Windows.Forms.Button();
btnOK.Location = new Point(40,(txtUser.Location.Y + txtUser.Size.Height + btnOK.Size.Height));
btnOK.Text = "OK";
btnOK.Name = "btnOK";
this.Controls.Add(btnOK);
btnCancel = new System.Windows.Forms.Button();
btnCancel.Location = new Point((this.Size.Width -
btnCancel.Size.Width) - 40,
(txtUser.Location.Y + txtUser.Size.Height + btnOK.Size.Height));
btnCancel.Text = "Cancel";
btnCancel.Name = "btnCancel";
this.Controls.Add(btnCancel);
this.Size = new Size(this.Size.Width, btnCancel.Location.Y +
btnCancel.Size.Height + 60);
btnCancel.Click += new System.EventHandler(btnCancelHandler);
btnOK.Click += new System.EventHandler(btnEventHandler);
}
private void btnEventHandler(object sender, System.EventArgs e) {
MessageBox.Show(((Button)sender).Name);
}
private void btnCancelHandler(object sender, System.EventArgs e) {
MessageBox.Show("The second handler");
}
[STAThread]
static void Main() {
Application.Run(new frmLogin());
}
}
Keyboard event and TextBox
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label2;
public Form1() {
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.label2 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 20);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(16, 24);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(168, 20);
this.textBox2.TabIndex = 6;
this.textBox2.Text = "";
this.textBox2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox2_KeyDown);
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.textBox1});
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.TabIndex = 7;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Key Monitor";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 64);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(168, 20);
this.label1.TabIndex = 6;
//
// groupBox2
//
this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox2,
this.label2});
this.groupBox2.Location = new System.Drawing.Point(8, 120);
this.groupBox2.Name = "groupBox2";
this.groupBox2.TabIndex = 8;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Keys Enumeration";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(168, 20);
this.label2.TabIndex = 9;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(216, 229);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox2,
this.groupBox1});
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
label1.Text = Convert.ToString(e.KeyValue);
}
private void textBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
if (e.Shift) Console.WriteLine("Shift, ");
if (e.Alt) Console.WriteLine("Alt, ");
if (e.Control) Console.WriteLine("Ctrl, ");
if (e.KeyCode == Keys.W || e.KeyCode == Keys.R ) {
Console.WriteLine("W R ");
} else if (e.KeyCode == Keys.Escape && e.Modifiers == (Keys.Shift | Keys.Alt)) {
Console.WriteLine("Escape");
} else if (e.KeyCode == Keys.C && e.Modifiers == (Keys.Alt | Keys.Control)) {
Console.WriteLine("s");
textBox2.SelectedText = "";
textBox2.SelectionLength = 0;
} else {
Console.WriteLine(Convert.ToString(e.KeyData));
}
}
}
Set Text to Statusbar
using System;
using System.Drawing;
using System.Windows.Forms;
class SimpleStatusBar: Form
{
public static void Main()
{
Application.Run(new SimpleStatusBar());
}
public SimpleStatusBar()
{
Text = "Simple Status Bar";
ResizeRedraw = true;
StatusBar sb = new StatusBar();
sb.Parent = this;
sb.Text = "My initial status bar text";
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
Pen pen = new Pen(ForeColor);
grfx.DrawLine(pen, 0, 0, ClientSize.Width, ClientSize.Height);
grfx.DrawLine(pen, ClientSize.Width, 0, 0, ClientSize.Height);
}
}
StatusBar with two panels
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RadioButton rdoLarge;
private System.Windows.Forms.RadioButton rdoSmall;
private System.Windows.Forms.RadioButton rdoList;
private System.Windows.Forms.RadioButton rdoDetails;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
private System.Windows.Forms.StatusBar sbInfo;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.rdoDetails = new System.Windows.Forms.RadioButton();
this.sbInfo = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
this.rdoList = new System.Windows.Forms.RadioButton();
this.rdoLarge = new System.Windows.Forms.RadioButton();
this.rdoSmall = new System.Windows.Forms.RadioButton();
this.groupBox1 = new System.Windows.Forms.GroupBox();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// rdoDetails
//
this.rdoDetails.Location = new System.Drawing.Point(8, 96);
this.rdoDetails.Name = "rdoDetails";
this.rdoDetails.Size = new System.Drawing.Size(104, 16);
this.rdoDetails.TabIndex = 3;
this.rdoDetails.Text = "Details";
this.rdoDetails.CheckedChanged += new System.EventHandler(this.rdoDetails_CheckedChanged);
//
// sbInfo
//
this.sbInfo.Location = new System.Drawing.Point(0, 277);
this.sbInfo.Name = "sbInfo";
this.sbInfo.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1,
this.statusBarPanel2});
this.sbInfo.ShowPanels = true;
this.sbInfo.Size = new System.Drawing.Size(552, 16);
this.sbInfo.TabIndex = 3;
//
// statusBarPanel1
//
this.statusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
this.statusBarPanel1.Width = 526;
//
// statusBarPanel2
//
this.statusBarPanel2.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
this.statusBarPanel2.MinWidth = 0;
this.statusBarPanel2.Width = 10;
//
// rdoList
//
this.rdoList.Checked = true;
this.rdoList.Location = new System.Drawing.Point(8, 72);
this.rdoList.Name = "rdoList";
this.rdoList.Size = new System.Drawing.Size(104, 16);
this.rdoList.TabIndex = 2;
this.rdoList.TabStop = true;
this.rdoList.Text = "List";
this.rdoList.CheckedChanged += new System.EventHandler(this.rdoList_CheckedChanged);
//
// rdoLarge
//
this.rdoLarge.Location = new System.Drawing.Point(8, 24);
this.rdoLarge.Name = "rdoLarge";
this.rdoLarge.Size = new System.Drawing.Size(96, 16);
this.rdoLarge.TabIndex = 0;
this.rdoLarge.Text = "LargeIcon";
this.rdoLarge.CheckedChanged += new System.EventHandler(this.rdoLarge_CheckedChanged);
//
// rdoSmall
//
this.rdoSmall.Location = new System.Drawing.Point(8, 48);
this.rdoSmall.Name = "rdoSmall";
this.rdoSmall.Size = new System.Drawing.Size(104, 16);
this.rdoSmall.TabIndex = 1;
this.rdoSmall.Text = "SmallIcon";
this.rdoSmall.CheckedChanged += new System.EventHandler(this.rdoSmall_CheckedChanged);
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.rdoDetails,
this.rdoList,
this.rdoSmall,
this.rdoLarge});
this.groupBox1.Location = new System.Drawing.Point(424, 16);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(120, 128);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "View mode";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(552, 293);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.sbInfo,
this.groupBox1,
});
this.Name = "Form1";
this.Text = "StatusBar";
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
static void Main()
{
Application.Run(new Form1());
}
private void rdoLarge_CheckedChanged(object sender, System.EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
{
this.sbInfo.Panels[1].Text = "Large Icon";
}
this.sbInfo.Panels[0].Text = "AAA";
}
private void rdoList_CheckedChanged(object sender, System.EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
{
this.sbInfo.Panels[1].Text = "List";
}
this.sbInfo.Panels[0].Text = "BBB";
}
private void rdoSmall_CheckedChanged(object sender, System.EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
{
this.sbInfo.Panels[1].Text = "Small Icon";
}
this.sbInfo.Panels[0].Text = "CCC";
}
private void rdoDetails_CheckedChanged(object sender, System.EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
{
this.sbInfo.Panels[1].Text = "Details";
}
this.sbInfo.Panels[0].Text = "DDD";
}
}
Status bar: display time and prompt message for menu item
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class MainForm : System.Windows.Forms.Form
{
private StatusBar statusBar = new StatusBar();
private StatusBarPanel sbPnlPrompt = new StatusBarPanel();
private StatusBarPanel sbPnlTime = new StatusBarPanel();
private Timer timer1 = new Timer();
private MainMenu mainMenu;
private System.ComponentModel.Container components;
public MainForm()
{
InitializeComponent();
Text = "Status Bar Example";
CenterToScreen();
BackColor = Color.CadetBlue;
// Configure the timer.
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
this.MenuComplete += new EventHandler(StatusForm_MenuDone);
BuildMenuSystem();
BuildStatBar();
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
static void Main()
{
Application.Run(new MainForm());
}
// Clicked handlers.
private void FileExit_Clicked(object sender, EventArgs e)
{
this.Close();
}
// Help | About Menu item handler
private void HelpAbout_Clicked(object sender, EventArgs e)
{
MessageBox.Show("The amazing menu app...");
}
// Selected handlers.
private void FileExit_Selected(object sender, EventArgs e)
{
sbPnlPrompt.Text = "Terminates this app";
}
private void HelpAbout_Selected(object sender, EventArgs e)
{
sbPnlPrompt.Text = "Displays app info";
}
// Other handlers...
private void StatusForm_MenuDone(object sender, EventArgs e)
{
sbPnlPrompt.Text = "Ready";
}
private void timer1_Tick(object sender, EventArgs e)
{
DateTime t = DateTime.Now;
string s = t.ToLongTimeString() ;
sbPnlTime.Text = s ;
}
private void BuildMenuSystem()
{
mainMenu = new MainMenu();
MenuItem miFile = mainMenu.MenuItems.Add("&File");
miFile.MenuItems.Add(new MenuItem("E&xit",new EventHandler(this.FileExit_Clicked), Shortcut.CtrlX));
miFile.MenuItems[0].Select += new EventHandler(FileExit_Selected);
MenuItem miHelp = mainMenu.MenuItems.Add("Help");
miHelp.MenuItems.Add(new MenuItem("&About", new EventHandler(this.HelpAbout_Clicked), Shortcut.CtrlA));
miHelp.MenuItems[0].Select += new EventHandler(HelpAbout_Selected);
this.Menu = mainMenu;
}
private void BuildStatBar()
{
// Configure the status bar.
statusBar.ShowPanels = true;
statusBar.Panels.AddRange(new StatusBarPanel[] {sbPnlPrompt, sbPnlTime});
// Configure prompt panel.
sbPnlPrompt.BorderStyle = StatusBarPanelBorderStyle.None;
sbPnlPrompt.AutoSize = StatusBarPanelAutoSize.Spring;
sbPnlPrompt.Width = 62;
sbPnlPrompt.Text = "Ready";
// Configure time pane.
sbPnlTime.Alignment = HorizontalAlignment.Right;
sbPnlTime.Width = 76;
try
{
Icon i = new Icon("status.ico");
sbPnlPrompt.Icon = i;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
this.Controls.Add(statusBar);
}
}
StatusBar Example
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace StatusBarExample
{
/// <summary>
/// Summary description for StatusBarExample.
/// </summary>
public class StatusBarExample : System.Windows.Forms.Form
{
internal System.Windows.Forms.StatusBar statusBar;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public StatusBarExample()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.statusBar = new System.Windows.Forms.StatusBar();
this.SuspendLayout();
//
// statusBar
//
this.statusBar.Location = new System.Drawing.Point(0, 138);
this.statusBar.Name = "statusBar";
this.statusBar.ShowPanels = true;
this.statusBar.Size = new System.Drawing.Size(292, 24);
this.statusBar.SizingGrip = false;
this.statusBar.TabIndex = 1;
//
// StatusBarExample
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
this.ClientSize = new System.Drawing.Size(292, 162);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.statusBar});
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Name = "StatusBarExample";
this.Text = "StatusBar Example";
this.Load += new System.EventHandler(this.StatusBarExample_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new StatusBarExample());
}
private void StatusBarExample_Load(object sender, System.EventArgs e)
{
StatusBarPanel pnlStatus = new StatusBarPanel();
pnlStatus.Text = "Ready";
pnlStatus.Icon = new Icon(Application.StartupPath + "active.ico");
pnlStatus.AutoSize = StatusBarPanelAutoSize.Contents;
StatusBarPanel pnlConnection = new StatusBarPanel();
pnlConnection.Text = "Connected to " + "localhost";
pnlConnection.AutoSize = StatusBarPanelAutoSize.Spring;
statusBar.Panels.Add(pnlStatus);
statusBar.Panels.Add(pnlConnection);
}
}
}
StatusBarExample.zip( 22 k)

