using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void webBrowser1_DocumentTitleChanged(object sender, EventArgs e)
{
this.Text = webBrowser1.DocumentTitle.ToString();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
webBrowser1.Navigate(textBox1.Text);
}
}
private void webBrowser1_Navigated(object sender,
WebBrowserNavigatedEventArgs e)
{
textBox1.Text = webBrowser1.Url.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
buttonBack.Enabled = false;
buttonForward.Enabled = false;
buttonStop.Enabled = false;
}
private void buttonBack_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
textBox1.Text = webBrowser1.Url.ToString();
}
private void buttonForward_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
textBox1.Text = webBrowser1.Url.ToString();
}
private void buttonStop_Click(object sender, EventArgs e)
{
webBrowser1.Stop();
}
private void buttonHome_Click(object sender, EventArgs e)
{
webBrowser1.GoHome();
textBox1.Text = webBrowser1.Url.ToString();
}
private void buttonRefresh_Click(object sender, EventArgs e)
{
webBrowser1.Refresh();
}
private void buttonSubmit_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(textBox1.Text);
}
private void webBrowser1_CanGoBackChanged(object sender, EventArgs e)
{
if (webBrowser1.CanGoBack == true)
{
buttonBack.Enabled = true;
}
else
{
buttonBack.Enabled = false;
}
}
private void webBrowser1_CanGoForwardChanged(object sender, EventArgs e)
{
if (webBrowser1.CanGoForward == true)
{
buttonForward.Enabled = true;
}
else
{
buttonForward.Enabled = false;
}
}
private void webBrowser1_Navigating(object sender,
WebBrowserNavigatingEventArgs e)
{
buttonStop.Enabled = true;
}
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
buttonStop.Enabled = false;
}
private void InitializeComponent()
{
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.textBox1 = new System.Windows.Forms.TextBox();
this.buttonSubmit = new System.Windows.Forms.Button();
this.buttonRefresh = new System.Windows.Forms.Button();
this.buttonHome = new System.Windows.Forms.Button();
this.buttonStop = new System.Windows.Forms.Button();
this.buttonForward = new System.Windows.Forms.Button();
this.buttonBack = new System.Windows.Forms.Button();
this.SuspendLayout();
this.webBrowser1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.webBrowser1.Location = new System.Drawing.Point(15, 74);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Size = new System.Drawing.Size(700, 339);
this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
this.textBox1.Location = new System.Drawing.Point(14, 47);
this.textBox1.Size = new System.Drawing.Size(399, 20);
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
this.buttonSubmit.Location = new System.Drawing.Point(419, 18);
this.buttonSubmit.Size = new System.Drawing.Size(75, 49);
this.buttonSubmit.Text = "Submit";
this.buttonSubmit.UseVisualStyleBackColor = true;
this.buttonSubmit.Click += new System.EventHandler(this.buttonSubmit_Click);
this.buttonRefresh.Location = new System.Drawing.Point(338, 18);
this.buttonRefresh.Size = new System.Drawing.Size(75, 23);
this.buttonRefresh.Text = "Refresh";
this.buttonRefresh.UseVisualStyleBackColor = true;
this.buttonRefresh.Click += new System.EventHandler(this.buttonRefresh_Click);
this.buttonHome.Location = new System.Drawing.Point(257, 18);
this.buttonHome.Size = new System.Drawing.Size(75, 23);
this.buttonHome.Text = "Home";
this.buttonHome.UseVisualStyleBackColor = true;
this.buttonHome.Click += new System.EventHandler(this.buttonHome_Click);
this.buttonStop.Location = new System.Drawing.Point(176, 18);
this.buttonStop.Size = new System.Drawing.Size(75, 23);
this.buttonStop.Text = "Stop";
this.buttonStop.UseVisualStyleBackColor = true;
this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
this.buttonForward.Location = new System.Drawing.Point(95, 18);
this.buttonForward.Size = new System.Drawing.Size(75, 23);
this.buttonForward.Text = "Forward";
this.buttonForward.UseVisualStyleBackColor = true;
this.buttonForward.Click += new System.EventHandler(this.buttonForward_Click);
this.buttonBack.Location = new System.Drawing.Point(14, 18);
this.buttonBack.Size = new System.Drawing.Size(75, 23);
this.buttonBack.Text = "Back";
this.buttonBack.UseVisualStyleBackColor = true;
this.buttonBack.Click += new System.EventHandler(this.buttonBack_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(728, 430);
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.buttonSubmit);
this.Controls.Add(this.buttonRefresh);
this.Controls.Add(this.buttonHome);
this.Controls.Add(this.buttonStop);
this.Controls.Add(this.buttonForward);
this.Controls.Add(this.buttonBack);
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.WebBrowser webBrowser1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button buttonSubmit;
private System.Windows.Forms.Button buttonRefresh;
private System.Windows.Forms.Button buttonHome;
private System.Windows.Forms.Button buttonStop;
private System.Windows.Forms.Button buttonForward;
private System.Windows.Forms.Button buttonBack;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
GUI Windows Form
Simple WebBrowser
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Windows.Forms;
using System.Drawing;
using AxSHDocVw;
public class WebBrowser : Form
{
private AxWebBrowser browser;
private Button goButton;
private TextBox addressBox;
private Panel panel1;
private Panel panel2;
public WebBrowser()
{
panel1 = new Panel();
panel2 = new Panel();
browser = new AxWebBrowser();
browser.BeginInit();
this.SuspendLayout();
panel1.SuspendLayout();
panel2.SuspendLayout();
this.Text = "MyWebBrowser";
panel1.Size = new Size(300, 30);
panel1.Dock = DockStyle.Top;
panel2.Size = new Size(285,240);
panel2.Location = new Point(5, 31);
panel2.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
browser.Dock = DockStyle.Fill;
addressBox = new TextBox();
addressBox.Size = new Size(260, 20);
addressBox.Location = new Point(5,5);
addressBox.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left;
goButton = new Button();
goButton.Image = Image.FromFile("Arrow.ico");
goButton.Location = new Point(270,5);
goButton.Size = new Size(20,20);
goButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
panel1.Controls.AddRange(new Control[] { addressBox, goButton });
panel2.Controls.Add(browser);
this.Controls.AddRange(new Control[] { panel1, panel2 });
browser.EndInit();
panel1.ResumeLayout();
panel2.ResumeLayout();
this.ResumeLayout();
goButton.Click += new EventHandler(goButton_Click);
browser.GoHome();
}
private void goButton_Click(object sender, EventArgs e)
{
object o = null;
browser.Navigate(addressBox.Text, ref o, ref o, ref o, ref o);
}
[STAThread]
public static void Main()
{
Application.Run(new WebBrowser());
}
}
My Web Browser
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MyWebBrowser
{
/// <summary>
/// Summary description for MyWebBrowser.
/// </summary>
public class MyWebBrowser : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.TextBox addressBox;
private System.Windows.Forms.Button goButton;
private AxSHDocVw.AxWebBrowser axWebBrowser1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MyWebBrowser()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
axWebBrowser1.GoHome();
//
// 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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MyWebBrowser));
this.panel1 = new System.Windows.Forms.Panel();
this.goButton = new System.Windows.Forms.Button();
this.addressBox = new System.Windows.Forms.TextBox();
this.panel2 = new System.Windows.Forms.Panel();
this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.goButton,
this.addressBox});
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(408, 40);
this.panel1.TabIndex = 0;
//
// goButton
//
this.goButton.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.goButton.Image = ((System.Drawing.Bitmap)(resources.GetObject("goButton.Image")));
this.goButton.Location = new System.Drawing.Point(368, 8);
this.goButton.Name = "goButton";
this.goButton.Size = new System.Drawing.Size(32, 24);
this.goButton.TabIndex = 1;
this.goButton.Click += new System.EventHandler(this.goButton_Click);
//
// addressBox
//
this.addressBox.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.addressBox.Location = new System.Drawing.Point(8, 8);
this.addressBox.Name = "addressBox";
this.addressBox.Size = new System.Drawing.Size(344, 20);
this.addressBox.TabIndex = 0;
this.addressBox.Text = "";
//
// panel2
//
this.panel2.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.panel2.Controls.AddRange(new System.Windows.Forms.Control[] {
this.axWebBrowser1});
this.panel2.Location = new System.Drawing.Point(8, 48);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(392, 240);
this.panel2.TabIndex = 1;
//
// axWebBrowser1
//
this.axWebBrowser1.ContainingControl = this;
this.axWebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.axWebBrowser1.Enabled = true;
this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
this.axWebBrowser1.Size = new System.Drawing.Size(392, 240);
this.axWebBrowser1.TabIndex = 0;
//
// MyWebBrowser
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(408, 293);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panel2,
this.panel1});
this.Name = "MyWebBrowser";
this.Text = "MyWebBrowser";
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MyWebBrowser());
}
private void goButton_Click(object sender, System.EventArgs e)
{
object o = null;
object address = (object)addressBox.Text;
axWebBrowser1.Navigate2(ref address, ref o, ref o, ref o, ref o);
}
}
}
MyWebBrowser.zip( 126 k)
WebBrowser in action
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
private System.Windows.Forms.WebBrowser webBrowser1;
public Form1() {
InitializeComponent();
webBrowser1.Navigate("http://www.kutayzorlu.com/java2s/com");
}
private void InitializeComponent()
{
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.SuspendLayout();
//
// webBrowser1
//
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(0, 0);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(600, 600);
this.webBrowser1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(600, 600);
this.Controls.Add(this.webBrowser1);
this.Text = "Split Window";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Error Provider to validate the text in a TextBox
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class ErrorForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnValidate;
private System.Windows.Forms.ErrorProvider errorProvider1;
private System.Windows.Forms.TextBox txtInput;
public ErrorForm()
{
InitializeComponent();
CenterToScreen();
}
private void InitializeComponent()
{
this.errorProvider1 = new System.Windows.Forms.ErrorProvider();
this.label1 = new System.Windows.Forms.Label();
this.txtInput = new System.Windows.Forms.TextBox();
this.btnValidate = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// errorProvider1
//
this.errorProvider1.BlinkRate = 500;
this.errorProvider1.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(376, 56);
this.label1.TabIndex = 2;
this.label1.Text = "The following text box only allows 5 characters.";
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(144, 80);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(120, 20);
this.txtInput.TabIndex = 0;
this.txtInput.Text = "";
this.txtInput.Validating += new System.ComponentModel.CancelEventHandler(this.txtInput_Validating);
//
// btnValidate
//
this.btnValidate.Location = new System.Drawing.Point(16, 72);
this.btnValidate.Name = "btnValidate";
this.btnValidate.Size = new System.Drawing.Size(112, 32);
this.btnValidate.TabIndex = 1;
this.btnValidate.Text = "OK";
//
// ErrorForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(400, 125);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.btnValidate,
this.txtInput});
this.Name = "ErrorForm";
this.Text = "Error Trapper";
this.ResumeLayout(false);
}
static void Main()
{
Application.Run(new ErrorForm());
}
private void txtInput_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if(txtInput.Text.ToString().Length > 5) {
errorProvider1.SetError( txtInput, "Can't be greater than 5!");
}
else{
errorProvider1.SetError(txtInput, "");
}
}
}
TextBox value validation Demo
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblName;
private System.Windows.Forms.Label lblAddress;
private System.Windows.Forms.Label lblOccupation;
private System.Windows.Forms.Label lblAge;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnHelp;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtAddress;
private System.Windows.Forms.TextBox txtOccupation;
private System.Windows.Forms.TextBox txtAge;
public Form1()
{
InitializeComponent();
this.btnOK.Enabled = false;
this.txtAddress.Tag = false;
this.txtAge.Tag = false;
this.txtName.Tag = false;
this.txtOccupation.Tag = false;
this.txtName.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
this.txtAddress.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
this.txtOccupation.Validating += new System.ComponentModel.CancelEventHandler(this.txtOccupation_Validating);
this.txtAge.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtAge_KeyPress);
this.txtAge.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
this.txtName.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
this.txtAddress.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
this.txtAge.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
this.txtOccupation.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
}
private void InitializeComponent()
{
this.lblName = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.lblAge = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.txtAge = new System.Windows.Forms.TextBox();
this.txtAddress = new System.Windows.Forms.TextBox();
this.lblAddress = new System.Windows.Forms.Label();
this.lblOccupation = new System.Windows.Forms.Label();
this.txtOccupation = new System.Windows.Forms.TextBox();
this.btnHelp = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblName
//
this.lblName.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left);
this.lblName.Location = new System.Drawing.Point(8, 16);
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(92, 23);
this.lblName.TabIndex = 0;
this.lblName.Text = "Name";
//
// txtName
//
this.txtName.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.txtName.Location = new System.Drawing.Point(112, 16);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(176, 20);
this.txtName.TabIndex = 5;
this.txtName.Text = "";
//
// lblAge
//
this.lblAge.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left);
this.lblAge.Location = new System.Drawing.Point(8, 144);
this.lblAge.Name = "lblAge";
this.lblAge.Size = new System.Drawing.Size(92, 23);
this.lblAge.TabIndex = 3;
this.lblAge.Text = "Age";
//
// btnOK
//
this.btnOK.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.btnOK.Location = new System.Drawing.Point(296, 16);
this.btnOK.Name = "btnOK";
this.btnOK.TabIndex = 10;
this.btnOK.Text = "OK";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// txtAge
//
this.txtAge.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.txtAge.Location = new System.Drawing.Point(112, 136);
this.txtAge.Name = "txtAge";
this.txtAge.Size = new System.Drawing.Size(176, 20);
this.txtAge.TabIndex = 8;
this.txtAge.Text = "";
//
// txtAddress
//
this.txtAddress.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.txtAddress.Location = new System.Drawing.Point(112, 40);
this.txtAddress.Multiline = true;
this.txtAddress.Name = "txtAddress";
this.txtAddress.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtAddress.Size = new System.Drawing.Size(176, 72);
this.txtAddress.TabIndex = 6;
this.txtAddress.Text = "";
//
// lblAddress
//
this.lblAddress.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left);
this.lblAddress.Location = new System.Drawing.Point(8, 40);
this.lblAddress.Name = "lblAddress";
this.lblAddress.Size = new System.Drawing.Size(92, 23);
this.lblAddress.TabIndex = 1;
this.lblAddress.Text = "Address";
//
// lblOccupation
//
this.lblOccupation.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left);
this.lblOccupation.Location = new System.Drawing.Point(8, 120);
this.lblOccupation.Name = "lblOccupation";
this.lblOccupation.Size = new System.Drawing.Size(92, 23);
this.lblOccupation.TabIndex = 2;
this.lblOccupation.Text = "Occupation";
//
// txtOccupation
//
this.txtOccupation.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.txtOccupation.Location = new System.Drawing.Point(112, 112);
this.txtOccupation.Name = "txtOccupation";
this.txtOccupation.Size = new System.Drawing.Size(176, 20);
this.txtOccupation.TabIndex = 7;
this.txtOccupation.Text = "";
//
// btnHelp
//
this.btnHelp.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.btnHelp.CausesValidation = false;
this.btnHelp.Location = new System.Drawing.Point(296, 48);
this.btnHelp.Name = "btnHelp";
this.btnHelp.TabIndex = 11;
this.btnHelp.Text = "Help";
this.btnHelp.Click += new System.EventHandler(this.btnHelp_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(376, 253);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.btnHelp,
this.btnOK,
this.txtAge,
this.txtOccupation,
this.txtAddress,
this.txtName,
this.lblAge,
this.lblOccupation,
this.lblAddress,
this.lblName});
this.MinimumSize = new System.Drawing.Size(384, 280);
this.Name = "Form1";
this.Text = "TestBoxTest";
this.ResumeLayout(false);
}
static void Main()
{
Application.Run(new Form1());
}
private void btnOK_Click(object sender, System.EventArgs e)
{
string output;
output = "Name: " + this.txtName.Text + "
";
output += "Address: " + this.txtAddress.Text + "
";
output += "Occupation: " + this.txtOccupation.Text + "
";
output += "Age: " + this.txtAge.Text;
Console.WriteLine(output);
}
private void btnHelp_Click(object sender, System.EventArgs e)
{
string output;
output = "Name = Your name" + "
";
output += "Address = Your address" + "
";
output += "Occupation = Only allowed value is 'Employee' or empty" + "
";
output += "Age = Your age";
Console.WriteLine(output);
}
private void txtBoxEmpty_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
TextBox tb;
tb = (TextBox)sender;
if (tb.Text.Length == 0)
{
tb.BackColor = Color.Red;
tb.Tag = false;
}
else
{
tb.BackColor = System.Drawing.SystemColors.Window;
tb.Tag = true;
}
ValidateAll();
}
private void ValidateAll()
{
this.btnOK.Enabled = ((bool)(this.txtAddress.Tag) &&
(bool)(this.txtAge.Tag) &&
(bool)(this.txtName.Tag) &&
(bool)(this.txtOccupation.Tag));
}
private void txtOccupation_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text.CompareTo("Employee") == 0 || tb.Text.Length == 0)
{
tb.Tag = true;
tb.BackColor = System.Drawing.SystemColors.Window;
}
else
{
tb.Tag = false;
tb.BackColor = Color.Red;
}
ValidateAll();
}
private void txtAge_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
e.Handled = true; // Remove the character
}
private void txtBox_TextChanged(object sender, System.EventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text.Length == 0 && tb != txtOccupation)
{
tb.Tag = false;
tb.BackColor = Color.Red;
}
else if (tb == txtOccupation && (tb.Text.Length != 0 && tb.Text.CompareTo("Employee") != 0))
{
tb.Tag = false;
}
else
{
tb.Tag = true;
tb.BackColor = SystemColors.Window;
}
ValidateAll();
}
}
The read-only property control.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
public class TimerCounter : System.Windows.Forms.UserControl {
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
private int fCounter = 0;
public int Counter {
get {
return fCounter;
}
}
public TimerCounter() {
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.OnTick);
this.Name = "TimerCounter";
}
private void OnTick(object sender, System.EventArgs e) {
fCounter++;
}
}
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();
private System.Windows.Forms.Label CounterLabel;
private System.Windows.Forms.Button Update;
private System.ComponentModel.Container components = null;
private TimerCounter counter;
public Form1() {
counter = new TimerCounter();
this.CounterLabel = new System.Windows.Forms.Label();
this.Update = new System.Windows.Forms.Button();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(32, 24);
this.label1.Size = new System.Drawing.Size(48, 23);
this.label1.Text = "Counter: ";
this.CounterLabel.Location = new System.Drawing.Point(96, 24);
this.CounterLabel.Size = new System.Drawing.Size(32, 23);
this.Update.Location = new System.Drawing.Point(80, 72);
this.Update.Text = "Update";
this.Update.Click += new System.EventHandler(this.Update_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(224, 133);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.Update,this.CounterLabel,this.label1});
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
private void Update_Click(object sender, System.EventArgs e) {
CounterLabel.Text = counter.Counter.ToString();
}
}