Zebra0.com

csharpC# Windows Forms Programming

C# Windows Forms Programming

Learn C# (C sharp) in FREE step-by-step lessons.

C# Windows Forms ProgrammingC# Windows Forms Programming

These lessons will use Microsoft Visual Studio to create Windows form applications using C#. (C Sharp)

Module 1: Introduction to C# +/-

Create your first C# program

  1. INDEX (start here)
  2. Microsoft Reference: C# Guide Microsoft Reference: C# Guide: Complete Microsoft Reference for C#

  3. Install Visual Studio

  4. Create your first Program Create your first Program: Step-by-step instructions for creating a C# windows form application

  5. The Visual Studio Menu: Describes the items you will see on menu after you start a C# project.

  6. Your first Program: Hello World Your first Program: Hello World: Shows how to change the text and backcolor of the form at design time.

  7. Adding Pictures in Hello World Program Adding Pictures in Hello World Program: Shows how to add a picture for the background and change some settings

  8. Changing Pictures in Hello World Program Changing Pictures in Hello World Program: Shows some additional setting for the background image

  9. Files in your project Files in your project: Looks at the files that are created as part of a C# project and how to zip the entire folder.

  10. Active Learning: Review what you have learned: Suggestions for practicing what you have learned.

  • Self Study Questions
  • Module 2: Controls in C# +/-

    Learn to add controls to the form.

    1. INDEX (start here)
    2. Microsoft Reference Microsoft Reference: C# Toolbox, Controls on Windows Forms

    3. Controls Controls: Adding controls to the form and change a few properties

    4. Ambient Properties for a Uniform Look Ambient Properties for a Uniform Look: Change font and colors of form first: When control is added it will have those same values

    5. Adding Pictures with PictureBox Adding Pictures with PictureBox: An animated gif is added to a picture box.

    6. Pictures with Other controls Pictures with Other controls: In additon to the form and picture boxes,
      images can also be added to other controls such as label and button.

    7. Adding Tool Tip to controls Adding Tool Tip to controls: Tool tips are the small words that appear when you move the mouse over a control

    8. Image alignment and text alignment: Image and text can be placed in one of 9 positions.

    9. Hot Spots Hot Spots: Labels are arranged over a picture to create hot spots to show parts of computer

    10. Naming controls: Give controls meaningful names such as picComputer or lblInstructions

    11. Naming controls: Give controls meaningful names such as picComputer or lblInstructions

    12. List of Controls: A list of some of the most common controls, names and use

    Module 3: Writing Code in C# +/-

    Write code in C#, to change Properties and create a console app

    1. INDEX (start here)
    2. Microsoft Reference Microsoft Reference: C# Coding Standards

    3. Writing Code to change properties of the form Writing Code to change properties of the form: Instead of changing properties in the design view,
      it can be done with code.

    4. Comments: Comments are notes a programmer makes about the code

    5. Comments: Comments are notes a programmer makes about the code

    6. Code created Code created: An explanation of each line of code

    7. Adding events for controls Adding events for controls: Some events can be added by double clicking on the control

    8. Event: Events are things that happen such as a user action with the mouse or keyboard, and also events such as a timer going off.

    9. Event Handling: Event handling means writing code to specify what to do when an event (such as the user clicking the mouse) occurs.

    10. Adding items to listbox at runtime Adding items to listbox at runtime: Clicking a button adds the conents of a text box to a list box.

    11. Changing the location of a control Changing the location of a control: The location is a Point with x and y values

    12. Mouse Move Event, Arguments Mouse Move Event, Arguments: The mousemove event has argument e that has the location

    13. Using the TextBox Control Using the TextBox Control: After user types their name and presses a button,
      a greeting is displayed

    14. Using the Scroll Bar Control Using the Scroll Bar Control: A scrollbar is used to select inches

    15. Debugging Tools Debugging Tools: A few tools for debugging are shown

    16. Creating a Console App Creating a Console App: A console app runs in the system window instead of a form

    Module 4: Variables +/-

    Declaring and assigning values to variables

    1. INDEX (start here)
    2. Microsoft Reference Microsoft Reference: C# Types and Variables

    3. Variables: Variable definition, types, and how to declare variables

    4. Variable Types: Object types

    5. Drill on type Drill on type: Self study drill to make sure you understand

    6. Variable Names: Rules and guidelines for naming variables

    7. Drill on Names Drill on Names: Self study drill to make sure you understand

    8. Declaring Variables: Syntax for statement to declare a variable

    9. Assigning Values to a Variable: Syntax of the assignment statement to give value to variable

    10. Assign means to give a value to a variable.: This statement assigns a new value to a variable.

    11. Global Variables Global Variables: A global variable can be accessed, changed,
      or modified anywhere in the program.

    12. Global Variables Global Variables: A global variable can be accessed, changed,
      or modified anywhere in the program.

    13. Using the local directory Using the local directory: This shows how to store pictures or other files in the local directory

    14. Using the local directory Using the local directory: This shows how to store pictures or other files in the local directory

    Module 5: Arithmetic Operations +/-

    Learn to use arithmetic operators

    1. INDEX (start here)
    2. Arithmetic Expressions: Arithmetic Expressions are statements that perform a calculation. There are specific rules for the order the operations are performed.

    3. Microsoft Reference: C# Operators Microsoft Reference: C# Operators: C# Operators

    4. Drill: Operators Drill: Operators: Do this drill to make sure you understand

    5. Centering a control on the form Centering a control on the form: The control is centered when the form is resized

    6. Centering a control on the form Centering a control on the form: The control is centered when the form is resized

    7. Remainder operator: %

    8. Remainder operator: %

    9. Drill: The % operator Drill: The % operator: Drill on the % operator: remainder

    10. Finding Dozens: A program to select quantity on scroll bar and display dozens: quantity%12

    11. Finding Feet and Inches Finding Feet and Inches: A scroll bar selects inches, feet and inches is displayed

    12. Order of operations: The Order of operations for arithmetic operations is to do parenthesis () then *, / %, then + and - working from left to right.

    13. Order of operations: The Order of operations for arithmetic operations is to do parenthesis () then *, / %, then + and - working from left to right.

    14. Drill: Order of operations Drill: Order of operations: Test how well you can solve these problems

    15. Calculate Grade: Scroll bars select midterm and final exam, average is calculated

    16. Write a function: Write a function to avoid the same code in two places

    17. Algebra to code: Convert algebraic expressions to programming statements

    18. Decimal values on scroll bars: Value on scroll bar is integer, how to select a decimal value

    19. Decimal values on scroll bars: Value on scroll bar is integer, how to select a decimal value

    20. A function to display values on the scroll event and also form load A function to display values on the scroll event and also form load: Calculate tip, avoid misinformation at form load

    21. Real world math: A few real world problems you can solve in C#

    Module 6: Built in functions in C# +/-

    Built-in functions in C#, Math functions, Parsing, Random numbers

    1. INDEX (start here)
    2. Microsoft Reference Microsoft Reference: C# Math Functions

    3. Math Functions: C# has a number of built-in functions and constants that you can use.

    4. Demonstration of Math Functions Demonstration of Math Functions: Shows how math functions might be used

    5. Drill on Math Functions Ceiling, Floor, and Round Drill on Math Functions Ceiling, Floor, and Round: Check your understanding of these functions

    6. ToString function instead of + to concatenate ToString function instead of + to concatenate: Compares using + and ToString()

    7. Parse: Convert text to Double Parse: Convert text to Double: A user does not always type a numeric value in text box,
      parse to get numeric value

    8. TryParse: Convert text to Double if Numeric TryParse: Convert text to Double if Numeric: TryParse to handle numeric error

    9. Ceiling: How many busses?: Real world problem to solve

    10. Random Numbers: Code to declare and use a random number

    11. Circle: Write a function to find area Circle: Write a function to find area: A function receives the radius and returns the area

    12. A Function to Generate Random Colors A Function to Generate Random Colors: A function to return a random color using Color.FromArgb(red, green, blue);

    13. A Function to Generate Random Colors A Function to Generate Random Colors: A function to return a random color using Color.FromArgb(red, green, blue);

    14. BMI: A Void Function to calculate Body Mass Index BMI: A Void Function to calculate Body Mass Index: A void function does not return a value

    Module 7: Boolean Expressions in C# +/-

    Be able to use Boolean Expressions using if/else Control Structures; Values in Controls: text box, check box, radio buttons; Conditional operator:?; and switch/case

    1. INDEX (start here)
    2. Microsoft Reference: Boolean expressions Microsoft Reference: Boolean expressions: C# if/else, conditional operators, OR ||, AND &&, switch/case

    3. Boolean Expressions: A Boolean expression is one that can be evaluated to true or false

    4. if Statements: An if statement executes a group of statements only if certain conditions are true

    5. Getting Numbers from Text Box: Numeric values in a text box must be parsed.

    6. A buffet restaurant part 1: Adding radio buttons A buffet restaurant part 1: Adding radio buttons: A restaurant charges different prices for breakfast, lunch and dinner, dicounts.

    7. A buffet restaurant part 2: Adding code for radio buttons and check boxes A buffet restaurant part 2: Adding code for radio buttons and check boxes: Radio buttons and check boxes have a Boolean property checked

    8. Rats in a maze Rats in a maze: A few problems to solve using && and ||

    9. Left or Right?: On mouse move determine if the mouse is on left or right of form

    10. Top or bottom?: On mouse move determine if the mouse is on top or bottom of form

    11. Adding the Mouse Move Event Adding the Mouse Move Event: Shows the X and Y positions when the mouse moves on the form

    12. Scrolling Scrolling: Scrolls a label across the form on the Timer event

    13. Letter Grade Letter Grade: Nested if/else to find letter grade

    14. Letter Grade Letter Grade: Nested if/else to find letter grade

    15. BMI: Body Mass Index BMI: Body Mass Index: Nested if/else to find BMI Risk

    16. BMI: Body Mass Index BMI: Body Mass Index: Nested if/else to find BMI Risk

    17. The conditional operator: ?: The ? can be used for very simple Boolean expressions in place of if/else code

    18. The conditional operator: ?: The ? can be used for very simple Boolean expressions in place of if/else code

    19. The conditional operator: ?: The ? can be used for very simple Boolean expressions in place of if/else code

    20. AND && And OR ||: && means AND, || means OR

    21. Drill on Boolean Drill on Boolean: Drill to test your understanding

    22. Middle or Side: Determine if mouse is on the side or middle

    23. Boolean Variables and Properties: Boolean values do not need to be compared to anything

    24. switch/case switch/case: switch/case to show season

    25. switch/case switch/case: switch/case to show season

    Module 8: Loops in C# +/-

    Loops: for, while, do; Endless loops; Nested loops; Arrays with for each

    1. INDEX (start here)
    2. References: loops References: loops: View the Microsoft information on loops in C#

    3. Prepare code for loop Prepare code for loop: Statements to add 1,2,3 to list box individually are converted to a while loop.

    4. A few notes about the while loop A few notes about the while loop: Cautions about possibility of endless loops and loops that execute 0 times.

    5. Sorted values: If the sorted property of the list box is true, the values are sorted as strings

    6. While, cont.: A while loop generates the powers of 2: 2, 4, 8, 16, 32, 64, 128, 512, 1024

    7. Position of increment in loop: The position of the increment changes the values and also what is added to list box

    8. Introducing the for loop Introducing the for loop: The while loop to add 1, 2, 3 to the listbox is changed to a for loop.

    9. Introducing the for loop Introducing the for loop: The while loop to add 1, 2, 3 to the listbox is changed to a for loop.

    10. Format of the for Loop: Explanation of the format of the for loop.

    11. Compare while and for Loops: Compares a for and a while loop that each generate the values 1, 2, 3, 4, 5.

    12. Compare while and for Loops: Compares a for and a while loop that each generate the values 1, 2, 3, 4, 5.

    13. Introduction to do Loops Introduction to do Loops: A do loop always executes at least once.

    14. Introduction to do Loops Introduction to do Loops: A do loop always executes at least once.

    15. do loops test condition at end, and execute 1 or more times : A do loop tests the condition at the end of the loop.

    16. Endless do loops: Shows a do loop that is endless because there is no increment of the variable.

    17. Endless do loops: Shows a do loop that is endless because there is no increment of the variable.

    18. Compare for, while, and do Loops: Each of the loops puts the numbers from 1 to 10 in a list box.

    19. Compare for, while, and do Loops: Each of the loops puts the numbers from 1 to 10 in a list box.

    20. Months and Seasons Months and Seasons: The switch/case block is embedded inside the loop

    21. Months and Seasons Months and Seasons: The switch/case block is embedded inside the loop

    22. Nested Loops: One loop can be inside another loop, or nested

    23. Nested Loops: One loop can be inside another loop, or nested

    24. Nested Loops: generate a deck of cards Nested Loops: generate a deck of cards: A loop for each suit with a nested loop for each card.
      Also uses switch.

    25. For Each Loops For Each Loops: The foreach loop works on a collection or an array.

    26. For Each Loops For Each Loops: The foreach loop works on a collection or an array.

    27. For Each Loops with a List For Each Loops with a List: Use the foreach loop to remove blank lines from a list box.

    28. For Each Loops with a List For Each Loops with a List: Use the foreach loop to remove blank lines from a list box.

  • Self Study Questions
  • Module 9: Arrays and Lists in C# +/-

    Understand Arrays, Lists, and Collections in C#

    1. INDEX (start here)
    2. Microsoft Reference: C# Arrays and Collections Microsoft Reference: C# Arrays and Collections: Read these white papers from Microsoft

    3. Introduction to Arrays Introduction to Arrays: An array is like a list. This shows how to declare and change values in an array

    4. Variables as index of Arrays: Finds the total of all values in an array using a loop

    5. Variables as index of Arrays: Finds the total of all values in an array using a loop

    6. Out of bounds: An exception: If we try to access an element beyond the last element there is a run time error.

    7. Out of bounds: An exception: If we try to access an element beyond the last element there is a run time error.

    8. Out of bounds: An exception: If we try to access an element beyond the last element there is a run time error.

    9. Use timer to cycle through colors in an array Use timer to cycle through colors in an array: Global parallel arrays are used for the color names and colors. Make sure the subscript is not out of bounds.

    10. Use timer to cycle through colors in an array Use timer to cycle through colors in an array: Global parallel arrays are used for the color names and colors. Make sure the subscript is not out of bounds.

    11. Colors: Add an array of strings to combo box: A global array of color names is created.
      In form load, a loop, adds the colors to combo box.

    12. Colors: Change the color to selected color in combo box: Uses parallel arrays: a string with color names and Color objects

    13. Colors: Change the color to selected color in combo box: Uses parallel arrays: a string with color names and Color objects

    14. Wishes Wishes: At timer event lables are given random font, color and text from arrays.

    15. Comparison of arrays and lists Comparison of arrays and lists: Examples of declaring arrays vs. lists

    16. Comparison of arrays and lists Comparison of arrays and lists: Examples of declaring arrays vs. lists

    17. Advice: An application with a List and Timer Advice: An application with a List and Timer: A list of good advice is randomly selected and scrolls across the form.

    18. Season: A special type of search Season: A special type of search: An array of first day of season is searched to find where the selected date belongs.

    Module 10: Dialogs in C# +/-

    Create Menus with Submenus; be able to use Toolstrip and Status Bar; Know and use the Common Dialog controls for Font, Color, Open, Save; Message Box

    1. INDEX (start here)
    2. Microsoft Reference Microsoft Reference: C# Dialogs, Common Dialogs and Message Boxes

    3. Happy Birthday Happy Birthday: This is an overview of the app that we will create in this lesson.

    4. Create the menu for the Happy Birthday App Create the menu for the Happy Birthday App: Shows how to build the menu for the Happy Birthday program.

    5. Create the tool strip for the Happy Birthday App Create the tool strip for the Happy Birthday App: Shows how to build the toolstrip (at the top of the form), plus two images to download

    6. Create the status strip for the Happy Birthday App Create the status strip for the Happy Birthday App: Create the status strip (at the bottom of the form) & add code to display the current time

    7. Add the font dialog for the Happy Birthday App Add the font dialog for the Happy Birthday App: The Font Dialog is one of the common dialog controls for selecting fonts and font properties

    8. modal: When a dialog is shown modal, nothing else happens until the dialog is closed.

    9. Adds the Open dialog to select a picture Adds the Open dialog to select a picture: Includes setting the filter to select only picture types.

    10. Add the Color dialog to select a background color for the Happy Birthday App Add the Color dialog to select a background color for the Happy Birthday App: Includes a different way to determine if the user clicked cancel

    11. Add the code for New and Exit: The last of the Happy Birthday App Add the code for New and Exit: The last of the Happy Birthday App: Shows how to save and restore the form settings at start

    12. A Message Box Example: Explanation of using a message box.

    13. Message Box Buttons and Icons: In addition to OK, and YesNO, other buttons can be shown

    Module 11: Forms in C# +/-

    Be able to add Forms, including About Box; Login Form; Custom Input Form

    1. INDEX (start here)
    2. Microsoft Reference Microsoft Reference: Forms

    3. Add an About Box to a program Add an About Box to a program: An About Box is one type of form that can be added to a program.

    4. About Box, part 2 About Box, part 2: Demonstrates the difference between Show() and ShowDialog()

    5. Custom Forms Custom Forms: In this video we create a custom form to get the days of the week the user can meet.

    6. Using the custom form.: A Form is shown modal, when closed selections are processed

    7. A second way of getting days. A second way of getting days.: Create a list of the days from the custom form

    8. Create a login form. Create a login form.: Close the program if the user and password are not correct

    9. Create a login form. Create a login form.: Close the program if the user and password are not correct

    10. Create a picture viewer to change size of picture. Create a picture viewer to change size of picture.: Add a form to display a picture.

    Module 12: Strings in C# +/-

    Work with Strings; Built-in functions; Validate input; Regular Expressions; Word Games

    printable lesson
    1. INDEX (start here)
    2. Microsoft Reference Microsoft Reference: Strings and String methods

    3. Parse: Parse means to break a string up into significant parts, or to convert a string to a numeric type.

    4. String Functions: Examples of the most used string methods

    5. String Length: Length gives the number of ALL characters in the String

    6. String Substr: Substr extracts a portion of a string

    7. Names: Using String Functions: Split name into first and last

    8. Escape Sequence: \ and @ Escape Sequence: \ and @: Using the \ and @ with strings with special characters

    9. String is valid Social Security Number.: A valid Social Security number has 9 digits

    10. Regular Expressions: A regular expression is a way to determine if a string matches a pattern.

    11. Regular Expressions for a sentence: A sentence must have letters of the alphabet and end with . ? or !

    12. Regular Expression for Robot: R2D2, C3P0?: Regex robotregx= new Regex(@"([A-Z][0-9]){2}");

    13. Regular Expression Drill: Test your understanding

    14. Scramble Array Algorithm Scramble Array Algorithm: Easy algorithm to scramble an array

    15. Scramble Array Algorithm Scramble Array Algorithm: Easy algorithm to scramble an array

    16. Scramble Array Algorithm Scramble Array Algorithm: Easy algorithm to scramble an array

    17. Anagram Demo: An anagram is simply the letters of the word scrambled

    18. Anagrams: Details for creating the form

    19. Cryptogram Demo: A cryptogram shuffles the alphabet to create a code

    20. Cryptograms: Details for creating the form

    21. Comparing strings: Download the PDF file

    22. @ is the verbatim identifier character.

    23. @ is the verbatim identifier character.

    Module 13: Files in C# +/-

    Read and Write Text Files; The Directory Command; Reformat a File; Copy and Paste using Clipboard

    1. INDEX (start here)
    2. Microsoft Reference: Microsoft Reference:: C# Files, The Directory Class

    3. Filter: The Filter is used with the file dialog to specify the types of files to be selected.

    4. Font: Font is a property that can be selected for controls and for drawing.

    5. Font Dialog: The Font Dialog is one of the common dialog controls for selecting fonts and font properties

    6. Clipboard program Clipboard program: Build the form

    7. Clipboard program Part 2 Clipboard program Part 2: Code: read file into list box, copy from listbox to clipboard

    8. Read a file line by line: Read in a CSV (comma separated values) file

    9. Write to text file: Write the contents of a combo box to a text file

    10. Reformat as XML: Read a text file and write as XML format.

    11. Directory: The Directory command is used to retrieve an array of all the files in a directory.

    12. Parameters for the Directory Method: Specify file type or pattern

    Module 14: A Picture Viewer Application +/-

    Use Dialog to Select folder or file; Open and Display a Picture; Use Timer to Create Slide Show

    1. INDEX (start here)
    2. Picture Viewer Part 1:: Set up the form

    3. Picture Viewer Part 2:: Code and instructions to view a picture

    4. Select a folder: When the user selects a folder all of the picture files
      in the folder will be displayed in a combo box

    5. User selects picture: Display the picture selected in the combo box

    6. Slide Show: Add a timer to create a slice show

    Module 16: Drawing in C# +/-

    Use drawing methods to create drawings and graphs

    1. INDEX (start here)
    2. Microsoft Reference: C# Drawing Graphics Microsoft Reference: C# Drawing Graphics

    3. Drawing Lines with the Pen

    4. Drawing Rectangles with the Brush

    5. Drawing Filled Circle with Border

    6. Drawing Filled Polygon with Border

    7. Drawing Strings

    8. Drawing Images

    9. Bezier Curves: A Bézier curve is a parametric curve used in computer graphics.

    10. Bezier Curves: A Bézier curve is a parametric curve used in computer graphics.

    11. Drawing Pie

    12. Drawing Pie with 2 Scroll Bars

    13. Draw pie graph for Budget

    14. About Graphs

    15. Bar Graph for Sales

    16. A drawing program with other brushes A drawing program with other brushes: A brush is used to draw solid shapes. A pen is used to draw outlines

    17. A drawing program with other brushes A drawing program with other brushes: A brush is used to draw solid shapes. A pen is used to draw outlines

    18. A drawing program with other brushes A drawing program with other brushes: A brush is used to draw solid shapes. A pen is used to draw outlines

    Module 19: Object oriented programming in C# +/-

    Be able to create and use Objects in C#

    1. INDEX (start here)
    2. Microsoft Reference: Classes vs. Structs Microsoft Reference: Classes vs. Structs: A class can be described as the blueprint or template for creating an instance of the class. An instance of the class is called an object. An object combines both data (properties) and events (procedures).

    3. Fraction Class with variables and constructors Fraction Class with variables and constructors

    4. Fraction Class with Accessors, Mutators, and < Fraction Class with Accessors, Mutators, and <

    5. Working with the DateTime class.

    6. A Person Class.

    7. An Employee Class derived from the Person Class (Inheritance).

    Module 20: Zebra Notes: Open, Save, Save As, and Format with a Rich Text Box +/-

    Work with a Rich Text Box; Format Text; Save changes; Remind to Save; Context menus to copy, cut and paste

    1. INDEX (start here)
    2. Microsoft Reference Microsoft Reference: The Rich Text Box Overview

    3. Build the Form Build the Form: Show all the controls and properties for this project

    4. Declare variables to track changes.: In order to remind the user to save, we use a Boolean variable changed

    5. Resize: Rich text box fills the form.: Richtext box fills the lower part of the form: between the toolstrip and the statusbar

    6. Display the date and time on timer tick.: Timer event: toolStripStatusDateTime.Text = System.DateTime.Now.ToString();

    7. Save the file as rtf.: Code for function to save the contents of the richtext box to file

    8. Do you want to save?: If there have been changes warn user when they select New, Open, or exit program.

    9. Remind to save before closing.: Code to cancel closing the form if user wants to save first.

    10. Remind to save before Opening new file.: Code to open a new file and load it asks about saving first

    11. Remind to save when selecting New.: On selecting new, first ask about saving (if changes), then clear text and filename

    12. Change font of selected text.: Uses the Font Dialog to select font

    13. Change color of selected text.: Uses the Color Dialog to select color of selected text

    14. Using clipboard to copy, cut and paste.: Code implements copy, cut and paste using the clipboard

    15. Using a context menu to copy, cut and paste. Using a context menu to copy, cut and paste.: A context menu is a pop-up menu that appears on right click

    16. Finishing touches: Finishing touches include adding icon, about box, and removing unused menu items

    Projects

    Glossary