WinForms-1 Source Code Files === FutureValue1.FutureValue1.Form1.cs ======================= using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using Microsoft.VisualBasic; namespace FutureValue1 { /// /// FutureValue1 Example: Use the Microsoft.VisualBasic.Financial.FV /// method to compute the total amount of principal and interest /// paid to buy a house. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnReset_Click(object sender, EventArgs e) { txtPrincipal.Clear(); txtTermInYears.Clear(); txtInterest.Clear(); txtFutureValue.Clear(); txtPrincipal.Focus(); } private void btnCompute_Click(object sender, EventArgs e) { double principal, interest, monthlyRate, futureValue; int termInYears, termInMonths; bool principalFlag, termInYearsFlag, interestFlag; // Make sure that input textboxes have numeric values. principalFlag = double.TryParse(txtPrincipal.Text, out principal); termInYearsFlag = int.TryParse(txtTermInYears.Text, out termInYears); interestFlag = double.TryParse(txtInterest.Text, out interest); if (!principalFlag || !termInYearsFlag || !interestFlag) return; // Compute future value of initial value. termInMonths = termInYears * 12; monthlyRate = interest / (12 * 100); futureValue = Financial.FV(monthlyRate, termInMonths, 0, -principal, DueDate.EndOfPeriod); txtFutureValue.Text = futureValue.ToString("$#,##0.00"); } private void EnterHandler(object sender, EventArgs e) { // Change text to red when control gains focus. Control c; c = (Control) sender; c.ForeColor = Color.Red; } private void LeaveHandler(object sender, EventArgs e) { // Change text to red when control gains focus. Control c; c = (Control)sender; c.ForeColor = Color.Black; } } } === DragDrop.DragDrop.Form1.cs =============================== using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace DragDrop { /// /// DragDrop Example: Shows how to use drag and drop to copy /// text from one textbox to another. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { txtSource.Text = "Lorem ipsum dolor sit amet, " + "consectetuer adipiscing elit."; txtSource.Select(0, 0); } private void txtSource_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) txtSource.DoDragDrop(txtSource.SelectedText, DragDropEffects.Copy); } private void txtDestination_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; } private void txtDestination_DragDrop(object sender, DragEventArgs e) { txtDestination.SelectedText = e.Data.GetData(DataFormats.Text).ToString(); } } } === KeyPress.KeyPress.Form1.cs =============================== using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace KeyPress { /// /// KeyPress Example: Show how to use the KeyPress event. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void txtInput_KeyPress(object sender, KeyPressEventArgs e) { string key = e.KeyChar.ToString(); e.Handled = true; txtInput.SelectedText = key; txtKey.Text = e.KeyChar.ToString(); txtAsciiCode.Text = Convert.ToInt16(e.KeyChar).ToString(); } } } === Cascade.Cascade.Form1.cs ================================= using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace Cascade { /// /// Cascade Example: Show how cascading events interact: /// txtA.TextChanged causes txtB.TextChanged causes /// txtA.TextChanged, etc. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void txtA_TextChanged(object sender, EventArgs e) { MessageBox.Show("Event txtA_TextChanged"); txtB.Text += "b"; } private void txtB_TextChanged(object sender, EventArgs e) { MessageBox.Show("Event txtB_TextChanged"); txtA.Text += "a"; } } } === TextChanged1.TextChanged1.Form1.cs ======================= using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace TextChanged1 { /// /// TextChanged1 Example: When a character is entered in one /// textbox, change the string in that textbox to the /// appropriate case and copy it to the other textbox. /// /// Backspace('\b') and space (' ') are /// counted as legal characters for each textbox. /// public partial class Form1 : Form { // Boolean Flags to prevent cascading events. private bool upperFlag = false, lowerFlag = false; public Form1() { InitializeComponent(); } private void txtUpperCase_KeyPress(object sender, KeyPressEventArgs e) { // Send only uppercase characters to txtUpper. char c = e.KeyChar; if ((c < 'A' || 'Z' < c) && c != '\b' && c != ' ') { e.Handled = true; upperFlag = false; } else { e.Handled = false; upperFlag = true; } } private void txtUpperCase_TextChanged(object sender, EventArgs e) { // Only enter new string in txtLower if it results from // a KeyPress event, not from any other event. if (upperFlag) { txtLowerCase.Text = txtUpperCase.Text.ToLower(); upperFlag = false; } } private void txtLowerCase_KeyPress(object sender, KeyPressEventArgs e) { // Send only lowercase characters to txtLower. char c = e.KeyChar; if ((c < 'a' || 'z' < c) && c != '\b' && c != ' ') { e.Handled = true; upperFlag = false; } else { e.Handled = false; lowerFlag = true; } } private void txtLowerCase_TextChanged(object sender, EventArgs e) { // Only enter new string in txtLower if it results from // a KeyPress event, not from any other event. if (lowerFlag) { txtUpperCase.Text = txtLowerCase.Text.ToUpper(); lowerFlag = false; } } } } === TrackBar.TrackBarForm1.cs ================================ using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace TrackBar { /// /// TrackBar Example: Shows how to use a TrackBar control to /// enter Fahrenheit values into a textbox. /// /// /// The default event of the TrackBar control is Scroll. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void trkTemp_Scroll(object sender, EventArgs e) { txtTemp.Text = trkTemp.Value.ToString(); } } } === PictureBox.PictureBox.Form1.cs =============================== using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace PictureBox { /// /// PictureBox Example: Displays an image from disk in a PictureBox /// control. /// /// /// Copy the image from the source code folder into the /// root folder of the c: Drive before executing the application. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MessageBox.Show("Click picture box to see image."); } private void pictureBox1_Click(object sender, EventArgs e) { pictureBox1.Image = Image.FromFile("c:/pizza.jpg"); } } } === FutureValue2.FutureValue2.Form1.cs =========================== using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using Microsoft.VisualBasic; namespace FutureValue2 { /// /// FutureValue2 Example: Use the Microsoft.VisualBasic.Financial.FV /// method to compute the total amount of principal and interest /// paid on a loan. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnReset_Click(object sender, EventArgs e) { txtPrincipal.Clear(); txtTermInYears.Clear(); txtInterest.Clear(); txtFutureValue.Clear(); txtPrincipal.Focus(); } private void btnCompute_Click(object sender, EventArgs e) { double principal, interest, monthlyRate, futureValue; int termInYears, termInMonths; bool principalFlag, termInYearsFlag, interestFlag; // Make sure that input textboxes have numeric values. principalFlag = double.TryParse(txtPrincipal.Text, out principal); termInYearsFlag = int.TryParse(txtTermInYears.Text, out termInYears); interestFlag = double.TryParse(txtInterest.Text, out interest); if (!principalFlag || !termInYearsFlag || !interestFlag) return; // Compute future value of initial value. termInMonths = termInYears * 12; monthlyRate = interest / (12 * 100); futureValue = Financial.FV(monthlyRate, termInMonths, 0, -principal, DueDate.EndOfPeriod); txtFutureValue.Text = futureValue.ToString("$#,##0.00"); } private void EnterHandler(object sender, EventArgs e) { // Change text to red when control gains focus. Control c; c = (Control) sender; c.ForeColor = Color.Red; } private void LeaveHandler(object sender, EventArgs e) { // Change text to red when control gains focus. Control c; c = (Control)sender; c.ForeColor = Color.Black; } private void ValidatingHandler(object sender, CancelEventArgs e) { // Don't allow user to leave a textbox if its value // is nonnumeric. string s = ((TextBox)sender).Text; double v; if (!Double.TryParse(s, out v)) { Control c = (Control)sender; errorProvider1.SetError(c, "Nonnumeric Value"); txtPrincipal.Select(0, s.Length); e.Cancel = true; } } private void ValidatedHandler(object sender, EventArgs e) { // If textbox contains legal value when user tries // to leave, clear error. Control c = (Control)sender; errorProvider1.SetError(c, ""); } } } === TextChanged2.TextChanged2.Form1.cs =========================== using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace TextChanged2 { /// /// TextChanged2 Example: When a character is entered /// in one textbox, change the string in that textbox /// to the appropriate case and copy it to the other /// textbox. Use an ErrorProvider control to indicate /// when an illegal character for a textbox is entered. /// /// Backspace('\b') and space (' ') are /// counted as legal characters for each textbox. /// public partial class Form1 : Form { // Boolean Flags to prevent cascading events. private bool upperFlag = false, lowerFlag = false; public Form1() { InitializeComponent(); } private void txtUpperCase_KeyPress(object sender, KeyPressEventArgs e) { // Send only uppercase characters to txtUpper. char c = e.KeyChar; if ((c < 'A' || 'Z' < c) && c != '\b' && c != ' ') { e.Handled = true; upperFlag = false; errorProvider1.SetError(txtUpperCase, "Enter an uppercase letter."); } else { e.Handled = false; upperFlag = true; errorProvider1.SetError(txtUpperCase, ""); errorProvider1.SetError(txtLowerCase, ""); } } private void txtUpperCase_TextChanged(object sender, EventArgs e) { // Only enter new string in txtLower if it results from // a KeyPress event, not from any other event. if (upperFlag) { txtLowerCase.Text = txtUpperCase.Text.ToLower(); upperFlag = false; } } private void txtLowerCase_KeyPress(object sender, KeyPressEventArgs e) { // Send only lowercase characters to txtLower. char c = e.KeyChar; if ((c < 'a' || 'z' < c) && c != '\b' && c != ' ') { e.Handled = true; upperFlag = false; errorProvider1.SetError(txtLowerCase, "Enter a lowercase letter."); } else { e.Handled = false; lowerFlag = true; errorProvider1.SetError(txtUpperCase, ""); errorProvider1.SetError(txtLowerCase, ""); } } private void txtLowerCase_TextChanged(object sender, EventArgs e) { // Only enter new string in txtLower if it results from // a KeyPress event, not from any other event. if (lowerFlag) { txtUpperCase.Text = txtLowerCase.Text.ToUpper(); lowerFlag = false; } } } } === ThreeState.ThreeState.Form1.cs =============================== using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace ThreeState { /// /// ThreeState Example: Show the three states of a /// check box: Unchecked, Checked, Indeterminate (Grayed out). /// /// /// By default, the user can only set the states /// Unchecked and Checked for a checkbox. To /// also allow the user to set the Indeterminate state, /// change the ThreeState property of the checkbox to /// true. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void radUnchecked_CheckedChanged(object sender, EventArgs e) { chkBox.CheckState = CheckState.Unchecked; } private void radChecked_CheckedChanged(object sender, EventArgs e) { chkBox.CheckState = CheckState.Checked; } private void radioButton1_CheckedChanged(object sender, EventArgs e) { chkBox.CheckState = CheckState.Indeterminate; } } } === Beverage.Beverage.KeyPress.Form1.cs ========================== using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace Beverage { /// /// Breakfast Example: A Breakfast Beverage prototype /// order form. /// /// /// When orange juice is selected, the other group /// boxes are disabled because hot orange juice, /// or orange juice with cream and sugar does not /// make sense. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void radCoffee_CheckedChanged(object sender, EventArgs e) { configureForOJ(false); } private void radOJ_CheckedChanged(object sender, EventArgs e) { configureForOJ(true); } private void radTea_CheckedChanged(object sender, EventArgs e) { configureForOJ(false); } private void configureForOJ(bool flag) { groupBox2.Enabled = !flag; groupBox3.Enabled = !flag; radCold.Checked = flag; radHot.Checked = !flag; if (flag) { chkCream.Checked = false; chkSugar.Checked = false; } } private void btnSubmit_Click(object sender, EventArgs e) { string beverageString, tempString, extrasString, displayString; // Set beverage string. if (radCoffee.Checked) beverageString = "coffee"; else if (radOJ.Checked) beverageString = "orange juice"; else beverageString = "tea"; // Set temperature string. if (radCold.Checked) tempString = "cold"; else tempString = "hot"; // Set extras string. if (!chkCream.Checked && !chkSugar.Checked) extrasString = ""; else if (chkCream.Checked && !chkSugar.Checked) extrasString = " with cream"; else if (!chkCream.Checked && chkSugar.Checked) extrasString = " with sugar"; else extrasString = " with cream and sugar"; // Set and show display string. displayString = "You ordered " + tempString + " " + beverageString + extrasString + "."; txtDescription.Text = displayString; } } } === Panel.Panel.Form1.cs ========================================= using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace Panel { /// /// Panel Example: Use radio button input to decide which /// icon to display in a picture box. /// /// /// Copy the .ico files from the source code folder into /// the bin/Debug folder before executing the application. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { radHeart.Checked = true; } private void CheckedChangedHandler(object sender, EventArgs e) { string imageFileName, root; // Get icon file from source code folder and // and display in picture box and window icon. root = ((RadioButton)sender).Text; imageFileName = root + ".ico"; picSuit.Image = Image.FromFile(imageFileName); this.Icon = new Icon(imageFileName); } } } === ListBox.ListBox.Form1.cs ===================================== using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace ListBox { public partial class Form1 : Form { /// /// ListBox Example: Shows how to add, select and /// remove items from a ListBox control. /// public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { lstAnimals.Items.Add("Oppossum"); lstAnimals.Items.Add("Mouse"); lstAnimals.Items.Add("Skunk"); } private void lstAnimals_Click(object sender, EventArgs e) { int i = lstAnimals.SelectedIndex; txtSelectedIndex.Text = i.ToString(); if (i >= 0) txtSelectedItem.Text = lstAnimals.Items[i].ToString(); } private void btnDelete_Click(object sender, EventArgs e) { int i = lstAnimals.SelectedIndex; if (i >= 0) { lstAnimals.Items.RemoveAt(i); lstAnimals.SelectedIndex = -1; txtSelectedIndex.Clear(); txtSelectedItem.Clear(); } } } } === CheckedListBox.CheckedListBox.Form1.cs ======================= using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace CheckedListBox { /// /// CheckedListBox Example: Shows how to access programatically /// the checked items in a CheckedListBox. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { chklstAnimals.Items.Add("Oppossum"); chklstAnimals.Items.Add("Mouse"); chklstAnimals.Items.Add("Skunk"); } private void mnuShowChecked_Click(object sender, EventArgs e) { lstSelected.Items.Clear(); foreach(String animal in chklstAnimals.CheckedItems) lstSelected.Items.Add(animal); } } } === ComboBox.ComboBox.Form1.cs =================================== using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace ComboBox { /// /// ComboBox Example: Show how to add and select /// items from a ComboBox. /// /// /// To remove items from a combobox, use the /// Items.RemoveAt or Items.Remove methods. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { cboAnimals.Items.Add("Oppossum"); cboAnimals.Items.Add("Mouse"); cboAnimals.Items.Add("Skunk"); } private void cboAnimals_SelectedIndexChanged(object sender, EventArgs e) { int i = cboAnimals.SelectedIndex; txtSelectedIndex.Text = i.ToString(); if (i >= 0) txtSelectedItem.Text = cboAnimals.SelectedItem.ToString(); else txtSelectedItem.Clear(); } private void cboText_TextChanged(object sender, EventArgs e) { txtText.Text = cboAnimals.Text; txtSelectedIndex.Text = cboAnimals.SelectedIndex.ToString(); } } } === ImageList.ImageList.Form1.cs ================================= using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace ImageList { /// /// ImageList Example: Displays icon files stored in an /// ImageList control. The icons files are added and retrieved /// by key. /// /// /// Copy the .ico files into the bin/Debug folder before /// executing the application. /// public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Load images into ImageList control. string root; foreach(Control c in panel1.Controls) { root = c.Text; imlIcons.Images.Add(root, Image.FromFile(root + ".ico")); } // Set hearts as default suit. radHeart.Checked = true; } private void CheckedChangedHandler(object sender, EventArgs e) { // Display suit in picture box using image from // ImageList control. Use the text of the radio // button as the key to get the image. string root = ((RadioButton)sender).Text; Image img = imlIcons.Images[root]; picSuit.Image = img; } } } ==================================================================