visual studio windows form application #3...

34
Visual Studio Windows Form Application #3 ComboBoxes, CheckBoxes, & MDI Containers Dr. Thomas E. Hicks Computer Science Department Trinity University Use LibraryApp3 Project Visual Studio Windows Form Application #1 Basic Form Properties http://carme.cs.trinity.edu/thicks/3321/Handouts/VS-1-SE-Basic-Form-Tutorial.pdf Visual Studio Windows Form Application #2 Button & TabFrame Properties http://carme.cs.trinity.edu/thicks/3321/Handouts/VS-2-Buttons-TabFrame-Form-Tutorial.pdf Purpose 1] The purpose of this tutorial is to show how to create, and use, a basic Windows Application with Visual Studio. CheckBox, ComboBox, RadioButton, and MaskedTextBox controls will be added to the application created in the first two tutorials. Additional basic form Guidelines will be introduced. 2] Topics included are: A) Rename The Application B) Create Main Menu Page C) Add A Region Initialization D) Application.Exit() E) MenuStrip ToolTips F) CreateParms G) Using Main.cs For Form Control H) MultiDocument Container I) Add A Diagnostic Output Window To Our Application J) lbTrace Region K) Diagnostic Testing Region L) Add Variables To Main M) Add A Reference To The Father & Second Constructor To Users.cs N) Test Communication Between Parent Form & Child Form O) Close Button P) Order By ComboBox Q) Testing ComboBox Conrol Selections - 1 R) Select ComboBox Will Act As A Drill Down Filter 3] More topics included are:

Upload: others

Post on 13-Aug-2020

23 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

Visual Studio Windows Form Application #3 ComboBoxes, CheckBoxes, & MDI Containers

Dr. Thomas E. Hicks

Computer Science Department Trinity University

Use LibraryApp3 Project

Visual Studio Windows Form Application #1 Basic Form Properties

http://carme.cs.trinity.edu/thicks/3321/Handouts/VS-1-SE-Basic-Form-Tutorial.pdf

Visual Studio Windows Form Application #2 Button & TabFrame Properties

http://carme.cs.trinity.edu/thicks/3321/Handouts/VS-2-Buttons-TabFrame-Form-Tutorial.pdf

Purpose

1] The purpose of this tutorial is to show how to create, and use, a basic Windows Application with Visual Studio. CheckBox, ComboBox, RadioButton, and MaskedTextBox controls will be added to the application created in the first two tutorials. Additional basic form Guidelines will be introduced.

2] Topics included are:

A) Rename The Application B) Create Main Menu Page C) Add A Region Initialization D) Application.Exit() E) MenuStrip ToolTips F) CreateParms G) Using Main.cs For Form Control H) MultiDocument Container I) Add A Diagnostic Output Window To Our Application J) lbTrace Region K) Diagnostic Testing Region L) Add Variables To Main M) Add A Reference To The Father & Second Constructor To Users.cs N) Test Communication Between Parent Form & Child Form O) Close Button P) Order By ComboBox Q) Testing ComboBox Conrol Selections - 1 R) Select ComboBox Will Act As A Drill Down Filter

3] More topics included are:

Page 2: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [2]

A. Testing ComboBox Conrol Selections – 2 B. Decision: How To Populate The ComboBox Control C. Populate The ComboBox Control With a Dictionary D. CheckBox E. Radio Buttons F. Password TextBox G. Masked TextBox For HomePhone H. Masked TextBox For CellPhone I. Masked TextBox For UniversityID J. C# Masking Elements K. Masking Inputs

Last Form

1] Our form, at the endo of LibraryApp2, looked like the following:

Page 3: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [3]

Rename The Application

1] Open Project Library3. Right-Mouse Click on the LibraryApp2 Select Rename. Change the name to LibraryApp3. (See Below)

2] Select Solution 'LibraryApp2' change LibraryApp1 to LibraryApp3. (See Below)

3] Re-run your form. Note that this also changes the name of the Application at the top. (See Below)

Create Main Menu Page

