c#
Chapter 1: Input, how to create dynamic programs (not finished unfortunately)
Table Of Contents
Introduction to chapter:
This chapter deals with using input, there are a few fundamentally different types of input. However, for now we will focus on user input using controls. But lets go over all the different types real quick:
User Input. This is inputted by the user into the program while it is running. Most common is through text fields.
Computer input. This is inputted by the computer and can include values such as Enviremont.UserName or random number.
File/database input. This uses data stored on the computer or on a server either created by the user or by other people. Such as a login database or program preferences file.
Programmer input. This is inputted by the programmer straight into the code. Mainly hard coded values such as "Hello ".
Using the above types of input we can create dynamic programs that when run can change and look different, without any input all we can do is look at a black screen, because even starting the computer (pressing the power button, is input).
User Input:
Lets expand the user input some: there are 2 main types mouse inputs and keyboard inputs. However, any peripheral (external device) that communicates to the computer can be used for input, including not limited to scanner, microphone, touchpad, USB drive, and so many others.
Mouse inputs, include moving, primary clicking, secondary clicking, wheel movement, and wheel click for most mice.
Keyboard inputs, include tying any key, key combinations, and some external devices like barcode readers.
Video summary:
Common input controls in Visual Studio:
Common controls can be added to the Form using toolbox.
Common Controls section:
Pointer: used to reset mouse to default behavior if deciding to not add a different control.
Button: Normally uses click event to evaluate a page or submit a form to move to next screen. (allows users to decide when form is updated)
Checkbox: Allows the user to check the box, results in changing the .Checked property to True or False (True is if it is checked).
CheckedListBox : Allows a list of checkboxes, though to use does require casting which we will cover in Chapter 2.
ComboBox: Allows a selection of items to be displayed, such as Apples, Oranges, and Pears. And user to select one value. Which the value selected can be accessed with .SelectedIndex, .SelectedText, and .SelectedItem, and .SelectedValue properties.
DateTimePicker: Allows user to pick date/time, the display can be changed through .Format property. The .Text property can be used to get the text representation of the value. The .Value property can be used to get the Date/Time value, so math can be preformed on it.
Label and LinkLabel: are output and will be covered in more detail in chapter 3.
ListBox: Allows user to select one value by default just like combo box, however, can display multiple items at the same time and allows us to change how many the user can select with .SelectionMode. Similar properties as ComboBox can be used to retrieve first selected value (.SelectedIndex, .Text, .SelectedItem, and .SelectedValue), to retrieve multiple selections use .SelectedIndices, .SelectedItems, and .SelectedValues properties.
ListView: Is a more complex listBox and has more view options to allow different groupings and display styles with .View property Defaults .MultiSelect property to True. And removed the .SelectedValue and .SelectedValues properties.
MaskedTextBox: Allows user to enter text values; which are limited in values. Used in passwords, and data that has particular formatting such as SSN, and phone numbers. Use .Mask property to set the mask. .PasswordChar or .UseSystemPasswordChar property to hide the input. The .Text property will contain what the user types in.
MonthCalendar: expanded form of DateTimePicker, which allows start and end date to be selected. The values can be accessed with .SelectionStart and .SelectionEnd properties (use .ToString(), .ToShortDateString(), .ToLongDateString(), or .ToShortTimeString() methods to display the output)
NotifyIcon: is an output and will be covered in Chapter 3.
NumericUpDown: Allows user to enter a number, and they can change the number using the up and down arrows. .Minimum property sets the lowest number possible and .Maximum property sets the highest number possible. The .Value property gets the number user chose so that math can be done on it.
PictureBox: is an output; however, also can serve as a button, with adding the Click event. (Added exactly the same way as Click event is added with the Button). Normally looks better then adding an image to the button's .Image property.
(Every control can have events added to it, so all output controls can become input controls; however, this one it common for it to be done for.)
ProgressBar: is an output and will be covered in chapter 3.
RadioButton: Is just like a checkbox, except only allows one to be checked at the same time in same container. (Also uses round icon to indicate it is a radio button)
RichTextBox: Used to input paragraph(s) of text by the user. The .Text property can be used to access entire content of the box, or the .Lines property can be used to access the lines like a list.
TextBox: Most commonly used input. Allows the user to enter text of any format. The .MultiLine property is set to false by default. Value can be accessed with .Text, or .Lines just like the RichTextBox.
ToolTip: Is an output and will be covered in Chapter 3.
TreeView: Allows displaying a structure like a file structure. Use .SelectedNode property to get the selected node. Can use .Text property on the .SelectedNode property to get the text of the selected node. (Example treeView1.SelectedNode.Text). Can navigate through the TreeView using .Parent to get the parent node, .NextNode and .PrevNode to get the siblings and .Nodes to get the children Nodes.
WebBrowser: Is an output control and will be discussed in Chapter 3.
Containers: Allows grouping of forms and parts of forms together, especially used in grouping radio buttons together.
Manus & Toolbars: Allows creation of typical drop down menu's shown at the top of the screen typically. And toolbars typically shown at the bottom of the screen.
Components: Allows interacting with the system values, in a more visual way.
Printing: Allows to interact with print options in a visual method. Will cover more in later chapter.
Dialogs: Allows interacting with dialog boxes in a more visual way.
WPF Interoperability: Allows advance form controls, will be covered in a later chapter.
Data: Allows interacting with databases, will be covered in a later chapter.
Inputs, when to use which one.
- Computer input:
Can be used to track most of the states of the computer and are normally reliable; however, documentation, should be read for full understanding of all values that can be given back. (Sometimes bad request such as asking battery life on a desktop, can lead to interesting results.) Often used to get the status of the computer the program is being ran on, not wide range of applicable uses; however, when it can be used, to prevent needless user input it should be used.
- File/database input:
Can be used to "remember" between runs or use other applications output as input for this application. Both are commonly used and done. Databases are almost always preferred, due to faster processing in almost every aspect; however, files are simpler and have lower overhead, so can be used when using a database would be overkill.
- Programmer input:
This should be avoided; however, often is just impractical to avoid. For example, creating a program where the weight on Mars is to be calculated can be done with using the value hardcoded, or can have a dedicated database/file shipped with the program that contains the conversion ratio for Mars to Earth. Either way would result in the correct value; however, hardcoding the value, makes updating the program, harder and creating a dedicated database/file takes more time upfront to connect and use the easily changeable value. To be clear, most of the time, I would hardcode the value; however, if I knew in the future that there would be changes to the values, then I would use the external file/database. This really is always a judgement call.
- User input:
Should only be used when all other methods are either too impractical or not applicable. Some examples would be user's weight doesn't exist in a database we have access to (highly impractical to use DMV database) and we want to calculate the user's weight on Mars, so we have to ask user for their weight. However, the conversion 37.83 for converting the weight would be easy for the programmer to put in as hardcoded value, since that doesn't change often.
Some more considerations for using User input, always treat it as they will give the worst possible information possible, for example in a weight text box a user could enter -120.2lbs. We would be highly advised to use a NumericUpDown and set the minimum to 1lbs and maximum to 1000lbs, because this prevents the user from entering negatives, and the letters "lbs" (which could not have math performed on the entire input until the "lbs" was removed). If we want to make it friendly to our metric users, we could have a combo box to select lbs or kg, to prevent the user from entering ounces into a text box. However, if the input cannot be limited, for example if using text files, handle the input gracefully and catch any problems and display error messages where behavior would otherwise be unpredictable (will be discussed more later when we go over exception handling)
Walkthrough, using inputs in Visual Studio:
This is a follow along exercise, to allow students to learn as you go. All actions will be explained in the parentheses.
Follow Setting up steps from chapter 0 if not already done, change the name of the program to Input example. (Setting up, installs the software, workload, and creates the project, this allows the computer to be in a state to create and run the program).
2. On the left side click the 'Toolbox' and pin it using the thumb tack icon.
3. Add the three GroupBox containers
4. Add the 6 RadioButtons.
5. Add the label and CheckBoxList elements.
6. Add the Label and the two DateTimePicker elements.
7. Add the three CheckBox elements.
8. Add the Add and Reset Button elements
9. Add the Receipt Label and the RichText Box elements.
We are almost done, but since this is learning to program course, lets write our first line of code.
10. Double click anywhere white on the form. (This takes us to the code side of the form and creates an event (action) handle that acts on form load.
11. Enter the code label1.Text = "Hello " + Environment.UserName; between the brackets in Form1_load.
'label1' refers to the element with the 'name' property set to label1 (the default name for first label added to the screen).
'.Text' refers to the Text property of the element that it is directly left of it, in this case the label1's Text property
' = ' Sets the property on the left to the value on the right. (Note all math is done before setting the value).
'"Hello " is a String that contains the letters 'H', 'e', 'l', 'l', 'o', and ' ' (yes the space is a letter). Strings are always just a series of characters.
'Environment' Is referring to the environment the program is being run in, in this case my computer.
'.UserName' is referring to the Environments UserName, in this case, what is the UserName of my computer, the logged in user's UserName. (In my case 'Jimmy M Patton'.
';' Indicates the end of a line, this is mandatory. Because this lets us control the white spacing and lets there be a visual indication where the end of a line is. Just get in the habit of adding it every time you hit enter with anything on the line.
To summarize, the code first looks at the element label1's Text property and set the value to ("Hello " + The logged on user's UserName)
12. Finally lets see the result, just to see what it does. Run the program again, just like in step 7. (This is to verify we have typed everything correctly and verify our program is finished.)
13. Congratulations, you have officially written your first program in C#. Give yourself a pat on the back.
Assignment:
Create a program that when run:
Displays a greeting to the user who logs on (displaying the user's username). (Can be any greeting except Hello)
Displays the greeting and name in Font of 22 or larger. (Hint use the Font Property on the label to change the Font size)
Display a copyright message along the bottom of the screen.
Example:
Summary:
We installed Visual Studio Community edition 2022, with .NET Development Desktop. We created our first hello world program. We modified that program to display the user's username on form load. You created a more advanced one, with larger font and an extra label, to show mastery of skills shown in the 1st lesson.
Overall, very good work this week. Next week, we will cover inputs, and how we get and use inputs from the user.