Visual C# Tutorial.

Devin

Active Member
Joined
Aug 6, 2010
Location
Canada
First off, if you're already a C# programmer or a more advanced programmer, this tutorial is not for you.

Introduction
Programming C# 2010 is simple, and 1 step up from VB. A few things have changed and now require basic programming 'stops' such as "();" at the end of a statement and a ";" at the end of every line-break.

Including files and DOM/COM components
Including these COM and DOM (Domestic Operations Management) tools are the same as Visual Basic. Select "Project > Add Reference.." OR "Project > Add Service Reference". This is not the final step! You must include the references in your project code. For example, if I want a Windows Form in a console application (or just a form application), simply type at the top of your code structure, "using System.Windows.Forms;". More on that soon.

Creating Windows Forms to "Open" on demand
Right click your project in the right "Project Explorer", select "Add New.." > "Windows Form". Name your form to what you desire and go back to your target form "E.g. Form 1" and select where you want the form to open on demand, e.g. "Button1" that is on your form, double click it and add the following code:

Code:
Form2Name MyForm = new Form2Name();

Now, you have the form declared and ready to open on demand. Form2Name = the form you just created's name.

Opening on demand: To open on demand, add the following line(s) of code:

Code:
MyForm.Show();
(this will show the form and will allow you to click other objects on the previous form as well)

To have the form open and LOCK (disabling any other forms except for the current one), type the following:

Code:
MyForm.ShowDialog();
(this will LOCK the form from being unfocused)

"Message boxes"
In order to use these "Message boxes", you need to type the following (aka Msgbox("") in Visual Basic):

Code:
MessageBox.Show("MESSAGE","TITLE",MessageBoxButtons.OK,MessageBoxIcon.Stop);

Where MESSAGE = message to display on message box. and TITLE = title of message box.

Advanced: Catching exceptions
If you want your program to stop showing exception errors and quitting when the client does something unexpected, add the following line(s) of code:

Code:
try
{
YOUR CODE HERE TO TRY
}

catch (Exception 0285x129)
{
WHAT TO DO IF EXCEPTION IS CAUGHT
}

Updates
To set updates for your application, right click your project name in "Project Explorer" and go to "Publish". From there, select the update feature. This must be a server or a network.

Conversions
If you're coming from VB, C# doesn't do this for you. You may see an exception "Cannot be used as an integer or string". You can solve this by doing the following:

If I have a progress bar and I want to display its percentage, I'd do the following, because I cannot use "MessageBox.Show("The progress bar is at "+progressBar1.Value + "%");

Code:
string perc;
perc = Convert.ToString(progressBar1.Value);
MessageBox.Show("The progress bar is at "+perc+"%");

'If' and 'Else' statements
The IF and ELSE statements in C# are not the same as previous Visual Basic versions, you must add "Operands" and "Operators" such as "==" and "!=". I'll step you through it.

To detect if a text box equals "Hello World!", I'd type the following:

Code:
if (TextBoxName.Text == "Hello World!")
{
MessageBox.Show("HELLO TO YOU TOO!!!");
}

Else
{
MessageBox.Show("NO! You didn't say hello :(");
}

For more advanced, cleaner and more reliable/stable code, try the following:

Code:
string isWorld;
string isText;
isWorld = Convert.ToString(TextBoxName.Text);
isText = ("Hello World!");

if (isWorld == isText)
{
   MessageBox.Show("Hello to you too!!");
}

Else 
{
  MessageBox.Show("Oh, no hello  :(");
}

Remember: The "ELSE" function is used in sequence and MUST be used after the IF. If you use another "IF" right after that current "IF" and THEN an "ELSE" after the second "IF", it will ignore the second "IF" and use the ELSE for the first "IF". Confusing but just remember: ALWAYS USE AN ELSE after EVERY IF, that way you don't lose track of which else belongs to where -- Even if it has nothing in it!


I will post more soon, this is all I have for now :)



Here's an example right from a program I'm working on:

Code:
        private void playlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            recplay.Items.Add(opemed.FileName);
            error.Visible = false;
            Getall.recent = Getall.recent + 1;
            string post;
            post = Convert.ToString(Getall.recent);
            preferences.Default.recentcount = Getall.recent;
            preferences.Default.Save();
            label3.Text = ("Recently Played (" + preferences.Default.recentcount + ")");
            Getall.mintcount = 0;
            string playme;
            playme = Convert.ToString(playlist.SelectedItem);
            media.URL = (playme);
            ticker.Text = (media.currentMedia.name);
            this.Text = (media.currentMedia.name + " MediaLAN Player - v2.0.0");
            play.Visible = false;
            play.Enabled = true;
            pause.Visible = true;

            play.Enabled = true;
            // rcplay.Items.Add(opemed.FileName);
            duration.Enabled = true;
            lengthme.Enabled = true;
        }
 
Last edited by a moderator:
Back
Top