1] Right-mouse click on the application Select Add Select Windows Form Call the form Main.cs

Page 4: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [4]

2] We have a new windows form. It is linked to nothing we have no way to run it yet.

3] Open Program.cs Change the Main program to open Main.cs

4] The application now begins with our new form.

5] Add a good title to your form it must include your name. Add the MenuStrip seen below. Select the same color code as on your User.cs form. Add the MenuStrip options seen below.

6] Go to Google and find a library-type image that goes with your MenuStrip color scheme.. Place it in your images folder. Select an image different than mine.

Page 5: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [5]

7] Add the new image to your Resources.

8] Add the background to your image. Notice that I tried to select a background that seems compatible with the colors on the MenuStrip..

Add A Region Initialization

1] The code tends to get long. Many of my pages require more than 40 pages to print. Regions will help you to

concentrate on the functions at hand. Copy/Paste the following into Main.cs. Use your name. //-------------------------------------------------------------------------------- //---------------------- Dr. Hicks Initializtion Utilities ----------------------- #region ----Main--Main_Load--MyRenderer--MyColors--CreateParams ------------------ //-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------

Page 6: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [6]

#endregion ------------- Dr. Hicks Initializtion Utilities ----------------------- //-------------------------------------------------------------------------------- 2] Place move functions Main & Main_Load into the middle of this region. 3] Copy MyRenderer & MyColors from User.cs into the middle of this region. 4] Your region should look much like the following:

Application.Exit()

1] Double-Click on the Exit button (on the menustrip). Add the code necessary to exit the application.

MenuStrip ToolTips

1] Add the following ToolTips to main.

CreateParms

Page 7: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [7]

1] When the user pushes the close button at the top right corner of a window, things begin to close and data can be

lost. Microsoft provides an easy GUI way to remove the close/minimize container in the top right corner; but if we remove it, then there is no minimize.

2] I am going to include a function, called function CreateParams which disables only the option to close the form.

In most applications, a user can start to create/edit a User push the close button and lose all of the work that they have done. We will prevent this. I will tell the potential stakeholders about this during my prototype demo!

3] Copy/Paste function CreateParms into your initialization region of Main. 4] Copy/Paste function CreateParms into your initialization region of Main. //===========================================================================// //== CreateParams ==// //===========================================================================// //== Purpose: Block of code to disable the close box on a form and yet ==// //== control minimize and maximize functionality. ==// //== ==// //== Written By: Dr. Tom Hicks Operating System: Windows 10 ==// //== Date: XX\XX\XXXX Environment: C# VS 2017 ==// //===========================================================================// public const int CS_NOCLOSE = 0x200; protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE; return cp; } } 5] Run your application. Note that the close windows button in the top right corner is disabled; the Exit button will

close the application..

6] Suppose the user starts to create a new user, or edit an existing user. Once they do so, they will have the option to either Save, or Cancel, these changes. If they were to push the Exit button, I bring up a dialog button and tell them that they must finish with their user changes in order to exit. This helps to prevent potential data loss.

Using Main.cs For Form Control

Page 8: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [8]

1] We would definitely like to be able to launch our User form from Main but we would also like to launch it from other forms in our application. Perhaps we are in the Fine Sub-System and wish to bring up the User associated with the fine.

2] The best plan is to load the forms in Main and share access among all the other child forms. 3] Declare variable UserForm in Main.

4] Create UserForm in the constructor for Main. Do not show it yet.

5] Double-Click on the User Sub-System button on the menustrip in Main; in the on-click event, add the code to show the UserForm.

6] Run your application Push the User Sub-System button. As you can see below, both forms will come up at

one time.

MultiDocument Container

Page 9: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [9]

1] We are about to make Main a multi-document container. Once we do, we will be able to bind all other forms to fit within that container.

2] In the Properties of Main, make IsMdiContainer = true. Once you do, the background will appear to go gray; don't

worry about that.

3] Bind the UserForm to Main. It will open inside it.

4] Run your form; launch the UserForm. See how it stays inside Main.

