Microsoft C# Programming


  1. Check the Microsoft C# Help Pages.

  2. Download and Install Visual Studio Community.
  3. Visual Studio Community is a free, fully-featured, and extensible IDE (Integrated Development Environment) for creating modern applications for Windows, Android, and iOS, as well as web applications and cloud services. Visual Studio Community is free for individual developers, open source projects, academic research, and education.


  4. Start the Visual Studio Community.
  5. Start the Visual Studio Community by selecting the following Windows options:
       Start ⇒ All Programs
             ⇒ Visual Studio 2019
    If you want to open an existing project, pick the project with the .sln (solution) file extension as follows. Otherwise, go to the next step.


  6. Create a New Project.
  7. Follow the next steps:

    1. Pick the option “Create a new project.”


    2. Enter the options:

      • Language: C#,
      • Platform: Windows, and
      • Project: Desktop.


    3. Configure the project by entering the following information:

      • Project name such as WindowsFormsApp1,
      • Location such as C:\C#-workspace, and
      • Checking “Place solution and project in the same directory.”


    4. Visual Studio IDE (Integrated Development Environment) will be displayed as followss after clicking the button Create:


  8. Build the User Interface of the Form Form1.cs [Design].
  9. Visual Studio provides a WYSIWYG (What You See Is What You Get), drag-and-drop user interface. Create a user interface such as


    by using the panes of “All Windows Forms” of “Toolbox” and “Properties.” This user interface includes


    For how to create a Visual Studio user interface, check Creating the Visual Look of Your Program: Introduction to Windows Forms.

  10. Implement the Form Form1.cs.
  11. Write the events’ procedures, for example, by double clicking the button “Submit”


    C:\C#-workspace\WindowsFormsApp1\Form1.cs
     using System;
     using System.Windows.Forms;
    
     namespace WindowsFormsApp1 {
       public partial class Form1 : Form {
         public Form1( ) {
           InitializeComponent();
           }
         public static string sendtext = "";
         private void Button1_Click( object sender, EventArgs e ) {
           sendtext = textBox1.Text;
           Form2 frm = new Form2( );
           frm.Show( );
           this.Visible = false;
         }
         private void Button2_Click( object sender, EventArgs e ) {
           Application.Exit( );
         }
       }
     }

    where the method InitializeComponent loads the compiled page of a component. Just leave it there. This program is to send the entered name to the form, Form2, which displays the name. For how to write a C# program, check C# Tutorial.

  12. Create Another Form Form2.cs [Design].
  13. Select the following options to create another form:
         Project ⇒ Add Windows Form...

  14. Build the User Interface of the Form Form2.cs [Design].


  15. This user interface includes


  16. Implement the Form Form2.cs.
  17. Write the events’ procedures, for example, by double clicking the button “Home”


    C:\C#-workspace\WindowsFormsApp1\Form2.cs
     using System;
     using System.Windows.Forms;
    
     namespace WindowsFormsApp1 {
       public partial class Form2 : Form {
         public Form2( ) {
           InitializeComponent( );
         }
         private void Form2_Load( object sender, EventArgs e ) {
           label1.Text = "Welcome to C#, " + Form1.sendtext;
         }
         private void Button1_Click( object sender, EventArgs e ) {
           this.Visible = false;
           Form1 frm = new Form1( );
           frm.Show( );
         }
       }
    }

    It displays the name entered in the Form1.
    Note that the Page_Load( ) or Form2_Load( ) method may not work if it is entered manually. Instead, double click the form of the design view and use the template generated by it.
  18. Build the Project.
  19. Build the project by selecting the following VS options:
       Build ⇒ Build Solution

    If you encounter an error like
    Unable to copy file "obj\Release\WindowsFormsApp4.exe" to "bin\Release\WindowsFormsApp4.exe". The process cannot access the file 'bin\Release\WindowsFormsApp1.exe' because it is being used by another process.WindowsFormsApp1”
    most likely because the previous execution did not exit formally. To solve this problem, use the Windows Task Manager (Ctrl+Alt+Delete) to end the background processes.

  20. Execute the Project.
  21. Execute the project by selecting the following VS options:
       Debug ⇒ Start Without Debugging
    One example of execution results is shown as follows:

    Submit ⇒

    ⇐ Home