Tutorial VisualStudio2008

Embed Size (px)

Citation preview

  • 8/9/2019 Tutorial VisualStudio2008

    1/219

    Tutorial: Visual Studio Windows ApplicationBasic Form Properties

    1] Start Microsoft Visual Studio. Using the mouse, hold down the File Menu --> Select New --> Select Project. (SeeBelow!)

    2] Select C# for the language. Select the Windows Application template. The Faculty form is going to be one ofseveral forms that I wish to create for a Trinity University database application; I am naming the Application TU; itshall reside at the root of drive C.

    Un-Check the Create directory for solution. Using the mouse, select/push the OK button. (See Below!)

    3] All of the files and folders, created by Visual Studio, can be seen in directory C:\TU. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    2/219

    4] A fully executable windows application is now completed. (See Below!)

    Compile & Execute The Windows Application From Within The Visual Studio Application DevelopmentEnvironment

    1] I normally do not use the Debug environment unless I am having problems. I most often launch the applicationwithout the debugging. Using the mouse, hold down the Debug Menu --> Select Start Without Debugging. (SeeBelow!) You can also do this by holding down the Ctrl and F5 Keys.

    2] Since we have added nothing to the form, it is pretty plain, but it does execute.

    Close Form1.

  • 8/9/2019 Tutorial VisualStudio2008

    3/219

    Customize Visual Studio To Add A Start Without Debugging Icon

    1] There are literally hundreds of customizations that you can do with Visual Studio. Since I use Start WithoutDebugging a lot, I like to add it to my Visual Studio Menus. Using the mouse, hold down the Tools Menu --> SelectCustomize. (See Below!)

    2] Select the Commands Tab. Select the Debug Category. Scroll down and select the Start Without Debugging

    Command. Using the mouse grab hold of the Start Without Debugging icon

    and drag it to the desired location on the Visual Studio Tool Bar; I have placed it to the right of the Help Menu choice.(See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    4/219

    Just for the sake of illustration, I have added two copies of this quick launch to the Visual Studio. (See Below!)

    2] I can launch the new form by simply pressing either of the icons.

    Rename A Form

    1] An application will often have many forms. This form shall contain information about Faculty; it should be namedappropriately. Using the mouse, right mouse click on Form1.cs and select Rename. (See Below!) Rename the formFaculty.cs. Do not forget the .cs extension!

    2] This may take a moment or two. It will make numerous changes to the classes.

  • 8/9/2019 Tutorial VisualStudio2008

    5/219

    Program.cs & Main()

    1] Among the things changed with the renaming was Program.cs. Note that Program Main( ) concludes by runningApplication.Run(new Faculty()); it launches the form we just created.

    2] This same plain form still executes; try it.

  • 8/9/2019 Tutorial VisualStudio2008

    6/219

  • 8/9/2019 Tutorial VisualStudio2008

    7/219

    Close the Faculty form.

    Changing The Form Background Color

    1] Return to the properties panel for the form (right mouse clicking on any portion of the form that does not containan object, such as a button, label, text box, combo box, etc. --> Select Properties) Select the BackColor Property.

    Press the color selector drop-down control

    and select a color pallet from Custom, Web, & System. Using the mouse, I have selected a dark blue from the Customcolor pallet. (See Below!)

    2] Save the application and launch the form.

    Close the Faculty form.

    About The Form's Default Font Face, Style, Size & Color

    1] Return to the properties panel for the form (right mouse clicking on any portion of the form that does not containan object, such as a button, label, text box, combo box, etc. --> Select Properties) The default font for the form isused whenever you drag font-oriented controls to the form. Font oriented controls include Labels, Text Boxes, ComboBoxes, etc. Suppose I drag a Label to our form.

    2] In the screen capture below, a label is dragged from the toolbox to the form.

  • 8/9/2019 Tutorial VisualStudio2008

    8/219

    3] When you look at the current Font Property below, you will notice that the Microsoft Sans Serif (8.25 pt) font is

    defined for our form. Label1 uses this font in the screen capture above. When you look at the ForeColor Propertybelow, you will notice that the Black ControlText is defined. Label1 uses this black font color in the screen captureabove.

    Changing The Form Default Font Face, Style, & Size

    1] Return to the properties panel for the form (right mouse clicking on any portion of the form that does not containan object, such as a button, label, text box, combo box, etc. --> Select Properties) The default font for the form isused whenever you drag font-oriented controls to the form. Font oriented controls include Labels, Text Boxes, ComboBoxes, etc. Suppose I drag a Label to our form.

    2] It is indeed the case that you can always change the font style, size, and color, but it is often more efficient tomake the desired settings the default. Suppose we wish to change the default form Font to 12 Point Ariel. Select the

    Font Property. Press font configuration control

  • 8/9/2019 Tutorial VisualStudio2008

    9/219

    3] I wish to use the Ariel Font, Bold Style, and 12 Point Size. Using the mouse, select/push the OK button. (SeeBelow!)

    Changing The Form Default Font Color

    1] Return to the properties panel for the form (right mouse clicking on any portion of the form that does not containan object, such as a button, label, text box, combo box, etc. --> Select Properties) The default font for the form isused whenever you drag font-oriented controls to the form. Font oriented controls include Labels, Text Boxes, ComboBoxes, etc. Suppose I drag a Label to our form.

    2]t is indeed the case that you can always change the font style, size, and color, but it is often more efficient to makethe desired settings the default. Suppose we wish to change the default form Font Color to White. Select the

    ForeColor Property. Press the color selector drop-down control

  • 8/9/2019 Tutorial VisualStudio2008

    10/219

    and select White from the Custom Color Pallet. (See Below!)

    Revisit The Form's Default Font Face, Style, Size & Color

    1] Return to the properties panel for the form (right mouse clicking on any portion of the form that does not containan object, such as a button, label, text box, combo box, etc. --> Select Properties) The default font for the form isused whenever you drag font-oriented controls to the form. Font oriented controls include Labels, Text Boxes, ComboBoxes, etc. Suppose I drag a Label to our form.

    2] A Label is once more dragged from the Toolbox to our form. Note that the font is 12 pt, Bold, Ariel, and White incolor. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    11/219

    Determine The Form Starting Location

    1] Rather than use the Windows Default Location when this form is launched, suppose we wish to place the form 20pixels down and across from the upper left corner of the screen.

    2] Return to the properties panel for the form (right mouse clicking on any portion of the form that does not containan object, such as a button, label, text box, combo box, etc. --> Select Properties) The default font for the form isused whenever you drag font-oriented controls to the form. Font oriented controls include Labels, Text Boxes, ComboBoxes, etc. Suppose I drag a Label to our form.

    3] This is a two step process. The first step is to change the StartPosition Property. There are several options; selectthe Manual starting position. (See Below!)

    4] If the form were launched now, it would be in the upper left corner of the screen because the Location values are0,0. Change Location to 20, 20 to set the Manual Starting Position for our Faculty Form.

  • 8/9/2019 Tutorial VisualStudio2008

    12/219

    5] Save the application and launch the form.

    Close the Faculty form. The StartingPosition Property can also be used to center the from.

    AutoScroll

    1] Return to the properties panel for the form (right mouse clicking on any portion of the form that does not containan object, such as a button, label, text box, combo box, etc. --> Select Properties) The default font for the form isused whenever you drag font-oriented controls to the form. Font oriented controls include Labels, Text Boxes, ComboBoxes, etc. Suppose I drag a Label to our form.

    2] In order to AutoScroll, the AutoScaleMode Property must be set to None.

    3] Change the AutoScroll Property to True. In order to illustrate AutoScroll, there must be some objects on the form.(See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    13/219

    3] Save the application and launch the form.

    4] When you grab the form window by the bottom right corner and begin to close the window, the scroll bars willoccur once you begin to close out some of the form objects. (See Below)

    Form Variables Margin & Padding

    The Margin property defines the space around the control that keeps other controls a specified distance from thecontrol's borders.

    The Padding property defines the space in the interior of a control that keeps the control's content (for example, thevalue of its Text property) a specified distance from the control's borders.

    The following illustration shows the Padding and Margin properties on a control.

    Files & Code

    1] The main program for the project is Program.cs. See the code below!

  • 8/9/2019 Tutorial VisualStudio2008

    14/219

    using System;using System.Collections.Generic;using System.Windows.Forms;

    namespace TU{

    static class Program{

    /// /// The main entry point for the application. ///

    [STAThread]static void Main(){

    Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Faculty());

    }}

    }

    2] Visual Studio GUI creates code behind the Faculty.cs form in file Faculty.Designer.cs. (See Below!)

    namespace TU{

    partial class Faculty{

    /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null;

    /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false.protected override void Dispose(bool disposing)

    { if (disposing && (components != null)){

    components.Dispose();}base.Dispose(disposing);

    }

    #region Windows Form Designer generated code

    /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent(){

    this.SuspendLayout(); // // Faculty //this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 19F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.AutoSize = true;this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;this.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange;this.BackColor = System.Drawing.Color.Navy;this.ClientSize = new System.Drawing.Size(505, 188);this.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold,

    System.Drawing.GraphicsUnit.Point, ((byte)(0)));this.ForeColor = System.Drawing.Color.White;this.Location = new System.Drawing.Point(20, 20);this.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);this.Name = "Faculty";this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;this.Text = "Faculty Application Written By Dr. Thomas E. Hicks";

  • 8/9/2019 Tutorial VisualStudio2008

    15/219

    this.Load += new System.EventHandler(this.Faculty_Load);this.ResumeLayout(false);

    }

    #endregion}

    }

    Binary Executable TU.exe

    1] The TU Project folder is located at c:\TU. Inside the project is a folder, called bin, that holds the binary executable.By default, the binary executable is named like the project with an .exe extension. If you double-click on file TU.exe,it will launch the form above without having to load Visual Studio.

    Try this to make sure it works!

  • 8/9/2019 Tutorial VisualStudio2008

    16/219

    Tutorial: Visual Studio Windows ApplicationLabels & TextBoxes

    Dr. Thomas E. HicksComputer Science Department

    Trinity University

    Purpose

    The purpose of this tutorial is to show how to configure, name, and manipulate labels and textboxes in a basicWindows Application with Visual Studio.

    Purpose

    The purpose of this tutorial is to show how to create a basic Windows Application with Visual Studio.

    Previous Tutorials That Might Help You With This Tutorial

    If You Have Difficulty With This Tutorial, You Might Find These Help Prepare You For This TutorialI Recommend That My Students Complete The Tutorials In This Order!

    Tutorial: Visual Studio 2008 Install

    Tutorial: Visual Studio 2008 SP1 Install

    Tutorial: Visual Studio Windows Application - Forms - Basic Forms

    Project TU

    1] The TU Project that is used for this tutorial is found below; place it at the root of drive C:\

    TU-1.zip

    Creating A Label

    1] Labels are used to identify form content; They are used for prompts and to provide extraneous information onforms. To create a label on the form, drag a Label from the Toolbox to the Faculty form. We are about to create aLabel for a prompt -> First Name. (See Below!)

    http://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-Install/VisualStudio-2008-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-Install/VisualStudio-2008-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-SP1-Install/Visual-Studio-2008-SP1-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-SP1-Install/Visual-Studio-2008-SP1-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Basic-Form/Visual-Studio-Windows-App-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Basic-Form/Visual-Studio-Windows-App-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/MSSQL-Windows-Forms-Basic-Form/TU-1.ziphttp://www.cs.trinity.edu/~thicks/Tutorials/MSSQL-Windows-Forms-Basic-Form/TU-1.ziphttp://www.cs.trinity.edu/~thicks/Tutorials/MSSQL-Windows-Forms-Basic-Form/TU-1.ziphttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Basic-Form/Visual-Studio-Windows-App-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-SP1-Install/Visual-Studio-2008-SP1-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-Install/VisualStudio-2008-Install.html
  • 8/9/2019 Tutorial VisualStudio2008

    17/219

    Moving A Label About The Form

    1] The label is an object created on the form with the line of code:

    this.label1 = new System.Windows.Forms.Label();

    2] When you select the form with the mouse, you can see a box that identifies object label1. A dark background canmake this box more difficult to identify. (See Below!)

    3] Note the appearance of the cross-hairs icon when the mouse is moved over object label1 below.

    4] If you right mouse click on the label, you can drag it to the desired location on the form. Note the box that isformed around the label as you move it.

  • 8/9/2019 Tutorial VisualStudio2008

    18/219

    Changing The Label's Font

    1] Go to the properties panel for the label (right mouse clicking on the label. --> Select Properties) (See Below!)

    2] Select the Font Property. You can see that the current font is 12pt Ariel bold. Suppose we wish to change the font

    to Comic Sans MS. Hold down the font selector (See Below!)

    3] Select the Comic Sans MS font - Regular style - 16 point. Using the mouse, select/push the OK button. (SeeBelow!)

  • 8/9/2019 Tutorial VisualStudio2008

    19/219

    4] Note the font change below.

    5] You may select select and change multiple labels, using the procedure outlined above, at one time.

    6] When doing multiple labels, it is often more efficient to set the Font associated with the form; thus it becomes thedefault.

    Changing The Label's Color

    1] Return to the properties panel for the label (right mouse clicking on the label. --> Select Properties). Select theForeColor Property. You can see that the current forecolor is white. Suppose we wish to change the forecolor to

    yellow. Press the color selector drop-down control (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    20/219

    and select Yellow from the Custom Color Pallet. (See Below!)

    2] Note that the label color has been changed below!

    Changing The Label's Background Color

  • 8/9/2019 Tutorial VisualStudio2008

    21/219

    1] Return to the properties panel for the label (right mouse clicking on the label. --> Select Properties). Select theBackColor Property. You can see that the current backcolor is navy. Suppose we wish to change the forecolor to

    purple. Press the color selector drop-down control (See Below!)

    and select a purple from the Custom Color Pallet. (See Below!)

    2] Note that the label color has been changed below!

    Changing The Label's Text

  • 8/9/2019 Tutorial VisualStudio2008

    22/219

    1] Return to the properties panel for the label (right mouse clicking on the label. --> Select Properties). Select theText Property. You can see that the current text is label1. Suppose we wish to change the text to First Name. Changethe Text Property to First Name. (See Below!)

    2] Replace button1 with First Name. The label is now a prompt for the First Name. (See Below!)

    4] This form still executes; try it. Note that the label object is no longer outlined. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    23/219

    Close the Faculty form.

    Naming A Label

    1] Some developers name each and every object on a form; I name almost every object, except labels. If you wish tochange one or more of the label properties with program code, it should be named; otherwise I encourage you toconsider it optional.

    2] The name should contain no blanks. Suppose we wish to name the label - labFirstName.

    3] Return to the properties panel for the label (right mouse clicking on the label. --> Select Properties). Select theName Property. You can see that the current name is label1. Change the Name Property to labFirstName. (SeeBelow!)

    2] You can see that the label below has now been named.

    Creating A Textbox

    1] Textboxes are used both to display data and to enable the user to change that data. To create a textbox on theform, drag a Label from the ToolBox to the right of the First Name prompt on the Faculty form. We are about tocreate a Textbox which will enable the user to input and/or alter the FirstName. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    24/219

    Moving A Textbox About The Form

    1] The Textbox is an object created on the form with the line of code:

    this.textBox1 = new System.Windows.Forms.TextBox();

    2] When you select the form with the mouse, you can see a box with three controls helps to identify the textbox. (SeeBelow!)

    3] If you grab the right textbox control and drag it to the right, you can expand the textbox from the rightside; dragging it to the left will contract the textbox. (See Below!)

    4] If you grab the left textbox control and drag it to the left, you can expand the textbox from the rightside; dragging it to the right will contract the textbox. (See Below!)

    5] By default, the Textbox control is a single line field. One of the ways that you can change the control to handle

    multiple lines is to grab the top Textbox control

    This control brings up a dialog box

  • 8/9/2019 Tutorial VisualStudio2008

    25/219

    The multiline attribute adds six controls to the textbox (one on the top and one on the bottom and one on eachcorner). You can now expand/contract from the top and bottom. You can expand/contract from any corner. (SeeBelow!)

    6] Note the appearance of the cross-hairs icon when the mouse is moved over object textbox1 below. If you

    right mouse click on the textbox, you can drag it to the desired location on the form. (See Below!)

    Textbox Input

    1] The program should execute as is. Try it. The user can enter data into the form; try it. When designing the form,you should make the container large enough for the user to enter data. Note that Contricissia almost fills thecontainer.

    2] Note that Johassiofati more than fills the container, but the data simply scrolls; none of it is lost, but not all of itis visible.

    Changing The Textbox's Font

    1] Go to the properties panel for the Textbox (right mouse clicking on the Textbox. --> Select Properties) (SeeBelow!)

  • 8/9/2019 Tutorial VisualStudio2008

    26/219

  • 8/9/2019 Tutorial VisualStudio2008

    27/219

    5] This change becomes much more obvious when you execute the application. (See Below!)

    6] You may select select and change multiple Textboxes, using the procedure outlined above, at one time.

    6] When doing multiple Textboxes, it is often more efficient to set the Font associated with the form; thus it becomes

    the default.

    Changing The Textbox's Color

    1] Return to the properties panel for the Textbox (right mouse clicking on the Textbox. --> Select Properties). Selectthe ForeColor Property. You can see that the current forecolor is the black windows text. Suppose we wish to change

    the forecolor to red. Press the color selector drop-down control (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    28/219

  • 8/9/2019 Tutorial VisualStudio2008

    29/219

    and select a purple from the Custom Color Pallet. (See Below!)

    2] Note that the Textbox color has been changed below!

    3] Once again it is best to show this by executing the application. Try it! This is not a good color combination!

  • 8/9/2019 Tutorial VisualStudio2008

    30/219

    Naming A Textbox

    1] It is extremely important to name every Textbox.The name should contain no blanks. The names should clearlyidentify the data. Suppose we wish to name the Textbox - txtFirstName.

    2] Return to the properties panel for the Textbox (right mouse clicking on the Textbox. --> Select Properties). Selectthe Name Property. You can see that the current name is textBox1. Change the Text Property to First Name. (SeeBelow!)

    2] You can see that the Textbox below has now been named.

    Add Additional Labels & Textboxes

    1] Add Labels named labLastName, labFirstName, labID, labGender, labLastSpecialtyID, labAbout

    2] Add TextBoxes named txtLastName, txtFirstName, txtID, txtGender, txtLastSpecialtyID, txtAbout

  • 8/9/2019 Tutorial VisualStudio2008

    31/219

    3] It is generally considered a good design practice to name all textboxes

    4] It is generally considered a good design practice to name labels if you are going to change them programatically!

    Frame Your Form

    1] It is generally considered a good design practice to frame the form content.

    Manipulate the labels and textboxes on your form to create somewhat of a frame around the fields. There should beabout the same amount of blank space at the top, bottome, left, and right of your form. Make the amount of spacebetween the rows consistent; a lot of folk try to make it about the same as the distance to the frame. (See Below!)

    Align Your Prompt Labels & Your Textbox Data Fields

    1] It is generally considered a good design practice to have one or two major columns within a form, or within theform's containers.

    2] Using the mouse, drag/select Labels labFirstName, labSpecialtyID, and labAbout; (See Below!)

    3] When creating reports, sent to printers, it is often considered a good practice to left-justtify the prompts.

    4] When creating forms, it is often considered a good practice to right-justtify the prompts within their respectivecolumns.

    5] Once they are selected, hold down the Format Menu --> Select Align --> Select Rights to align labelslabFtirstName, labSpecialtyID, and labAbout on the right.

  • 8/9/2019 Tutorial VisualStudio2008

    32/219

    6] Using the mouse, drag/select Labels labLastName, labID, and labGender; (See Below!)

    7] Once they are selected, hold down the Format Menu --> Select Align --> Select Rights to align labelslabFirstName, labSpecialtyID, and labAbout on the right.

  • 8/9/2019 Tutorial VisualStudio2008

    33/219

    8] When creating forms, it is often considered a good practice to left-justtify the textboxes within their respectivecolumns.

    9] Using the mouse, drag/select Textboxes txtFirstName, txtSpecialtyID, and txtAbout; you can hold down the ctrlkey and mouse select if you like. (See Below!)

    10] Once they are selected, hold down the Format Menu --> Select Align --> Select Lefts to align TextboxestxtFirstName, txtSpecialtyID, and txtAbout; on the left.

    11] While the three fields are selected, drag the collection of textboxes close, but not too close, to their respectivelabels. (See Below!)

    12] Using the mouse, drag/select Labels labLastName, labID, and labGender; (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    34/219

  • 8/9/2019 Tutorial VisualStudio2008

    35/219

    16] It is often considered a good practice to make the space between the prompt and data entry fields consistent onthe entire form.

    17] While the three fields are selected, drag the collection of textboxes close, but not too close, to their respectivelabels; try to keep this distance consistent throughout the form. (See Below!)

    18] When the prompt is to the left of the data entry control (textbox, checkbox, drop-down, etc), it is oftenconsidered a good practice to make sure that the row data is aligned well horizontally

    19] Using the mouse, drag/select all of the Labels & Textboxes in the first row of the data. (See Below!)

    20] Once they are selected, hold down the Format Menu --> Select Align --> Select Middles to align the first rowhorizontally.

  • 8/9/2019 Tutorial VisualStudio2008

    36/219

    21] Using the mouse, drag/select all of the Labels & Textboxes in the second row of the data. (See Below!)

    22] Once they are selected, hold down the Format Menu --> Select Align --> Select Middles to align the second rowhorizontally.

  • 8/9/2019 Tutorial VisualStudio2008

    37/219

  • 8/9/2019 Tutorial VisualStudio2008

    38/219

    27] Once aligned, your form should look something like the one below.

    Loading Values & Default Values In Textbox Fields.

    1] Specialty ID # 4 is Parallel Processing. Suppose that the majority of Faculty were specializing in ParallelProcessing.

    2] Assign the value "4" to txtSpecialtyID

    3] Return to the properties panel for the Textbox (right mouse clicking on the Textbox. --> Select Properties). Selectthe Text Property. You can see that the current Text is blank. Change the Text Property to 4. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    39/219

  • 8/9/2019 Tutorial VisualStudio2008

    40/219

  • 8/9/2019 Tutorial VisualStudio2008

    41/219

    6] Run the form. As you can see from the mask, the field could be a bit smaller. Try entering the data; note that youdo not have to enter the dashes. It does not verify that the Social Security Number entered is indeed valid, but it willnot accept an 'l' or a '0' or any other invalid character; try it! (See Below!)

    7] There are often many ways to do something. Click the control on top the MaskedTextBox, you will also get anopportunity to set/change the mask. (See Below!)

    8] Add a MaskedTextBox field, called txtOffPhone, to the form. Configure it for the phone number type. (See Below!)

    9] If we wanted the ID to be a four digit number with a dash in the middle, we could create a custom mask 00-00.

    10] If we wanted the input mask to be two digit alpha-numeric, we could make the input mask AA.

    Enable Scroll Bars On MultiLine Textbox

    1] Enter the following data into the txtAbout container. You can paste it in if you like.

  • 8/9/2019 Tutorial VisualStudio2008

    42/219

    Now is the time for all good folk to use C# for rapid prototyping anddevelopment. This prototyping can be done for both web apps and stand alonesystems.

    Welcome to Software Engineering at Trinity University's Computer ScienceDepartment.

    2] The top portion of it scrolled off. You can hit the up arrow key and see it, but this would give the user no indicationthat there is additional data. Yuk! (See Below!)

    3] As we look at the properties for txtAbout , you will notice that the contents of this Multiline are going to bestored in a dynamic String Array . Using the mouse, hold down the ScrollBars and select Vertical. (See Below!)

    4] Once enough data is entered to need the scroll, the control activates; this provides the user with some indicationthat there is additional data. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    43/219

  • 8/9/2019 Tutorial VisualStudio2008

    44/219

    Files & Code

    1] The main program for the project is Program.cs. See the code below!

    using System;using System.Collections.Generic;using System.Windows.Forms;

    namespace TU{

    static class Program{

    /// /// The main entry point for the application./// [STAThread]

    static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Faculty());

    }}

    }

    2] Visual Studio GUI creates code behind the Faculty.cs form in file Faculty.Designer.cs. Notice how the namedobjects make it much easier to identify the various form objects! (See Below!)

    namespace TU{

    partial class Faculty{

    /// /// Required designer variable./// private System.ComponentModel.IContainer components = null;

    /// /// Clean up any resources being used./// /// true if managed resources should be disposed; otherwise, false.protected override void Dispose(bool disposing){

    if (disposing && (components != null)){

    components.Dispose();}base.Dispose(disposing);

    }

    #region Windows Form Designer generated code

    /// /// Required method for Designer support - do not modify/// the contents of this method with the code editor.///

  • 8/9/2019 Tutorial VisualStudio2008

    45/219

  • 8/9/2019 Tutorial VisualStudio2008

    46/219

    this.txtID.Size = new System.Drawing.Size(65, 26);this.txtID.TabIndex = 8;//// labID//this.labID.AutoSize = true;this.labID.Location = new System.Drawing.Point(346, 24);this.labID.Name = "labID";this.labID.Size = new System.Drawing.Size(25, 19);this.labID.TabIndex = 7;this.labID.Text = "ID";//// txtAbout//this.txtAbout.Location = new System.Drawing.Point(118, 212);this.txtAbout.Multiline = true;this.txtAbout.Name = "txtAbout";this.txtAbout.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;this.txtAbout.Size = new System.Drawing.Size(442, 79);this.txtAbout.TabIndex = 10;//// labAbout//this.labAbout.AutoSize = true;this.labAbout.Location = new System.Drawing.Point(58, 212);this.labAbout.Name = "labAbout";this.labAbout.Size = new System.Drawing.Size(55, 19);this.labAbout.TabIndex = 9;this.labAbout.Text = "About";//

    // labOffPhone//this.labOffPhone.AutoSize = true;this.labOffPhone.Location = new System.Drawing.Point(264, 165);this.labOffPhone.Name = "labOffPhone";this.labOffPhone.Size = new System.Drawing.Size(107, 19);this.labOffPhone.TabIndex = 11;this.labOffPhone.Text = "Office Phone";//// labSSN//this.labSSN.AutoSize = true;this.labSSN.Location = new System.Drawing.Point(57, 165);this.labSSN.Name = "labSSN";this.labSSN.Size = new System.Drawing.Size(56, 19);this.labSSN.TabIndex = 12;this.labSSN.Text = "SSN #";//// txtSSN//this.txtSSN.Location = new System.Drawing.Point(118, 163);this.txtSSN.Mask = "000-00-0000";this.txtSSN.Name = "txtSSN";this.txtSSN.Size = new System.Drawing.Size(97, 26);this.txtSSN.TabIndex = 13;//// txtOffPhone//this.txtOffPhone.Location = new System.Drawing.Point(378, 163);this.txtOffPhone.Mask = "(999) 000-0000";this.txtOffPhone.Name = "txtOffPhone";this.txtOffPhone.Size = new System.Drawing.Size(116, 26);this.txtOffPhone.TabIndex = 14;

    //// Faculty//this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;this.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange;this.BackColor = System.Drawing.Color.Navy;this.ClientSize = new System.Drawing.Size(584, 322);this.Controls.Add(this.txtOffPhone);this.Controls.Add(this.txtSSN);this.Controls.Add(this.labSSN);this.Controls.Add(this.labOffPhone);this.Controls.Add(this.txtAbout);this.Controls.Add(this.labAbout);this.Controls.Add(this.txtID);this.Controls.Add(this.labID);this.Controls.Add(this.txtGender);this.Controls.Add(this.labGender);this.Controls.Add(this.txtLastName);this.Controls.Add(this.labLastName);this.Controls.Add(this.txtSpecialtyID);this.Controls.Add(this.labSpecialtyID);this.Controls.Add(this.txtFirstName);this.Controls.Add(this.labFirstName);this.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold,

  • 8/9/2019 Tutorial VisualStudio2008

    47/219

    System.Drawing.GraphicsUnit.Point, ((byte)(0)));this.ForeColor = System.Drawing.Color.White;this.Location = new System.Drawing.Point(20, 20);this.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);this.Name = "Faculty";this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;this.Text = "Faculty Application Written By Dr. Thomas E. Hicks";this.Load += new System.EventHandler(this.Faculty_Load);this.ResumeLayout(false);this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Label labFirstName;private System.Windows.Forms.TextBox txtFirstName;private System.Windows.Forms.TextBox txtSpecialtyID;private System.Windows.Forms.Label labSpecialtyID;private System.Windows.Forms.TextBox txtLastName;private System.Windows.Forms.Label labLastName;private System.Windows.Forms.TextBox txtGender;private System.Windows.Forms.Label labGender;private System.Windows.Forms.TextBox txtID;private System.Windows.Forms.Label labID;private System.Windows.Forms.TextBox txtAbout;private System.Windows.Forms.Label labAbout;private System.Windows.Forms.Label labOffPhone;private System.Windows.Forms.Label labSSN;private System.Windows.Forms.MaskedTextBox txtSSN;

    private System.Windows.Forms.MaskedTextBox txtOffPhone;}}

    Binary Executable TU.exe

    1] The TU Project folder is located at c:\TU. Inside the project is a folder, called bin, that holds the binary executable.By default, the binary executable is named like the project with an .exe extension. If you double-click on file TU.exe,it will launch the form above without having to load Visual Studio.

    Try this to make sure it works!

  • 8/9/2019 Tutorial VisualStudio2008

    48/219

    Tutorial: Visual Studio Windows ApplicationButtons And Color Picker

    Dr. Thomas E. HicksComputer Science Department

    Trinity University

    Purpose

    The purpose of this tutorial is to show how to configure, name, and manipulate buttons in a basic WindowsApplication with Visual Studio.

    Previous Tutorials That Might Help You With This Tutorial

    If You Have Difficulty With This Tutorial, You Might Find These Help Prepare You For This TutorialI Recommend That My Students Complete The Tutorials In This Order!

    Tutorial: Visual Studio 2008 Install

    Tutorial: Visual Studio 2008 SP1 Install

    Tutorial: Visual Studio Windows Application - Forms - Basic Forms

    Tutorial: Visual Studio Windows Application - Forms - Labels & TextBoxes

    Creating A Button

    1] Buttons are used to enable the user to trigger events; To create a Button on the form, drag a Button from theToolbox to the Faculty form. We are about to create a button to Exit the application. (See Below!)

    http://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-Install/VisualStudio-2008-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-Install/VisualStudio-2008-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-SP1-Install/Visual-Studio-2008-SP1-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-SP1-Install/Visual-Studio-2008-SP1-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Basic-Form/Visual-Studio-Windows-App-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Basic-Form/Visual-Studio-Windows-App-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Label-Textbox/Visual-Studio-Windows-App-Form-Label-Textbox.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Label-Textbox/Visual-Studio-Windows-App-Form-Label-Textbox.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Label-Textbox/Visual-Studio-Windows-App-Form-Label-Textbox.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Basic-Form/Visual-Studio-Windows-App-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-SP1-Install/Visual-Studio-2008-SP1-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-Install/VisualStudio-2008-Install.html
  • 8/9/2019 Tutorial VisualStudio2008

    49/219

    Moving A Button About The Form

    1] The Button is an object created on the form with the line of code:

    this.button1 = new System.Windows.Forms.Button();

    2] When you select the form with the mouse, you can see a box that identifies object Button1. Note the appearance

    of the cross-hairs icon when the mouse is moved over object Button1 below. (See Below!)

    3] If you right mouse click on the Button, you can drag it to the desired location on the form. Note the box that isformed around the Button as you move it.

    Changing The Button's Font

    1] Go to the properties panel for the Button (right mouse clicking on the Button. --> Select Properties) (SeeBelow!)

    2] Select the Font Property. You can see that the current font is 12pt Ariel bold. Suppose we wish to change the font

    to Comic Sans MS. Hold down the font selector (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    50/219

  • 8/9/2019 Tutorial VisualStudio2008

    51/219

    1] Return to the properties panel for the Button (right mouse clicking on the Button. --> Select Properties). Selectthe ForeColor Property. (See Below!)

    and select Maroon from the Custom Color Pallet. (See Below!)

    2] Note that the Button color has been changed below!

  • 8/9/2019 Tutorial VisualStudio2008

    52/219

    Changing The Button's Background Color

    1] Return to the properties panel for the Button (right mouse clicking on the Button. --> Select Properties). Selectthe BackColor Property. You can see that the current backcolor is navy. Suppose we wish to change the forecolor to acompatable blue-gray color. (See Below!)

    2] Suppose you screen capture a copy of our form as seen below.

    3] Load http://colorhunter.com into your browser and upload your image (See Below!)

    http://colorhunter.com/http://colorhunter.com/http://colorhunter.com/http://colorhunter.com/
  • 8/9/2019 Tutorial VisualStudio2008

    53/219

    4] I wish to make my buttons like the second color above. Highlight the Navy beside BackColor (See Below!)

    and replace it with #334E8A. (See Below!)

    Changing The Button's Text

    1] I ultimately want my text to fit the button. Now that we have demonstrated how to change the button font andcolor, take a moment to change your button to your button font to 12 pt bold Ariel Gold as shown below.

  • 8/9/2019 Tutorial VisualStudio2008

    54/219

    2] Return to the properties panel for the Button (right mouse clicking on the Button. --> Select Properties). Selectthe Text Property. You can see that the current text is button1. Suppose we wish to change the text to First Name.Change the Text Property to Exit. (See Below!)

    2] Replace button1 with Exit. (See Below!)

    4] This form still executes; try it. Note that the Button object is no longer outlined. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    55/219

    Close the Faculty form.

    Resizing A Button

    1] Select the button with the mouse. You can drag from the top to make it higher, drag from the bottom to make itlower, drag from the right to expand in that direction, and drag from the left to expand it in that direction. You canalso drag at a diagonal from any corner. Try it. (See Below!)

    2] In the screen capture below, you can see that I am dragging/expanding the exit button on the left. (See Below!)

    3] The OK button is now larger. (See Below!)

    Naming A Button

    1] You should name every Button. The name should contain no blanks. Suppose we wish to name the Button -btnExit; you might also name it cmdExit.

    2] Return to the properties panel for the Button (right mouse clicking on the Button. --> Select Properties). Select

    the Name Property. You can see that the current name is Button1. Change the Name Property to btnExit. (SeeBelow!)

  • 8/9/2019 Tutorial VisualStudio2008

    56/219

  • 8/9/2019 Tutorial VisualStudio2008

    57/219

    2] When you double click on a button, it automatically creates a shell for the button's click event in the class codefile. You can see the shell for btnExit_Click in the Facutlty.cs file below. The cursor is even placed in the procedure;we are ready to insert code.

    3] The code necessary to exit the application is

    Application.Exit();

    4] Enter that code in the function.

    5] Run/Compile/Execute your application. Try the Exit button! When the button is pushed/clicked, the click eventassociated with that named button is automatically fired; the application then stops. (See Below!)

    Using An Image For A Button

    1] I downloaded the image below from the Internet. It is neither creative nor good, but it was s bit easier thandrawing one with a paint package. I would like to use it on my Faculty form.

    2] I saved image Exit.jpg in C:\TU. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    58/219

    3] Return to the properties panel for the Button (right mouse clicking on the Button. --> Select Properties). Select

    the Image Property. You can see that there is no current image. Hold down the font selector (SeeBelow!)(See Below!)

    4] Import the image into the project. Using the mouse, select/push the Import button below.

    5] Go to C:\TU and select the Exit.jpg image. Using the mouse select/push the OK button. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    59/219

    6] The Exit Image has now been added to the Resource file associated with the project. Using the mouse select/pushthe OK button. (See Below!)

    7] The button needs to be reshaped. The exit text needs to be removed. (See Below!)

    8] I have reshaped the button below.

    9] You can use any image from the internet, any image from a button collection or any image you draw as abackground for your buttons. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    60/219

    10] This exit image is not very good, but it might be better if you shaded the background. (See Below!)

    About Button Flatstyles

    1] The default flatstyle is Standard.

    2] The left image, below, shows a standard flatstyle button; the right image, below, shows a standard flatstyle buttonwith mouse over. There is no obvious difference.

    3] The flatstyle is now set to popup.

    4] The left image, below, shows a popup flatstyle button; the right image, below, shows a popup flatstyle button withmouse over. Note a slight difference around the image border as it turns light blue.

    5] The flatstyle is now set to flat.

  • 8/9/2019 Tutorial VisualStudio2008

    61/219

  • 8/9/2019 Tutorial VisualStudio2008

    62/219

    6] The call to MessageBox.Show brings up a message box and displays the string passed as an argument. (SeeBelow!)

    7] This is cool, but it does not show the full name. Change the code to the following:

    8] Enter an integer ID and push the ShowName button. (See Below!)

    9] The call to MessageBox.Show brings up a message box and displays the string passed as an argument. (SeeBelow!)

    10] A more compact version of code which does the same thing can be seen below. (See Below!)

    11] This is cool, but it does not show the full name. Change the code to the following:

  • 8/9/2019 Tutorial VisualStudio2008

    63/219

    12] Enter data such as that below and push the ShowName button. (See Below!)

    13] Note that all of the character-type data has been changed to upper case. (See Below!)

    14] This is cool, but it does not show the full name. Change the code to the following:

    15] Enter data such as that below and push the ShowName button. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    64/219

    Tutorial: Tutorial: Visual Studio Windows ApplicationMenu Driven Application With Multiple Forms

    Purpose

    The purpose of this tutorial is to show how to configure, name, and manipulate menus in a basic Windows Applicationwith Visual Studio; buttons on a single form shall open multiple database forms within a single application.

    Tutorial: MSSQL-Forms - Windows Application & Basic Form Properties

    Design Considerations

    1] It is always best to think about design at the beginning of a project. The best way to design a menu driven systemis to design the upper level main menu first and design your lower-level data forms later. This is great in theory, but Ihave found that my students often design at least one or two of the lower-level data forms before they begin to thinkabout the main menu.

    2] Within this tutorial, I am going to assume that we have already completed the TU Faculty form backed upin directory C:\TU-Faculty . We will try to figure out how to get that form into our new menu driven system.

    3] Let us assume that we would like to have an upper level menu with four options:

    1. Bring Up The Faculty Form2. Bring Up The Specialty ID Form3. Bring Up The Student Form

    4. Exit

    Rename The Main Form To MainMenu.cs

    1] Change the name of Form1.cs to MainMenu.cs

    2] Select a background Color (Dark Blue) for the application.

    3] Select a default Font face (Arie), Font Color (White), Font Style (Bold)

    4] Open the application at the top of your screen - about the middle. You can adjust the size in accordance with yourbuttons later.

    5] Give the application a proper title (Trinity University Management System Written By Dr. Thomas E. Hicks) ; itshould include who designed the Application .

    Tutorial: MSSQL-Forms - Windows Application & Basic Form Properties

    http://www.cs.trinity.edu/~thicks/Tutorials/MSSQL-Windows-Forms-Basic-Form/MSSQL-Windows-Forms-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/MSSQL-Windows-Forms-Basic-Form/MSSQL-Windows-Forms-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/MSSQL-Windows-Forms-Basic-Form/MSSQL-Windows-Forms-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/MSSQL-Windows-Forms-Basic-Form/MSSQL-Windows-Forms-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/MSSQL-Windows-Forms-Basic-Form/MSSQL-Windows-Forms-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/MSSQL-Windows-Forms-Basic-Form/MSSQL-Windows-Forms-Basic-Form.html
  • 8/9/2019 Tutorial VisualStudio2008

    65/219

    6] you can see, Program.cs will start with this form.

    7] you can see, Program.cs will start with this form.

    Add A New Form To A Project

    1] Using the mouse, right mouse click on TU Project --> Select Add --> Select Windows Form ... (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    66/219

  • 8/9/2019 Tutorial VisualStudio2008

    67/219

  • 8/9/2019 Tutorial VisualStudio2008

    68/219

    4] Rename the form to Faculty.cs.

    5] Name the form Faculty. Review and make sure each of your forms are named properly!

    6] Change the form text to reflect which Application (TU ) and which form (Faculty Form) and who wrote or designedit ( Written By Dr. Thomas E. Hicks) (See Below!)

    Create Form Student - Make A Copy Of TUForm.cs - Rename It To Student.cs

    1] Use the process, described above, to make another copy of TUForms.cs.

    2] Rename the form to Student.cs.

    3] Name the form Student. Review and make sure each of your forms are named properly!

    4] Change the form text to reflect which Application (TU ) and which form (Student Form) and who wrote or designedit ( Written By Dr. Thomas E. Hicks) (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    69/219

    Create Form SpecialtyID - Make A Copy Of TUForm.cs - Rename It To SpecialtyID.cs

    1] Use the process, described above, to make another copy of TUForms.cs.

    2] Rename the form to SpecialtyID.cs.

    3] Name the form SpecialtyID. Review and make sure each of your forms are named properly!

    4] Change the form text to reflect which Application (TU ) and which form (Specialty ID Form) and who wrote ordesigned it ( Written By Dr. Thomas E. Hicks) (See Below!)

    Common Student Error

    1] An error message, similar to the one below, is generated when two of your forms have the same name.

  • 8/9/2019 Tutorial VisualStudio2008

    70/219

    Add btnFaculty To MainMenu.cs

    1] Add btnFaculty to MainMenu.cs.

    2] Select a compatible color combination.

    3] Make the flatstyle flat.

    4] Review the tutorial below if you are having problems.

    Tutorial: MSSQL-Forms - Windows Application - Buttons & Textboxes

    5] Add the code below to the btnFaculty click event.

    6] You should now be able to open the faculty form by pushing the button on the main menu. You should be able toclose it and reopen it agian. Adjust the Location Property until the form opens on the left side of you screen slightlybelow the main menu. (See Below!)

    Add btnStudent To MainMenu.cs

    1] Add btnStudent to MainMenu.cs.

    2] Select a compatible color combination.

    3] Make the flatstyle flat.

    4] I suggest that you copy and paste the btnFaculty. Rename the copy. Change the text to Student Sub-System.

    http://www.cs.trinity.edu/~thicks/Tutorials/MSSQL-Windows-Forms-Label-Textbox/MSSQL-Windows-Forms-Label-Textbox.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/MSSQL-Windows-Forms-Label-Textbox/MSSQL-Windows-Forms-Label-Textbox.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/MSSQL-Windows-Forms-Label-Textbox/MSSQL-Windows-Forms-Label-Textbox.html
  • 8/9/2019 Tutorial VisualStudio2008

    71/219

    5] Add the code below to the btnStudent click event.

    6] You should now be able to open the faculty form by pushing the button on the main menu. You should be able to

    close it and reopen it agian. Adjust the Location Property of the Student Menu until the form opens slightly below andslightly right of the Faculty Menu. Note that you can open and close both. (See Below!)

    6] Note that when you close the main form, both the Student and Faculty forms close as well.

    Add btnSpecialtyID To MainMenu.cs

    1] Add btnSpecialtyID to MainMenu.cs.

    2] Select a compatible color combination.

    3] Make the flatstyle flat.

    4] I suggest that you copy and paste the btnFaculty. Rename the copy. Change the text to Specialty Sub-System.

  • 8/9/2019 Tutorial VisualStudio2008

    72/219

    5] Add the code below to the btnSpecialtyID click event.

    6] You should now be able to open the SpecialtyID form by pushing the button on the main menu. You should be ableto close it and reopen it agian. Adjust the Location Property of the SpecialtyID Menu until the form opens slightlybelow and slightly right of the Student Menu. Note that you can open and close all three forms. (See Below!)

    6] Note that when you close the main form, both the Student and Faculty and the SpecialtyID forms close as well.

    Add btnExit To MainMenu.cs

    1] Add btnExit to MainMenu.cs.

    2] Add the standard exit code to the form. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    73/219

    3] Test the application to make sure that all four buttons function properly.

    Tranfer Form Data From TU-3

    1] Open the Project stored in C\:TU-Faculty.

    TU-3.zip

    2] Open the faculty form. Select everything on the form (ctrl-A). Copy the selection (ctrl-C). Close the Application.

    3] Return to the menu-driven TU system. Open the Faculty.cs form. (See Below!)

    4] Paste the data and move into place with your mouse.

    http://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Menu-Multiple-Forms/TU-3.ziphttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Menu-Multiple-Forms/TU-3.ziphttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Menu-Multiple-Forms/TU-3.zip
  • 8/9/2019 Tutorial VisualStudio2008

    74/219

  • 8/9/2019 Tutorial VisualStudio2008

    75/219

    3] Suppose that in the process of working, you close the Faculty form by selecting the X in the top right corner. Do it.

    4] Now you are finished with the Student Subsystem. Close the Student Sub-System.

    5] You are now ready to finish entering the faculty member Tom Hicks. Once more select the Faculty-Subsystem.

    6] We have a problem - the data is gone! Yuk!

    7] Suppose Jane never called us back and once more I need to find Jane. If you select the Student-Subsystem, youwill find that you have to look up Jane's record again. Yuk!

    8] Perhaps there is a way to change the state of the forms so that when we click the menu button at the top, itreturns us to the previous state of that sub-system.

    Form State Problem Solution

    1] Open the code for MainMenu.cs. The proper way to handle the state problem is to create the forms in theMainMenu() constructor and show them in the button code. In order for the both the constructor and the button codeto have access to variables faculty, student, and specialthid, they must be declared at the the top of the form asshown below.

    2] Add the three lines of code to initialize/create the three form objects to the constructor. (See Above!)

    3] The button click event needs only to show the respective forms. (See Below!)

    4] Run MainMenu.cs. When you load the faculty form the first time it works great. When you close it with the X at the

    top of the form and then re-load it you get the following error. Yuk!

  • 8/9/2019 Tutorial VisualStudio2008

    76/219

    5] We can eliminate this error by placing a close button to each of our forms. Create btnClose.

    6] The one line of code for the btnClose click event is as follows:

    7] Copy and paste the btnClose to forms Student and SpecialtyID, add the code behind each of the buttons as well.

    8] Run the application. Load all three forms. Move them around to where you can see them. Enter some data the FirstName, Last Name, and ID into the faculty form as shown below.

    9] Push the Close buttons on all of the forms. Push the Faculty button on the Menu once more. Note that the stateremains constant - the data is still there!.

    10] All is going to work well until the user forgets and closes one of the boxes with the x. This will happen over andover. Yuk!

    11] Solution I is to change the FormBorderStyle Property on the Faculty Form to None. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    77/219

    12] Solution I eliminates the error problem, but the Faculty window can not be moved. It can not be minimized! Ithas to icon to identify the app. It has no title to clearly distinguish this Sub-System from another. Yuk! (See Below!)

    13] Set the FormBorderStyle back to sizeable.

    14] Solution II is to change the ControlBox Property on the Faculty Form to false. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    78/219

    15] Solution II is certainly better than Solution I. The title bar enables you to distinguish one Sub-System fromanother. The window can be moved.. It has an icon to identify the app. Unfortunately it can not be minimized; thereis enough help on the internet to enable you to write a custom minimize button (See Below!)

    16] Set the ControlBox back to true.

    17] Solution III is to add the code, boxed in red, to the top of your Faculty, SpecialtyID, and Student Forms. Thiscode was sent to me by a former student by the name of Mr. Derek Farney. It offers the easiest good solution to theproblem.

    18] As you can see below, this code simply disables the close box. We can still use the app icon, the title bar, the

    minimize, and maximize buttons should we choose.

    19] This is such a good solution, that I wanted to provide you with the code in electronic form; you can copy andpaste it into your form files.

    private const int CS_NOCLOSE = 0x200;protected override CreateParams CreateParams{

    get

  • 8/9/2019 Tutorial VisualStudio2008

    79/219

    {CreateParams cp = base.CreateParams;cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;return cp;

    }}

    MenuStrip Objects

    1] MenuStrip objects offer an alternative to the button-oriented approach illustrated in this system.

    Left For You To Do

    1] This application is a long way from complete. The Student form and SpecialtyID form need to be populated withdata. All three forms will need buttons like New, Delete, Edit, Cancel, Next, Previous, Print, etc.

    2] Visual Studio has the ability to design custom, non-rectangular windows. Enjoy!

  • 8/9/2019 Tutorial VisualStudio2008

    80/219

    Tutorial: Visual Studio Windows ApplicationMenuStrip Driven Applications

    Dr. Thomas E. HicksComputer Science Department

    Trinity University

    Purpose

    The purpose of this tutorial is to show how to configure, name, and manipulate menu strips in a basic WindowsApplication with Visual Studio; buttons on a single menustrip shall open multiple database forms within a singleapplication.

    Previous Tutorials That Might Help You With This Tutorial

    If You Have Difficulty With This Tutorial, You Might Find These Help Prepare You For This TutorialI Recommend That My Students Complete The Tutorials In This Order!

    Tutorial: Visual Studio 2008 Install

    Tutorial: Visual Studio 2008 SP1 Install

    Tutorial: Visual Studio Windows Application - Forms - Basic Forms

    Tutorial: Visual Studio Windows Application - Forms - Labels & TextBoxes

    Tutorial: Visual Studio Windows Application - Forms - Buttons & Color Picker

    Tutorial: Visual Studio Windows Application - Forms - Menu Driven Application With Multiple Forms

    Rename The Basic Windows Application

    1] Launch Visual Studio and create a windows application called GarageManagement. (See Below!)

    2] Change the name of Form1 to Application GarageManagementApplication . Using the mouse, right mouse clickon Form1 and select Rename . (See Below!)

    http://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-Install/VisualStudio-2008-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-Install/VisualStudio-2008-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-SP1-Install/Visual-Studio-2008-SP1-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-SP1-Install/Visual-Studio-2008-SP1-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Basic-Form/Visual-Studio-Windows-App-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Basic-Form/Visual-Studio-Windows-App-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Label-Textbox/Visual-Studio-Windows-App-Form-Label-Textbox.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Label-Textbox/Visual-Studio-Windows-App-Form-Label-Textbox.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Buttons-Color-Picker/Visual-Studio-Windows-App-Form-Buttons-Color-Picker.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Buttons-Color-Picker/Visual-Studio-Windows-App-Form-Buttons-Color-Picker.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Menu-Multiple-Forms/Visual-Studio-Windows-App-Form-Menu-Multiple-Forms.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Menu-Multiple-Forms/Visual-Studio-Windows-App-Form-Menu-Multiple-Forms.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Menu-Multiple-Forms/Visual-Studio-Windows-App-Form-Menu-Multiple-Forms.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Buttons-Color-Picker/Visual-Studio-Windows-App-Form-Buttons-Color-Picker.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Label-Textbox/Visual-Studio-Windows-App-Form-Label-Textbox.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Basic-Form/Visual-Studio-Windows-App-Basic-Form.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-SP1-Install/Visual-Studio-2008-SP1-Install.htmlhttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-2008-Install/VisualStudio-2008-Install.html
  • 8/9/2019 Tutorial VisualStudio2008

    81/219

    3] When ask about renaming all references, select Yes . (See Below!)

    4] Using the mouse, right mouse click on the form and select Properties. Change the form Text property to GarageManagement Application . (See Below!

    Change The Main Application Form To A Multi Document Interface

    1] The first step in configuring this Application form to house all of our Garage Application forms is to make our forma Multi Document Interface type form. Change the IsMdIContainer property of the Garage Management Applicationform to True . (See Below).

  • 8/9/2019 Tutorial VisualStudio2008

    82/219

    Add A MenuStrip To The Form

    1] Using the mouse, drag the MenuStrip from the toolbox to the top of the Garage Management Appplication form.(See Below!)

    2] Note that the MenuStrip object actually appears below the window. Right mouse click on the MenuStrip and selectProperties. (See Below!)

    3] Change the Name property of the MenuStrip to MainMenu . See below..

  • 8/9/2019 Tutorial VisualStudio2008

    83/219

    Setting Up The Main Menu

    1] Move the mouse into the far left cell of the MainMenu (See Below!) Enter Login . Change the Name propertyto Login as well. (See Below!)

    2] Move the mouse into the next cell of the MainMenu (See Below!) Enter Customers . Change the Name propertyto Customers as well. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    84/219

    3] Move the mouse down below Customers in MainMenu (See Below!) Enter Add New Customer . Changethe Name property to AddNewCustomer as well. (See Below!)

    4] Move the mouse down below Customers in MainMenu (See Below!) Enter Search Customers . Changethe Name property to SearchCustomers as well. (See Below!)

    5] Move the mouse To the Right Of Search Customers (See Below!) Enter By Name . Change the Name propertyto SearchCustomersByName as well. (See Below!)

    CC

    6] Move the mouse below SearchByName of the MainMenu (See Below!) Enter By ID . Change the Name Propertyto SearchCustomersByID as well. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    85/219

    7] Move the mouse below SearchCustomers of the MainMenu (See Below!) Enter Print Reports . Changethe Name Property to CustomerPrintReports as well. (See Below!)

    8] Move the mouse below CustomerPrintReports of the MainMenu (See Below!) Enter View Customer. Changethe Name Property to ViewCustomer as well. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    86/219

    9] Creating menus is often a work in progress. Suppose we look at this application and realize that the View Customeris the most often used choice within the Customers menu; thus we would like it to be the top choice. You can see inthe series of screen captures below how selected ViewCustomer by pressing on it with the mouse and then I drug thecell upward one row at a t ime until it reached the top; I then let go of the mouse. (See Below!)

    10] Although you could do it with code, it is a simple process to re-organize the menu choices with the mouse. Youcan also copy and paste menu components. (See Below!)

    11] Move the mouse into the next cell of the MainMenu (See Below!) Enter Customer Purchases . Changethe Name property to CustomerPurchases as well. Were this a real application, there would be many more menuchoices below CustomerPurchase, but this shall suffice for an educational tutorial. (See Below!)

    12] Move the mouse into the next cell of the MainMenu (See Below!) Enter Inventory . Change the Name propertyto Inventory as well. Were this a real application, there would be many more menu choices below Inventory, but thisshall suffice for an educational tutorial. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    87/219

  • 8/9/2019 Tutorial VisualStudio2008

    88/219

    15] Move the mouse into the next cell of the MainMenu (See Below!) Enter Exit . Change the Name propertyto Exit as well. Were this a real application, I would add Lock and Logout below. (See Below!)

    Change The MenuStrip Font

    1] Using the mouse, select the MenuStirp. Right mouse click on the MenuStirp and select Properties. Select thebeside the Font property. (See Below!)

    2] Select Tahoma 11 point bold font for the MenuStrip. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    89/219

  • 8/9/2019 Tutorial VisualStudio2008

    90/219

    Change The Menu Text Color

    1] Using the mouse, push on Customers; select the desired color in the ForeColor property at the left. I have chosena shade of Blue. (See Below!)

    2] Likewise, you can select the ViewCustomer sub-menu under Customers and change the ForeColor property to ashade of Red. (See Below!)

    Change The Sub-Menu Background Color

    1] Using the mouse, right mouse click on the View Customer sub-menu item and change the BackColor property to

    DarkKahki. (See Below!)

    2] Change the other Customers sub-menu BackColor to DarkKahki. You can hold the control key and select all ofthem at one time if you like. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    91/219

    Create The Customer Form

    1] Using the mouse, hold down the Project Menu and select Add Windows Form... (See Below!)

    2] Select the Windows Form template. Name the form ViewCustomers.cs . Using the mouse, select/push the Addbutton. (See Below!)

    3] A tab frame and a variety of buttons have been added to form Customers.cs . It is a great idea to use Tool Tips to

    support almost all of your buttons. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    92/219

    Link The Customer Form To The Garage Management Application - 1

    1] Right mouse click on the Garage Management Application form and select View Code. (See Below!)

    2] By creating the ViewCustomersForm at the top of the GarageManagement Form, the form object will be availableto all other forms and buttons in the Garage Application. (See Below!)

    3] The GarageManagementApplication has already been designated as a Multi Document Interface type formwhen IsMdIContainer was set to True. We are now making the ViewCustomers a child form of parent formGarageManagementApplication. Now that this form has been created and associated with theGarageManagementApplication, it may be shown quickly at any time. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    93/219

    Link The Customer Form To The Garage Management Application - 2

    1] Using the mouse, right mouse click on the Garage Management Application form and select Properties. I am goingto very carefully show you how to use the GUI to assign the events to the MenuStrip. Use the mouse and touch theCustomers menu item one time. (See Below!)

    2] Using the mouse, touch the View Customer menu item one time. (See Below!)

    3] Using the mouse, select/push the Events button at the top of the Properties bar. Note that the ViewCustomers seems to stay selected. (See Below!)

    4] Using the mouse, double click on the Click event field. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    94/219

    5] A ViewCustomer_Click event was just created. Enter the line of code to show the ViewCustomerForm. (See Below!)

    6] Launch the Garage Management Application form.Using the mouse, select View Customer. (See Below!)

    7] Once the other forms are complete, you may can be editing an existing customer when you get a call from one ofyour suppliers about a Purchase Order you sent. You can minimize the Customer form if you like; it will fall to thebottom of the Garage Management Application form. You can then look up the Purchase Order. You may have severalwindows open at any given time. The Multi Document Interface helps to contain the many forms for theapplication. (See Below!)

    Login Form

    1] Create the shell of a Login form. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    95/219

  • 8/9/2019 Tutorial VisualStudio2008

    96/219

    3] Associate this new form with the Mdi parent in public partial class GarageManagementApplication : Form . Add theline of code below. (See Below!)

    4] Use the Events button to associate the Suppliers_Click event with the Garage Management Application MenuStripItem. Enter the line of code to show the form. (See Below!)

    Purchase Order Form

    1] Create the shell of a Purchase Orders form. (See Below!)

    2] Create PurchaseOrders Form at the top of public partial class GarageManagementApplication : Form. Add the lineof code below. (See Below!)

    3] Associate this new form with the Mdi parent in public partial class GarageManagementApplication : Form . Add theline of code below. (See Below!)

    4] Use the Events button to associate the PurchaseOrders_Click event with the Garage Management ApplicationMenuStrip Item. Enter the line of code to show the form. (See Below!)

    Customer Purchases Form

    1] Create the shell of a Customer Purchases form. (See Below!)

    2] Create CustomerPurchases Form at the top of public partial class GarageManagementApplication : Form. Add theline of code below. (See Below!)

    3] Associate this new form with the Mdi parent in public partial class GarageManagementApplication : Form . Add theline of code below. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    97/219

    4] Use the Events button to associate the CustomerPurchases_Click event with the Garage Management ApplicationMenuStrip Item. Enter the line of code to show the form. (See Below!)

    Scheduling Form

    1] Create the shell of a Scheduling form. (See Below!)

    2] Create Scheduling Form at the top of public partial class GarageManagementApplication : Form. Add the line ofcode below. (See Below!)

    3] Associate this new form with the Mdi parent in public partial class GarageManagementApplication : Form . Add theline of code below. (See Below!)

    4] Use the Events button to associate the Scheduling_Click event with the Garage Management Application MenuStripItem. Enter the line of code to show the form. (See Below!)

    Garage Management Application Code

    namespace GarageManagement{

    public partial class GarageManagementApplication : Form{

    ViewCustomers ViewCustomersForm = new ViewCustomers();Login LoginForm = new Login();Inventory InventoryForm = new Inventory();PurchaseOrders PurchaseOrdersForm = new PurchaseOrders();CustomerPurchases CustomerPurchasesForm = new CustomerPurchases();Suppliers SuppliersForm = new Suppliers();Scheduling SchedulingForm = new Scheduling();

    public GarageManagementApplication(){

    InitializeComponent();ViewCustomersForm.MdiParent = this;LoginForm.MdiParent = this;InventoryForm.MdiParent = this;PurchaseOrdersForm.MdiParent = this;CustomerPurchasesForm.MdiParent = this;SuppliersForm.MdiParent = this;SchedulingForm.MdiParent = this;

    }

    private const int CS_NOCLOSE = 0x200;protected override CreateParams CreateParams{

    get{

    CreateParams cp = base.CreateParams;

  • 8/9/2019 Tutorial VisualStudio2008

    98/219

  • 8/9/2019 Tutorial VisualStudio2008

    99/219

  • 8/9/2019 Tutorial VisualStudio2008

    100/219

    Tutorial: Tutorial: Visual Studio Windows ApplicationPassing Data Between Parent And Child Forms Without Delegates

    Dr. Thomas E. Hicks Computer Science Department

    Trinity University

    Purpose

    1] This tutorial is going to create a ParentForm which contains two public variables, No and ParentData . It willcreate a ChildForm which has access to both No and ParentData ; this child form shall changethe ParentData string.

    2] There are a number of ways to pass information between parent and child forms, but this seems to be about theeasiest.

    Create A New Windows Project Called PassingDataBetweenForms

    1] Rename Form1 to ParentForm. (See Below!)

    Add txtData TextBox To The ParentForm

    1] Add a text box, called txtData . Add a label, whose text is txtData = . (See Below!)

    Add Public Variables To The ParentForm

  • 8/9/2019 Tutorial VisualStudio2008

    101/219

    1] Add two public variables, called ParentData and No , to your Parent Form. Initialize ParentData to "HelpMe!" and No to 4 . (See Below!)

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms; namespace PassingDataFromChildToParent{

    public partial class ParentForm : Form

    { public String ParentData = "Help Me!"; public long No = 4;

    Add btnUpdateData To The ParentForm & Configure

    1] Add a button, called btnUpdateData , to your form; the text for the form is to be Update Data . (See Below!)

    2] The purpose of btnUpdateData is to simply transfer a copy of the ParentData string into the txtData textbox.Using the mouse, double-click on btnUpdateData and add the following code. (See Below!)

    3] Run your application. Push the Update Data button. Help Me! should be transferred into the txtData textbox. Yourresults should be similar to those below! (See Below!)

    Add btnShowNo To The ParentForm & Configure

    1] Add a button, called btnShowNo , to your form; the text for the form is to be Show No . (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    102/219

  • 8/9/2019 Tutorial VisualStudio2008

    103/219

    3] Run your application. Push the Double No button. (See Below!)

    4] Now push the Show No button. Parent->No = 8 should be displayed in the MessageBox. Your results should besimilar to those below!

    Add btnDoubleNo To The ParentForm & Configure

    1] Add a button, called btnDoubleNo , to your form; the text for the form is to be Double No . (See Below!)

    2] The purpose of btnDoubleNo to double the long integer value stored in No. Using the mouse, double-clickon btnDoubleNo and add the following code. (See Below!)

    3] Run your application. Push the Double No button. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    104/219

    4] Now push the Show No button. Parent->No = 8 should be displayed in the MessageBox. Your results should besimilar to those below!

    Create Form The ChildFom

    1] Add a new form, called ChildForm. (See Below!)

    Add txtData TextBox To The ChildForm

    1] Add a text box, called txtData . Add a label, whose text is txtData = . (See Below!)

    Add btnOpenChildForm To The ParentForm & Configure

    1] Add a button, called btnOpenChildForm , to your form; the text for the form is to be Open Child Form . (SeeBelow!)

  • 8/9/2019 Tutorial VisualStudio2008

    105/219

    2] The purpose of btnOpenChildForm is to simply open the child form. Using the mouse, double-clickon btnOpenChildForm and add the following code. (See Below!)

    3] Run your application. Push the Open Child Form button. (See Below!)

    Add txtData TextBox To The ChildForm

    1] Add a text box, called txtData . Add a label, whose text is txtData = . (See Below!)

    Add btnOpenChildForm To The ParentForm & Configure

  • 8/9/2019 Tutorial VisualStudio2008

    106/219

    1] Add a button, called btnOpenChildForm , to your form; the text for the form is to be Open Child Form . (SeeBelow!)

    2] The purpose of btnOpenChildForm is to simply open the child form. Using the mouse, double-clickon btnOpenChildForm and add the following code. (See Below!)

    Add btnShowNo To The ChildForm & Configure

    1] We would like to access No and ParentData from the child form. Since the two variables are public in theParentForm, most people try something like the following:

    2] Add a button, called btnOpenChildForm , to your form; the text for the form is to be Open Child Form . (SeeBelow!)

    3] The purpose of btnOpenChildForm is to simply open the child form. Using the mouse, double-clickon btnOpenChildForm and add the following code. The program will not compile because we do not have access tothe data. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    107/219

    Overload The ChildForm ConstructorTo Access The Parent Public Variables, One Must Pass The Reference To The Parent To The Child!

    1] Change The ChildForm: One of the ways to pass data between forms is to send a reference to the parent form tothe child constructor. This is a two step process that begins by creating a ParentForm variable in the ChildForm. Addpf to your Child Form. (See Below!)

    2] Change The ChildForm: Add a second constructor to the ChildForm; passed to this constructor will be a referenceto the ParentForm. Add this second constructor to your code.

    3] Change The ChildForm: Change btnShowNo to use pf. (See Below!)

    4] Change The ParentForm: Call this second variation of the constructor in btnOpenChildForm . (See Below!)(See Below!)

    5] Note that we can now Show No from either form. (See Below!) You might even want to push Double periodically?

  • 8/9/2019 Tutorial VisualStudio2008

    108/219

    6] The ChildForm is now able to access public numeric data in the ParentForm.

    Add btnShowParentData To The ChildForm & Configure

    1] Add a button, called btnShowParentData , to your form; the text for the form is to be Show ParentData . (SeeBelow!)

    2] The purpose of btnShowParentData is to simply open the child form. Using the mouse, double-clickon btnOpenChildForm and add the following code. (See Below!)

    3] Push button Update Data in the ParentForm and button Show ParentData in the ChildForm. (See Below!)

    4] The ChildForm is now able to access public string data in the ParentForm.

    Add btnTransferData To The ChildForm & Configure

    1] Add a button, called btnTransferData , to your form; the text for the form is to be Transfer Data ToParent . Thus far, we have not used the textbox in the Child. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    109/219

    2] The purpose of btnTransferData to copy the contents of the txtData textbox into the string variable ParentData.Using the mouse, double-click on btnTransferData and add the following code. (See Below!)

    3] ParentData contains Help Me! . Trinity University Computer Science is entered into ClientForm->txtData. TheTransfer Data Above To Parent button is pushed. (See Below!)

    4] Button Update Data is pushed on the ParentForm. (See Below!)

    5] The ChildForm has just changed the ParentForm. Passing the ParentForm reference to the ChildForm enables theChildForm to change any public items in the ParentForm; in addition to variables, one can change labels, textboxes,combo boxes, etc. (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    110/219

    Project PassingDataBetweenForms

    1] The PassingDataBetweenForms Project, completed above, is found below.

    PassingDataBetweenForms.zip

    Purpose: make the changes ChildForm->txtData automatically update ParentForm->txtData

    1] Copy PassingDataBetweenForms Project to PassingDataBetweenForms Project-1 Project.

    2] Let us make the changes ChildForm->txtData automatically update ParentForm->txtData.

    Change The ChildForm

    1] Remove button btnTransferData .

    Change The ParentForm

    1] Remove button btnUpdateData .

    The ParentForm->txtData Must Be Public

    1] In order for the ChildForm to change the ParentForm->txtData, it must be public. Open theParentForm.Designer.cs and make it so. (See Below!)

    txtData Change TextChanged Event

    http://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Passing-Data-Between-Parent-Child-Forms/PassingDataBetweenForms.ziphttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Passing-Data-Between-Parent-Child-Forms/PassingDataBetweenForms.ziphttp://www.cs.trinity.edu/~thicks/Tutorials/Visual-Studio-Windows-App-Form-Passing-Data-Between-Parent-Child-Forms/PassingDataBetweenForms.zip
  • 8/9/2019 Tutorial VisualStudio2008

    111/219

    1] Go to the ChildForm. Double-click on the txtData textbox. Add the code below for the event. With each and everychange to the ChildForm-txtData, the ParentForm->txtData will automatically be updated. (See Below!)

    2] Run the application. Open the Child Form. Enter Trinity University in the txtData field of the ChildForm. (SeeBelow!)

  • 8/9/2019 Tutorial VisualStudio2008

    112/219

    Tutorial: MSSQL-Forms - Windows Application - Web Browser

    Dr. Thomas E. HicksComputer Science Department

    Trinity University

    Purpose

    The purpose of this tutorial is to show how to add a web browser control to a Windows Application Form, using VisualStudio.

    Applicability

    1] Although you could easily create your own custom browser, i t is much more likely to be the case that you wouldlike to access web based media on a windows application.

    2] Combining a database with the webbrowser is a great way to dynamically generate content from a database;although this is beyond the scope of this tutorial, the construction of the browser object below will be helpful.

    Adding The WebBrowser

    1] I would like the top right corner of my form to be a WebBrowser. Using the mouse, drag the WebBrowser from thetoolbox to your form and size it accordingly. (See Below!)

    2] The default name for the WebBrowser object is webbrowser1 ; this is fine for our application.

  • 8/9/2019 Tutorial VisualStudio2008

    113/219

    3] Change the URL property of the webbrowser object to the URL to be loaded when the form is launched, if any. Iam entering http://cs.trinity.edu . (See Below!)

    3] Rename the form to WebBrowserDemo. Use the form Text property to add an appropriate commercial to the form.Select a color, font, and font color for your application. Execute your form. Try the links on the pages and you will seethat they work well. (See Below!)

    Enable The User To Select The URL With A Textbox And A Button.

    1] Drag a textbox to your form. Name it txtURL . (See Below!)

    2] Drag a button to your form. Change the Text property of the button to be Go . Change the button nameto btnGoURL . (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    114/219

    3] Double click on the Go button. Add the following code to btnGo_Click event.

    4] Enter either google.com or http://google.com in the text box. Push the Go button. (See Below!)

    5] If you leave the form blank, it will display the message box. If you enter an invalid URL, it does nothing. If youenter a valid URL, it navigates to the page. (See Google Below!)

    http://google.com/http://google.com/http://google.com/http://google.com/
  • 8/9/2019 Tutorial VisualStudio2008

    115/219

    Enable The User To Navigate Back With A Button.

    1] Drag a button to your form. Change the Text property of the button to be Back . Change the button nameto btnBack . (See Below!)

    2] Double click on the Back button. Add the following code to btnBack_Click event. (See Below!)

    3] Load your page. Select a link on the page, such as the About link on the Trinity Computer Science page. Push theback button; it returns you to your original page.

    Enable The User To Navigate Forward With A Button.

    1] Drag a button to your form. Change the Text property of the button to be Forward . Change the button nameto btnForward . (See Below!)

  • 8/9/2019 Tutorial VisualStudio2008

    116/219

    2] Double click on the Forward button. Add the following code to btnForward_Click event. (See Below!)

    3] Load your page. Select a link on the page, such as the About link on the Trinity Computer Science page. Push theback button; push the back button. Note that it takes remarkably little code to implement browser functionality.

  • 8/9/2019 Tutorial VisualStudio2008

    117/219

    Tutorial: Microsoft SQL Server 2008 Express Advanced Install

    About The Software

    1] Microsoft makes distributes a free version of Microsoft SQL Server 2008. It is so functional and reliable that anumber of companies never need to upgrade to a pay version. The advanced package includes a number of additionalsupport tools - such as Microsoft SQL Server Management Studio Express. Microsoft SQL Management Studio Express

    is a GUI tool which enables you to create databases, records, queries, etc. I encourage you to download and installthe advanced version of the software.

    Pre-Requisite Software

    1] If you are going to install Visual Studio 2008, I recommend that you do it first.

    Tutorial: VisualStudio-2008-Install

    2] I recommend that you install all service packs for your operating system before you begin. My students often skipthe requirements and waste installation time because Microsoft SQL Server 2008 Advanced will simply not installunless you have the following software on your computer:

    .NET 3.5 with Service Pack 1 Tutorial : .NET-3.5-Install

    Windows Installer 4.5 Tutorial : Windows-Installer-4.5-Install

    Windows Power Shell Tutorial : Windows-Power-Shell-2.0-Install

    Visual Studio 2008 Service Pack 1 (if you have Visual Studio 2008) Tutorial : Visual-Studio-2008-SP1-Install

    SQL Server 2008 Advanced (32-Bit) Software

    1] Either go to Microsoft.com and download the newest version of the software or use the one below:

    SQL-Server-2008-Adv.zip

    Install The Software

    1] Using the mouse, double-click on the installation software. (See Below!)

    2] Using the mouse, select