5] Try minimizing your UserForm.

Page 10: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [10]

6] Set the MenuStrips on both Main.cs and User.cs to not allow merge.

Add A Diagnostic Output Window To Our Application

1] Expand your form to allow room for a testing output window.

2] Drag a ListBox from the ToolBox to your form. The ListBox is nice because it allows us to add multiple lines to the output control. Drag the lbTrace window bigger. (See Below).

3] Select the font layout below for your listbox..

Page 11: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [11]

4] Name the listbox lbTrace.

lbTrace Region

1] Copy/Paste the lbTrace region to Users.cs maybe below the Initialization region?

//-------------------------------------------------------------------------------- //--------------------- Dr. Hicks Functions For lbTrace -------------------------- #region -----lbTraceLine-lbTraceNote---------------------------------------------- //-------------------------------------------------------------------------------- //================================================================================// // lbTraceLine // //================================================================================// // Purpose: Helper function for lbTraceNote. // //================================================================================// public void lbTraceLine(String Note, String ch = "=") { int len = Note.Length; if (len <= 0) return; String newStr = ch + ch; int Padding = Convert.ToInt16(18 - 0.5 * Note.Length); for (int pos = 1; pos <= Padding; pos++) newStr = newStr + " "; newStr = newStr + Note; while (newStr.Length < 38) newStr = newStr + " "; newStr = newStr + ch + ch; lbTrace.Items.Add(newStr); }

//================================================================================// // lbTraceNote // //================================================================================// // Purpose: Dr. Hicks routine for producing boxed output in lbTrace. // //================================================================================//

Page 12: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [12]

public void lbTraceNote(String ch, String Note1, String Note2 = "", String Note3 = "", String Note4 = "", String Note5 = "") { if (ch == "=") lbTrace.Items.Add("========================================"); else { lbTrace.Items.Add(" "); lbTrace.Items.Add("----------------------------------------"); } lbTraceLine(Note1, ch); lbTraceLine(Note2, ch); lbTraceLine(Note3, ch); lbTraceLine(Note4, ch); lbTraceLine(Note5, ch); if (ch == "=") lbTrace.Items.Add("========================================"); else lbTrace.Items.Add("----------------------------------------"); } //-------------------------------------------------------------------------------- #endregion ------------ End Dr. Hicks lbTrace Functions ------------------------ //--------------------------------------------------------------------------------

Diagnostic Testing Region

1] Copy/Paste the lbTrace region to Users.cs

// -------------------------------------------------------------------------------- //---------------------- Dr.Hicks Diagnostic Testing ------------------------------ #region ----TestingMaster-TestModule1, TestModule2, ... --------------------------- // -------------------------------------------------------------------------------- //===========================================================================// //== TestingMaster ==// //===========================================================================// //== Purpose: Testing Master which evokes TestModule1, TestModule2, ... ==// //== ==// //== ==// //== Written By: Dr.Tom Hicks Operating System: Windows 10 ==// //== Date: XX\XX\XXXX Environment: C# VS 2017 ==// //===========================================================================// public void TestingMaster() { int UserClassDiagnosticLevel = 1; if ((UserClassDiagnosticLevel <= 1) || (UserClassDiagnosticLevel == -1)) TestModule1(); //if ((UserClassDiagnosticLevel <= 2) || (UserClassDiagnosticLevel == -1)) // TestModule2(); //if ((UserClassDiagnosticLevel <= 3) || (UserClassDiagnosticLevel == -1)) // TestModule3(); //if ((UserClassDiagnosticLevel <= 4) || (UserClassDiagnosticLevel == -1)) // TestModule4(); } //===========================================================================// //== Test Module 1 - Testing llbTrace Functions ==// //===========================================================================// //== Purpose: Use the lbTrace Functions to display the contents of all of ==// //== the local variables in Users.cs ==//

Page 13: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [13]

