using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
public class Form1 : Form
{
private System.Windows.Forms.ListBox listBoxMessages;
private System.Windows.Forms.Button buttonChooseColor;
private ColorDialog chooseColorDialog = new ColorDialog();
public Form1()
{
this.listBoxMessages = new System.Windows.Forms.ListBox();
this.buttonChooseColor = new System.Windows.Forms.Button();
this.SuspendLayout();
this.listBoxMessages.Size = new System.Drawing.Size(288, 199);
this.buttonChooseColor.Location = new System.Drawing.Point(0, 208);
this.buttonChooseColor.Size = new System.Drawing.Size(104, 23);
this.buttonChooseColor.Text = "Choose Color";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 232);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.buttonChooseColor,
this.listBoxMessages});
this.ResumeLayout(false);
buttonChooseColor.Click += new EventHandler(OnClickChooseColor);
try
{
if (ReadSettings() == false)
listBoxMessages.Items.Add("No information in registry");
else
listBoxMessages.Items.Add("Information read in from registry");
StartPosition = FormStartPosition.Manual;
}
catch (Exception e)
{
listBoxMessages.Items.Add("A problem occurred reading in data from registry:");
listBoxMessages.Items.Add(e.Message);
}
}
void OnClickChooseColor(object Sender, EventArgs e)
{
if (chooseColorDialog.ShowDialog() == DialogResult.OK)
BackColor = chooseColorDialog.Color;
}
void SaveSettings()
{
RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software", true);
RegistryKey wroxKey = softwareKey.CreateSubKey("JJJ");
RegistryKey selfPlacingWindowKey = wroxKey.CreateSubKey("SelfPlacingWindow");
selfPlacingWindowKey.SetValue("BackColor", (object)BackColor.ToKnownColor());
selfPlacingWindowKey.SetValue("Red", (object)(int)BackColor.R);
selfPlacingWindowKey.SetValue("Green", (object)(int)BackColor.G);
selfPlacingWindowKey.SetValue("Blue", (object)(int)BackColor.B);
selfPlacingWindowKey.SetValue("Width", (object)Width);
selfPlacingWindowKey.SetValue("Height", (object)Height);
selfPlacingWindowKey.SetValue("X", (object)DesktopLocation.X);
selfPlacingWindowKey.SetValue("Y", (object)DesktopLocation.Y);
selfPlacingWindowKey.SetValue("WindowState", (object)WindowState.ToString());
}
bool ReadSettings()
{
RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software");
RegistryKey wroxKey = softwareKey.OpenSubKey("JJJ");
if (wroxKey == null)
return false;
RegistryKey selfPlacingWindowKey =
wroxKey.OpenSubKey("SelfPlacingWindow");
if (selfPlacingWindowKey == null)
return false;
else
listBoxMessages.Items.Add("Successfully opened key " + selfPlacingWindowKey.ToString());
int redComponent = (int)selfPlacingWindowKey.GetValue("Red");
int greenComponent = (int)selfPlacingWindowKey.GetValue("Green");
int blueComponent = (int)selfPlacingWindowKey.GetValue("Blue");
this.BackColor = Color.FromArgb(redComponent, greenComponent, blueComponent);
listBoxMessages.Items.Add("Background color: " + BackColor.Name);
int X = (int)selfPlacingWindowKey.GetValue("X");
int Y = (int)selfPlacingWindowKey.GetValue("Y");
this.DesktopLocation = new Point(X, Y);
listBoxMessages.Items.Add("Desktop location: " + DesktopLocation.ToString());
this.Height = (int)selfPlacingWindowKey.GetValue("Height");
this.Width = (int)selfPlacingWindowKey.GetValue("Width");
listBoxMessages.Items.Add("Size: " + new Size(Width, Height).ToString());
string initialWindowState = (string)selfPlacingWindowKey.GetValue("WindowState");
listBoxMessages.Items.Add("Window State: " + initialWindowState);
this.WindowState = (FormWindowState)FormWindowState.Parse
(WindowState.GetType(), initialWindowState);
return true;
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
AutoScrollPosition
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
public class Form1 : Form {
private Rectangle rectangleBounds = new Rectangle(new Point(0, 0), new Size(200, 200));
private Rectangle ellipseBounds = new Rectangle(new Point(50, 200), new Size(200, 150));
private Pen bluePen = new Pen(Color.Blue, 3);
private Pen redPen = new Pen(Color.Red, 2);
private Brush solidAzureBrush = Brushes.Azure;
private Brush solidYellowBrush = new SolidBrush(Color.Yellow);
static private Brush brickBrush = new HatchBrush(HatchStyle.DiagonalBrick,Color.DarkGoldenrod, Color.Cyan);
private Pen brickWidePen = new Pen(brickBrush, 10);
public Form1() {
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
Graphics dc = e.Graphics;
Point scrollOffset = this.AutoScrollPosition;
dc.TranslateTransform(scrollOffset.X, scrollOffset.Y);
if (e.ClipRectangle.Top + scrollOffset.X < 350 ||
e.ClipRectangle.Left + scrollOffset.Y < 250) {
dc.DrawRectangle(bluePen, rectangleBounds);
dc.FillRectangle(solidYellowBrush, rectangleBounds);
dc.DrawEllipse(redPen, ellipseBounds);
dc.FillEllipse(solidAzureBrush, ellipseBounds);
dc.DrawLine(brickWidePen, rectangleBounds.Location,ellipseBounds.Location + ellipseBounds.Size);
}
}
public static void Main() {
Application.Run(new Form1());
}
}
[/csharp]
Demonstrates creating a form in a console program
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Form.cs -- Demonstrates creating a form in a console program
//
// Compile this program with the following command line:
// C:>csc Form.cs
using System;
using System.Windows.Forms;
namespace nsForm
{
public class clsMainForm
{
static public void Main ()
{
Application.Run(new Form());
}
}
}
Form for data input
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Employee
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class EmployeeForm1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public EmployeeForm1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected 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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(48, 240);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 24);
this.button1.TabIndex = 2;
this.button1.Text = "Add Item";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(160, 240);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 24);
this.button2.TabIndex = 3;
this.button2.Text = "Cancel";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(38, 32);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(216, 186);
this.listBox1.TabIndex = 0;
this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_OnDoubleClick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listBox1,
this.button2,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new EmployeeForm1());
}
private void button1_Click(object sender, System.EventArgs e)
{
EMPLOYEE emp = new EMPLOYEE();
EmpForm eform = new EmpForm (emp);
if (eform.ShowDialog (this) == DialogResult.Cancel)
return;
eform.GetEmployeeData (out emp);
listBox1.Items.Add (emp);
}
private void button2_Click(object sender, System.EventArgs e)
{
Application.Exit ();
}
private void listBox1_OnDoubleClick(object sender, System.EventArgs e)
{
int index = listBox1.SelectedIndex;
if (index < 0)
return;
EMPLOYEE emp = (EMPLOYEE) listBox1.Items[index];
EmpForm eform = new EmpForm (emp);
if (eform.ShowDialog (this) == DialogResult.Cancel)
return;
eform.GetEmployeeData (out emp);
listBox1.Items.RemoveAt (index);
listBox1.Items.Insert (index,emp);
}
}
public struct EMPLOYEE
{
public string FirstName;
public string LastName;
public string Address;
public string City;
public string State;
public string Zip;
public override string ToString ()
{
return (LastName + ", " + FirstName);
}
}
/// <summary>
/// Summary description for EmpForm.
/// </summary>
public class EmpForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public EmpForm(EMPLOYEE emp)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
textBox1.Text = emp.FirstName;
textBox2.Text = emp.LastName;
textBox3.Text = emp.Address;
textBox4.Text = emp.City;
textBox5.Text = emp.State;
textBox6.Text = emp.Zip;
}
/// <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.textBox4 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox5 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(16, 182);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(120, 20);
this.textBox4.TabIndex = 3;
this.textBox4.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(136, 16);
this.label1.TabIndex = 8;
this.label1.Text = "First Name";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(128, 16);
this.label2.TabIndex = 9;
this.label2.Text = "Last Name";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 109);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(120, 19);
this.label3.TabIndex = 10;
this.label3.Text = "Address";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(168, 184);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(32, 20);
this.textBox5.TabIndex = 4;
this.textBox5.Text = "";
//
// button1
//
this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
this.button1.Location = new System.Drawing.Point(48, 224);
this.button1.Name = "button1";
this.button1.TabIndex = 6;
this.button1.Text = "OK";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.CausesValidation = false;
this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button2.Location = new System.Drawing.Point(160, 224);
this.button2.Name = "button2";
this.button2.TabIndex = 7;
this.button2.Text = "Cancel";
//
// label5
//
this.label5.Location = new System.Drawing.Point(168, 163);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(40, 16);
this.label5.TabIndex = 12;
this.label5.Text = "State";
//
// label6
//
this.label6.Location = new System.Drawing.Point(224, 163);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(48, 16);
this.label6.TabIndex = 13;
this.label6.Text = "Zip";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(16, 82);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(208, 20);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(16, 132);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(208, 20);
this.textBox3.TabIndex = 2;
this.textBox3.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 163);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(120, 16);
this.label4.TabIndex = 11;
this.label4.Text = "City";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(160, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(224, 184);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(48, 20);
this.textBox6.TabIndex = 5;
this.textBox6.Text = "";
//
// EmpForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.button2;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1,
this.label6,
this.label5,
this.label4,
this.label3,
this.label2,
this.label1,
this.textBox6,
this.textBox5,
this.textBox4,
this.textBox3,
this.textBox2,
this.textBox1});
this.MinimizeBox = false;
this.Name = "EmpForm";
this.ShowInTaskbar = false;
this.Text = "EmpForm";
this.ResumeLayout(false);
}
#endregion
public void GetEmployeeData (out EMPLOYEE emp)
{
emp.FirstName = textBox1.Text;
emp.LastName = textBox2.Text;
emp.Address = textBox3.Text;
emp.City = textBox4.Text;
emp.State = textBox5.Text;
emp.Zip = textBox6.Text;
}
private void button1_Click(object sender, System.EventArgs e)
{
this.Close ();
}
}
}
Add controls to a form
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace AddControls
{
///
///
public class AddControls : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
///
///
private System.ComponentModel.Container components = null;
public AddControls()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// button2
//
this.button2.Location = new System.Drawing.Point(160, 240);
this.button2.Name = “button2”;
this.button2.Size = new System.Drawing.Size(96, 24);
this.button2.TabIndex = 3;
this.button2.Text = “Cancel”;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(38, 200);
this.textBox1.Name = “textBox1”;
this.textBox1.Size = new System.Drawing.Size(216, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = “”;
//
// button1
//
this.button1.Location = new System.Drawing.Point(48, 240);
this.button1.Name = “button1”;
this.button1.Size = new System.Drawing.Size(80, 24);
this.button1.TabIndex = 2;
this.button1.Text = “Add Item”;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(38, 32);
this.listBox1.Name = “listBox1”;
this.listBox1.Size = new System.Drawing.Size(216, 147);
this.listBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1,
this.textBox1,
this.listBox1});
this.Name = “AddControls”;
this.Text = “AddControls”;
this.ResumeLayout(false);
}
#endregion
///
///
[STAThread]
static void Main()
{
Application.Run(new AddControls());
}
private void button1_Click(object sender, System.EventArgs e)
{
if (textBox1.Text == “”)
return;
string strAdd = textBox1.Text;
if (listBox1.FindString (strAdd, -1) < 0)
{
listBox1.Items.Add (strAdd);
textBox1.Text = "";
textBox1.Focus ();
return;
}
MessageBox.Show (""" + strAdd + "" is already in the list box", "Duplicate");
}
private void button2_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
}
[/csharp]
Simple form
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace FormSample
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class FormSample : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public FormSample()
{
//
// 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()
{
//
// FormSample
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FormSample());
}
}
}
A form-based Windows Skeleton
// A form-based Windows Skeleton.
using System;
using System.Windows.Forms;
// WinSkel is derived from Form.
public class WinSkel : Form {
public WinSkel() {
// Give the window a name.
Text = "A Windows Skeleton";
}
// Main is used only to start the application.
[STAThread]
public static void Main() {
WinSkel skel = new WinSkel(); // create a form
// Start the window running.
Application.Run(skel);
}
}