Getting basic with Visual Studio in C#

ddanbe 0 Tallied Votes 147 Views Share

Visual Studio is a great tool to use. But for beginners like me a few years back, it was a bit overwhelming and confusing. You got the feeling you lost all the control. VS has it’s special ways of doing things with partial classes and so on.
So lets get back in control (for a while) this has the drawback we have to do a lot of things for ourself. But it’s fun to do it once and in the future we will enjoy the benefits that VS offers even more.
So lets begin with a new project by clicking File->New Project in the dialog that opens click Empty Project and give it a name. Click OK. What you see now is a bit different from the usual Console- or Form Application. You should see practically nothing, emptyness all over!
So lets start filling that in. Rightclick your project in the solutions explorer and select Add New Item…In the dialog that appears select Code File and give it a name if you want to. Click the Add button. Paste the code snippet in or type it in. Or start with a very basic
public class MyWindow : Form
{
}
Run it and see what happens…Build it up line per line from here. Experiment like hell, it’s very instructive! Enjoy the learning. Don’t forget to fill in the References map with the necessary dll’s in the solution explorer by right clicking it and selecting Add Reference…, .

using System;
using System.Windows.Forms;
using System.Drawing;

public class MyWindow : Form    //derive a class MyWindow from a Form class
{
    Button MyButton = new Button();     //make a new button
    TextBox MyTextBox = new TextBox();  //make a new textbox

    public MyWindow() //constructor
    {
        //===================================================================
        //set some properties of this MyWindow form
        this.Text = "Just testing";
        this.BackColor = Color.DeepSkyBlue;

        //===================================================================
        //set location,size and text of the newly created button on the form
        MyButton.Location = new Point(100, 100);
        MyButton.Size = new Size(50, 50);
        MyButton.Text = "Do it";
        //hook in a routine to handle click events for MyButton
        MyButton.Click += new EventHandler(MyButton_Click);
        //add the control to the MyWindows forms control collection
        this.Controls.Add(MyButton);

        //===================================================================
        //set location,size and text of the newly created textbox on the form
        MyTextBox.Location = new Point(10, 10);
        MyTextBox.Size = new Size(150, 20);
        MyTextBox.Text = "";
        //add the control to the MyWindows forms control collection
        this.Controls.Add(MyTextBox);

        //===================================================================
    }

    private void MyButton_Click(object Obj, EventArgs EvA)
    {
        //aarghh the ultimate
        //by clicking on our button, set a text in a textbox
        this.MyTextBox.Text = "Hello world!";
    }

    private static void Main()
    {
        Application.Run(new MyWindow());    //start the application
    }
}