//===========================================================================// public void TestModule1() { lbTraceNote("-", "Test Module 1", "Testing lbTrace Functions", "Display User Variables"); lbTrace.Items.Add("Testing ....... = " + Testing.ToString()); lbTrace.Items.Add("Database ...... = " + Database); lbTrace.Items.Add("Port........... = " + Port.ToString()); lbTrace.Items.Add("DataToggle .... = " + DataToggle.ToString()); lbTrace.Items.Add("QuickLinkText.. = " + QuickLinkText); lbTrace.Items.Add(""); } // -------------------------------------------------------------------------------- #endregion ----Dr. Hicks Diagnostic Testing --------------------------------------- // --------------------------------------------------------------------------------

2] At the Prototype level, there will be minimal need for the diagnostic testing and the lbTrace Utilities, but we shall

use them a little. 3] When my students use actually implement this application with a database, it is not unusual to have 30-50

diagnostic levels. 4] Alter the code for the on-click event for btnTest to be as follows:

5] Run the form. Push the btnTest you should get the following:

6] We shall use the Diagnostic levels in the next section.

Add Variables To Main

1] Add the following three public variables to the top of Main.cs

2] Main.cs is the parent form. All other forms are going to be associated with this MDI container. They will be child

forms. In this application, there might be forms for Users, Books, CheckOut transactions, Fine transactions, etc. 3] It will be the case that the Main.cs form will call/open a User form Maybe to add a new user?

Page 14: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [14]

4] It will be the case that someone comes to check out a book the librarian is in the checkout sub-system. While there we need to look up the Users record because they forgot their card. The librarian might need to open user form to look up the account.

5] It might be the case that someone comes to pay a fine the librarian is in the Fine Payment sub-system. While

there we need to look up the Users record because they don't know the amount they owe. The librarian might need to open user form to look up the account.

6] We have just cited the need to open a user form from three other forms; within the library application, I could give

you at least four or five other examples. 7] The easiest way to handle this is to declare all of the forms in Main.cs. You can see UserForm declared above. 8] Communication between the Parent and Child forms have to be established in order for the child forms to be

able to access each other. There are a number of ways to make this happen, but the one I share below is among the easiest.

9] We are going to pass a reference to the parent form to the constructor. Change

to

Add A Reference To The Father & Second Constructor To Users.cs

1] Use form User.cs Add variable pf (short for parent-form).

2] Use form User.cs Add a second constructor to the user form; when called, it will establish a special relationship with the parent. The parent, in this case, has to be of class Main.

Test Communication Between Parent Form & Child Form

1] Use form User.cs Make the following change to TestingMaster:

Page 15: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [15]

2] Add/Code TestModule2 to the Diagnostic Testing Region

3] Run the application and push btnTest.

4] So far we have validated that the child form can display variables declared in Main.cs, but we have not verified

that child form can access public variables in the parent form, but we have not verified that the child form can change those variables.

5] Modify TestModule2. Change the UserName to mlewis and the Password to 987 & display.

5] Run the application and push btnTest. We have now confirmed that we can change public variables that are

declared in the parent form from the child form.

Page 16: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [16]

7] It is important to note that many of the database functions, such as Connect, ReadRecord etc. can be placed in Main.cs and called from each of the child forms. In order to call the function, you would simply do something like pf.Connect().

8] If we were using form CheckInForm, we could execute pf.UserForm.Show() to show the user form. 9] On some of my applications, I have as many as 20+ forms attached to Main most of these forms access the

database. It does not make sense to have more than twenty connections for the database server to manage from just one client. It is much better to define one connection in Main open the connection and share the open connection with the 20+ forms. Passing information between forms is important.

Close Button

1] Double-click on the menustrip Close button. Add the code to hide the form.

2] You should be able to launch the application. You should now be able to repeatedly open and close the User

Sub-System form TRY IT!

Order By ComboBox

1] Drag a ComboBox from the ToolBox to the User.cs form.

Name the ComboBox control cbOrderBy Add a Prompt Order By

2] Open the edit control in the top right corner.

Page 17: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [17]

3] Select Edit Items from the Tasks.

4] Enter Name, Email, ID, & Dept. Push the OK button.

