AllTheTimeWorld.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. Create your first Program Create your first Program: Step-by-step instructions for creating a C# windows form application

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

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

  6. 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

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

  8. 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.

  9. 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. List of Controls: A list of some of the most common controls, names and use

    Module 3: Writing Code in C# +/-

    Writing code in C#, Change Properties, 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. Code created Code created: An explanation of each line of code

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

    7. 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.

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

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

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

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

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

    13. 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 statment to declare a varaible

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

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

    11. 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. Microsoft Reference: C# Operators Microsoft Reference: C# Operators: C# Operators

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

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

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

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

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

    8. Order of operations: Explains how arithmetic expressions with more than 1 operator are evaluated

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

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

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

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

    13. Decimal values on scroll bars: Value on scroll bar are intege, how to select decimal values

    14. 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

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

    Module 6: 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. 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# +/-

    Boolean Expressions; if/else Control Structures; Values in Controls: text box, check box, radio buttons; Conditional operator:?; switch/case

    1. INDEX (start here)
    2. Microsoft Reference Microsoft Reference: 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. BMI: Body Mass Index BMI: Body Mass Index: Nested if/else to find BMI Risk

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

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

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

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

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

    20. 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. Format of the for Loop: Explanation of the format of the for loop.

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

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

    12. do Loops: A do loop tests the condition at the end of the loop.

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

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

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

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

    17. 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.

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

    19. 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 in C# +/-

    Understand Arrays, Lists, and Collections in C#

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

    3. Microsoft Reference: C# Collections Microsoft Reference: C# Collections: Read these white papers from Microsoft

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

    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. 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.

    8. 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.

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

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

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

    12. 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.

    13. 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# +/-

    Menus with Submenus; Toolstrip; Status Bar; 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 shown modal: Nothing else happens until the dialog closes.

    8. 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.

    9. 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

    10. 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

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

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

    Module 11: Forms in C# +/-

    Adding Forms: 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 is the user and password are not correct

    9. 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

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

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

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

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

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

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

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

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

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

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

    12. Regular Expression Drill: Test your understanding

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

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

    15. Anagrams: Details for creating the form

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

    17. Cryptograms: Details for creating the form

    18. Comparing strings: Download the PDF file

    Module 13: Files in C# +/-

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

    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 19: 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 menues 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