2: INTRODUCTION TO VISUAL C#

2.1 Getting Started with Forms and Controls 2.2 Creating the GUI for Your First Visual C# Application 2.3 Introduction to C# Code 2.4 Writing Code for the Hello World Application 2.5 Label Control 2.6 Making Sense of IntelliSense 2.7 PictureBox Controls 2.8 Comments, Blank Lines, and Indentation 2.9 Writing the Code to Close an Application's Form 2.10 Dealing with Syntax Error

2.1 GETTING STARTED WITH FORMS AND CONTROLS

A Visual C# application project starts with creating its GUI with Designer Toolbox Property window In the Designer, an empty form is automatically created Form's Bounding Box and Sizing Handles The Property Window Changing a Property's Value Adding Controls to a Form Rules for Naming Controls Create a C# Windows Program

2.2 CREATING THE GUI FOR YOUR FIRST VISUAL C# APPLICATION

Components: a Form and a Button control Purpose: Create the application's GUI Form, Button, TextBox, Label, PictureBox   Write the code that causes "Hello World" to appear when the user clicks the button?
         private void btnDoIt_Click(object sender, EventArgs e)
         {
         
         
            ??
         
         
         }  
         
         

2.3 INTRODUCTION TO C# CODE

C# code is primarily organized in three ways: Namespace: a container that holds classes Class: a container that holds methods Method: a group of one or more programming statements that perform some operations A file that contains program code is called a source code file Source Codes in the Solution Explorer Program.cs file: contains the application's start-up code to be executed when the application runs Form1.cs contains code that is associated with the Form1 form A sample of Form1.cs: The using directives indicate which namespaces of .NET Framework this program will use. C# code is organized as methods, which are contained inside classes, which are contained inside namespaces Multiple Buttons (Events, MessageBox)   Adding Your Code Message Boxes (aka dialog box) displays a message
            private void firstButton_Click(object sender, EventArgs e)
            {
               MessageBox.Show("You clicked the first button.");
            }     
            
            

2.4 WRITING CODE FOR THE HELLO WORLD APPLICATION

   using System;
   using System.Windows.Forms; // right click/organize usings/remove unused

   namespace Hello_World
   {
       public partial class Form1 : Form
       {
           public Form1()
           {
               InitializeComponent();
           }

           private void myButton_Click(object sender, EventArgs e)
           {
               MessageBox.Show("Thanks for clicking the button!");
           }
                       // OR...
           
           private void myButton_Click(object sender, EventArgs e)
           {
               MessageBox.Show("Thanks for " + "clicking the button!");
           }
                       // OR...
                        
           private void myButton_Click(object sender, EventArgs e)
           {
               MessageBox.Show("Thanks" + " " + "for" + " " + "clicking" + " " + "the button!");
           }           
       }
   }
   
  

2.5 LABEL CONTROLS

A Label control displays text on a form and can be used to display unchanging text or program output Properties: Text: gets or sets the text associated with Label control Name: gets or sets the name of Label control Font: allows you to set the font, font style, and font size BorderStyle: allows you to display a border around the control's text AutoSize: controls the way they can be resized TextAlign: set the text alignments Using Code to Display Output in a Label Control
      private void showAnswerButton_Click(object sender, EventArgs e)
      {
         answerLabel.Text = "Theodore Roosevelt";
      }  
      
      
Exit & Clear: PictureBox, ListBox  

2.6 MAKING SENSE OF INTELLISENSE

IntelliSense provides automatic code completion as you write programming statements Use with caution!!!

2.7 PICTUREBOX CONTROLS

A PictureBox control displays a graphic image on a form Properties: Image: specifies the image that it will display SizeMode: specifies how the control's image is to be displayed Visible: determines whether the control is visible on the form at run time Exit & Clear: PictureBox, ListBox  

2.8 COMMENTS, BLANK LINKS, AND INDENTATION

Comments are brief notes that are placed in a program's source code to explain how parts of the program work A line comment appears on one line in a program
      // Make the image of the back visible
      cardBackPictureBox.Visible = true;
      
  
A block comment can occupy multiple consecutive lines in a program
      /*
      Line one
         
      Line two
      */  
      
      
Use Blank Lines Indent for Readability Example: Guessing Game  The Fundamental Theorem of Readability (Art of Readable Code, O'Reilly, 2011) Code should be written to minimize the time it would take for someone else to understand it. Create Readable Code

2.9 WRITING THE CODE TO CLOSE AN APPLICATION'S FORM

To close an application's form in code, use the following statement:
      private void exitButton_Click(object sender, EventArgs e)
      {
        // Close the form.
        this.Close();
      } 

      

2.10 DEALING WITH SYNTAX ERRORS

Visual Studio code editor examines each statement as you type it and reports any syntax errors that are found If a syntax error is found, it is underlined with a jagged line If a syntax error exists and you attempt to compile and execute, you will see an error window YOU MUST FIX ALL SYNTAX ERRORS BEFORE PROCEEDING!!

2.X Changing Properties at "Run-Time", "Hover" Event, MessageBox

Run-time Properties Change, MessageBox