5] Run the application. Note that the control starts out empty. It needsngM to be initialized.

6] When we first start the form, we might want the order to start with Order By Name. Change the Text Property to Name.

Testing ComboBox Conrol Selections - 1

1] Make the following change to TestingMaster

2] Copy/Paste TestModule3 to the Diagnostic Testing Region.

Page 18: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [18]

//===========================================================================// //== Test Module 3 - Testing ComboBox Controls ==// //===========================================================================// //== Purpose: Display the selections of all ComboBox Controls ==// //===========================================================================// public void TestModule3() { lbTraceNote("-", "Test Module 3", "ComboBox Control", "Settings"); lbTrace.Items.Add("cbOrderBy.Text ....... = " + cbOrderBy.Text); lbTrace.Items.Add(""); } 3] Run the application. Make multiple selections with the cbOrderBy push btnTest as you make selections. You

should be able to get something like the following:

9] When applying code to your applications, it is imperative that you know how to determine the ComboBox

selections.

GUIDELINE # 28 Most Sub-System forms should have an Order By ComboBox.

Select ComboBox Will Act As A Drill Down Filter

Page 19: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [19]

1] Drag a ComboBox from the ToolBox to the User.cs form.

Name the ComboBox control cbSelect Add a Prompt Select Set the Text All Users

2] Set the choices as shown below:

3] Your Control should look like the following:

4] When we first start the form, we might want the order to start with Order By Name. Change the Text Property to

Name.

Testing ComboBox Conrol Selections - 2

1] Add the following line of code to TestModule3

2] Run your application. Test multiple selections.

Guideline 29

Page 20: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [20]

GUIDELINE # 29 Whenever appropriate, use ComboBoxes to reduce the likelihood of Data Entry Errors.

cbDeptID

1] Change the size of txtDeptID to 26, 26 move it to the right. (See Below) 2] Change the prompt from Dept ID to Dept 3] Add a ComboBox, called cbDeptID, to your User.cs form. Place it beside the Dept ID prompt. (See Below)

4] Prevent Data Entry Errors If the user were to enter a Department name (1) they would sometimes misspell

it, or attempt to abbreviate it a combobox of choices would be better. 5] Prevent Data Entry Errors If the user were to enter a Department name (1) they would sometimes enter an

invalid choice a combobox of choices would be better. 6] Prevent Data Entry Errors If the user were to enter a Department name (1) they would sometimes forget

what the choices were a combobox of choices would be better. 9] Prevent Data Entry Errors If the user were to enter a Department ID number (1) they would sometimes

forget which number went with which department a combobox of choices would be better. 10] Prevent Data Entry Errors If the user were to enter a Department ID number (1) they would sometimes

use an l instead of a 1 or a O instead of a 0 a combobox of choices would be better. 11] Prevent Data Entry Errors If the user were to enter a Department ID number (1) they would sometimes

enter an invalid choice a combobox of choices would be better. 12] It is true that selecting a numeric is better than selecting a string Searching would be faster. 13] It is true that selecting a numeric is better than selecting a string Data storage requirements would be less.

Decision: How To Populate The ComboBox Control

1] At the prototype level, it is acceptable to manually populate the ComboBox controls.

Page 21: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [21]

GUIDELINE # 30 If the selections in the ComboBox (dropdown control) might change in the next ten years, the software engineer should populate that dropdown control with a file or a database table; the control should not be manually populated.

2] If the selections in the ComboBox are not likely to change, it is acceptable to manually populate the ComboBox controls.

3] Trinity University has added at least one major in the last ten years. 4] I want all of my students to know how to populate ComboBox controls with both display values and key values;

this can be done with two dimensional arrays, classes, lists, and dictionary objects. The easiest way to do this is to bind the control to a database table.

Populate The ComboBox Control With a Dictionary

1] Suppose the database table has the following Department Information.

2] At the prototype level, it will be sufficient to include five or six choices; include the Select Major choice.

3] Copy/Paste function PopulateDeptIDComboBox into User.cs //===========================================================================// //== PopulateDeptIDComboBox ==// //===========================================================================// //== Purpose: Use a Dictionary to populate the cbDeptID ComboBox with ==// //== Department Names and IDs. ==// //== ==// //== Written By: Dr.Tom Hicks Operating System: Windows 10 ==// //== Date: XX\XX\XXXX Environment: C# VS 2017 ==// //===========================================================================//

public void PopulateDeptIDComboBox() {

} 4] Add the following code to your new function. Use at least five departments of your choice.

Page 22: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [22]

5] Call this new function in your Load event. Run your application.

6] Modify TesModule3 //===========================================================================// //== Test Module 3 - Testing ComboBox Controls ==// //===========================================================================// //== Purpose: Display the selections of all ComboBox Controls ==// //===========================================================================// public void TestModule3() { lbTraceNote("-", "Test Module 3", "ComboBox Control", "Settings"); lbTrace.Items.Add("cbOrderBy.Text ....... = " + cbOrderBy.Text); lbTrace.Items.Add("cbSelect.Text ........ = " + cbSelect.Text); lbTrace.Items.Add("cbDeptID.DisplayValue .= " + cbDeptID.Text.ToString()); lbTrace.Items.Add("cbDeptID.SelectedValue = " + cbDeptID.SelectedValue.ToString()); lbTrace.Items.Add(""); }

7] Do whatever you need to fill DeptID ComboBox when you push the DATA button. Hint: FillFormVariables should select one of the departments

8] Do whatever you need to fill DeptID ComboBox when you push the DATA button. Hint: Hint: FillFormBlank should select "--- Select Major ---"

9] Run the Application. If you have completed the update to FillFormVariables, you should have one of the departments selected. Push btnTest note that my DeptID is 1.

Page 23: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [23]

10] Try the control. Note that the cbDeptID is properly populated for your chosen departments.

11] Change the Dept selection with cbDeptID ==> Push btnTest note that my DeptID is 3.

12] Push the DATA button. If you have completed the update to FillFormBlank, the combobox selection should be

"--- Select Major ---" If the user were adding a new record, we would like the Dept choice to be asking them to make a selection.

13] Check out your other selections as well make sure that each is returning the proper selected vaue. 14] If this were a final project, we could actually open a file and fill the control with pairs of values in about the same

amount of code; this is not necessary for the prototype.

Page 24: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [24]

15] f this were a final project, we could actually connect to a database and fill the control with pairs of values in about

the same amount of code; this is not necessary for the prototype. 16] f this were a final project, we could actually bind the database table directly to the control. 17] Any of these three techniques would allow the end user to add/edit/delete Departments during the next ten

years. It would not be necessary to go back to the developers to create an update deploy that update to a multitude of workstations etc.

18] It is often a good idea to put the ComboBox selections in alphabetical order.

CheckBox

GUIDELINE # 31 Whenever appropriate, use CheckBoxes to reduce the likelihood of Data Entry Errors.

1] Change the size of txtAdministrator to 26,26 move it to the right. (See Below) 2] Drag a CheckBox from the ToolBox to the User.cs form.

2] Configure the CheckBox

Set the Name chAdministrator Set the Text to blank 3] It would be nice if we could increase the size of the CheckBox by changing the font but it does not work that

way.

4] Make the following changes to TestingMaster

Page 25: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [25]

5] Copy/Paste TestModule4 to the Diagnostic Testing Region. //===========================================================================//

//== Test Module 4 - Testing ComboBox Controls ==// //===========================================================================// //== Purpose: Display the selections of all CheckBox Controls ==// //===========================================================================// public void TestModule4() { lbTraceNote("-", "Test Module 4", "CheckBox Control", "Settings"); lbTrace.Items.Add("chAdministrator.Checked ....... = " + chAdministrator.Checked.ToString()); lbTrace.Items.Add("");

}

6] Do whatever you need to do to check the Administrator CheckBox when you push the DATA button. Hint: FillFormVariables should be checked

7] Do whatever you need to do to uncheck the Administrator CheckBox when you push the DATA button. Hint: FillFormBlank should be unchecked

8] Run the Application. If you have completed the update to FillFormVariables, you should have chAdministrator

checked.. Push btnTest note that my chAdministrtor is checked.

9] Push the DATA button. If you have completed the update to FillFormBlank, the checkbox should be unchecked

If the user were adding a new record, we would like the Administrator checkbox should have no selection.

Page 26: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [26]

10] Although it is a bit more hassle programmatically, this is far better than trusting the user to enter T and F. They

may enter t, f, y, n, Y, N, etc. This will help reduce the probability of data entry errors.

Radio Buttons

GUIDELINE # 32 Whenever appropriate, use Radio Buttons to reduce the likelihood of Data Entry Errors.

1] Change the size of txtGenderID to 26,26 move it to the far right. (See Below) 2] Change the prompt from Gender ID to Gender. 3] Drag a Radio Button from the ToolBox to the User.cs form. Configure radioButton1

Set the Name rbFemale Set the Text Female Change the font match the rest of your form 4] Drag a second Radio Button from the ToolBox to the User.cs form. Configure radioButton2

Set the Name rbMale Set the Text Male Change the font match the rest of your form

5] Make the following changes to TestingMaster.

Page 27: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [27]

6] Copy/Paste TestModule5 to the Diagnostic Testing Region. //===========================================================================// //== Test Module 5 - Testing Radio Button Controls ==// //===========================================================================// //== Purpose: Display the selections of all Radio Button Controls ==// //===========================================================================// public void TestModule5() { lbTraceNote("-", "Test Module 5", "Radio Button Control", "Settings"); lbTrace.Items.Add("rbFemale.Checked ....... = " + rbFemale.Checked.ToString()); lbTrace.Items.Add("rbMale.Checked ......... = " + rbMale.Checked.ToString()); lbTrace.Items.Add(""); }

7] Do whatever you need to do to check the Gender Radio Buttons when you push the DATA button. Hint: FillFormVariables one of the gender buttons should be checked

8] Do whatever you need to do to uncheck the Gender Radio Buttons when you push the DATA button. Hint: FillFormBlank neigther of the gender buttons should be checked

9] Run the Application. If you have completed the update to FillFormVariables, you should have one of the two

radio buttons checked. Push btnTest note that only one of the two radio buttons are checked.

10] Select the other Gender choice. Push btnTest. Note that when I pushed the Female selection, the Male selection

was automatically unselected. When you have multiple sets of radio buttons, you will have to enclose them in panels.

Page 28: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [28]

11] Push the DATA button. If you have completed the update to FillFormBlank, all of the radio buttons should be

unchecked If the user were adding a new record, we would like the Gender radio buttons should have no selection.

12] Although it is a bit more hassle programmatically, this is far better than trusting the user to enter 1 and 2

properly. Even if we changed it to Boolean, the user may enter t, f, y, n, Y, N, etc. This will help reduce the probability of data entry errors.

Password TextBox

GUIDELINE # 33 Passwords should never be exposed unless this option is selected by the users.

1] Select txtPassword Change the PasswordChar to * (See Below)

2] This provides better security.

3] When Adding, or Editing, a record, there should be one TextBox for txtPassword and another TextBox for

txtPasswordConfirmation. 4] Try selecting other characters to mask out the password maybe #

Page 29: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [29]

5] Run your application. It should look something like the following:

Masked TextBox For HomePhone

GUIDELINE # 34 Whenever appropriate, TextBoxes should be masked to provide to reduce the likelihood of Data Entry Errors.

1] Delete txtHomePhone.

2] Drag a MaskedTextBox form the ToolBox to your form. Name the control txtHomePhone. Make the size and font match those of the deleted TestBox.

3] Push the button to expand the MaskedTextBox choices.

4] Push the Set Mask… button.

Page 30: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [30]

5] Select this Phone Number selection. Push the OK button.

6] We could create custom mask.

( the left parentheses will be automatically recorded for the user 999 user may enter up to 3 digits (0-9) will ignore any other keys ) blank the right parentheses and the blank will be automatically recorded for the user 000 user must enter 3 digits (0-9) will ignore any other keys - the dash will be automatically recorded for the user 0000 user must enter 4 digits (0-9) will ignore any other keys

7] Make sure that you name the control txtHomePhone.

8] Push DATA until you get the blank form. Start the Data Entry Enter 123ablO.>4567

9] The mask forces better entry. The l is not interpreted as a 1 and the O is not interpreted as a 0. All invalid data is ignored.

10] If you use a custom mask of (000) 000-0000, it will force/require the user to enter all 10 digits.

11] Make sure that function FillFormVariables presents a nicely formatted Home Phone.

Page 31: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [31]

Masked TextBox For CellPhone

1] Delete txtCellPhone.

2] Mask it as a phone number just as we did the HomePhone above.

3] Make sure you name the new MaskedTextBox txtCellPhone.

4] Make sure that function FillFormVariables presents a nicely formatted Cell Phone.

Masked TextBox For UniversityID

Page 32: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [32]

GUIDELINE # 35 TextBoxes should be masked to provide for better Data Entry when possible; mask for Numeric Values (such as UniversityID.

1] We are preparing our University Library Application to be usable at a number of multitude of colleges and universities. All of the university IDs are numeric (0-9) and none of them require more than 9 digits.

2] Delete txtUniversityID. 3] Create a custom form for 9 digits

4] Name the control txtUniversityID. 5] When ready for data entry, your form should look much like this:

C# Masking Elements

Page 33: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [33]

Masking element Description

0 Digit, required. This element will accept any single digit between 0 and 9.

9 Digit or space, optional.

# Digit or space, optional. If this position is blank in the mask, it will be rendered as a space in the Text property. Plus (+) and minus (-) signs are allowed.

L Letter, required. Restricts input to the ASCII letters a-z and A-Z. This mask element is equivalent to [a-z A-Z] in regular expressions.

? Letter, optional. Restricts input to the ASCII letters a-z and A-Z. This mask element is equivalent to [a-z A-Z]? in regular expressions.

a Alphanumeric, optional. If the AsciiOnly property is set to true, the only characters it will accept are the ASCII letters a-z and A-Z. This mask element behaves like the "A" element.

. Decimal placeholder. The actual display character used will be the decimal symbol appropriate to the format provider, as determined by the control's FormatProvider property.

, Thousands placeholder. The actual display character used will be the thousands placeholder appropriate to the format provider, as determined by the control's FormatProvider property.

: Time separator. The actual display character used will be the time symbol appropriate to the format provider, as determined by the control's FormatProvider property.

/ Date separator. The actual display character used will be the date symbol appropriate to the format provider, as determined by the control's FormatProvider property.

$ Currency symbol. The actual character displayed will be the currency symbol appropriate to the format provider, as determined by the control's FormatProvider property.

< Shift down. Converts all characters that follow to lowercase.

> Shift up. Converts all characters that follow to uppercase.

| Disable a previous shift up or shift down.

\ Escape. Escapes a mask character, turning it into a literal. "\\" is the escape sequence for a backslash.

Masking Inputs

Page 34: Visual Studio Windows Form Application #3 …carme.cs.trinity.edu/thicks/3321/Handouts/VS-3-ComboBox...VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr

VS-3-ComboBox-CheckBox-MDI-Containers-Tutorial.docx Software Engineering Dr. Tom Hicks [34]

1] A dropdown control, with the 50 states, would be ideal for US States but if you were to use a MaskedTextBox, the ideal format would be >LL

2] Use the masking to prevent/reduce data entry errors.

Backup

1] Make a backup of LibraryApp3 Call it LibraryApp4

2] Copy LibraryApp1, LibraryApp2, LibraryApp3 & LibraryApp4 to your personal computer.

3] Copy LibraryApp1, LibraryApp2, LibraryApp3 & LibraryApp4 to your flash drive.