[TuT] (C#) Introduction to C# Programming Part 2

This is a discussion on [TuT] (C#) Introduction to C# Programming Part 2 within the Development forum part of the Software/Hardware category; Welcome to Wynthar's C# Tutorial Series Tutorial 2: More Windows Forms Features Introduction So in the Last Tutorial we downloaded ...


+ Reply to Thread
Results 1 to 6 of 6

Thread: [TuT] (C#) Introduction to C# Programming Part 2

  1. #1
    Wynthar's Avatar
    Wynthar is offline Advanced Hacker
    Wynthar has a spectacular aura about Wynthar has a spectacular aura about Wynthar has a spectacular aura about
    Join Date
    Feb 2009
    Location
    So Cal, USA
    Posts
    276
    Downloads
    0
    Uploads
    0
    Rep Power
    3
    Reputation
    235

    [TuT] (C#) Introduction to C# Programming Part 2

    Welcome to Wynthar's C# Tutorial Series
    Tutorial 2: More Windows Forms Features

    Introduction
    So in the Last Tutorial we downloaded Microsoft Visual C# Express Edition and created our first windows forms application. I hope you have at least tried to play around with the code we used. Experiment, change the string. See the results. If you have not managed to make enough changes that the program does not compile, you are not experimenting enough.

    Let's Begin
    When creating forms, I usually find that it's easiest to develop the user interface the way I want it to work first, then program in the functionality for all of the forms objects. So, let's add a pair of buttons, a check box, a text box and see what we can do with these. So, let's create our Tutorial 2 project.

    Go to File->New Project... Select Windows Forms Application. Set the Name to Tutorial2 and click OK.



    Next
    In the tool box, click on the [+] Common Controls to expand that menu. And click on Button.

    Now go over to your form, and click in the middle of your form. It should create a button for you.

    Repeat this process once more to create two buttons.



    Next
    Now, scroll down in the tool box and select TextBox. Click somewhere to the right of your first button.



    Next
    I had to scroll up a little to find CheckBox. Click on CheckBox and click on our form to the right of the second button.



    Next
    Let's add one more item to our form. Click on Label and add a label someplace below the other objects we already have.



    Next
    Excellent. Our user interface for this tutorial is now complete.

    Now, let's add some functionality to the program.

    First, let's add some functionality to button1. So, double click on button1.

    Enter the following into the function where the cursor is.

    Code:
    private void button1_Click(object sender, EventArgs e)
    {
    	//This takes the value out of the textBox1
    	//And changes the form's title to the value
    	//Of the text box.
    	string newWindowTitle = "Title: " + textBox1.Text;
    	this.Text = newWindowTitle;
    }
    
    Code Explaination

    So. I'll explain this code a bit. The forward slashes are comments. Comments are not read by the compiler. They are useful for making your code easier to read. It can also be helpful to add in reminders to yourself about what is happening in your code.

    I created a new variable of type "string" with a variable name of "newWindowTitle" and I set it equal to the string "Title: " then I attached the text from textBox1 to the end of the string.

    For example, if someone types "Hello" into the text box, then our string newWindowTitle will equal "Title: " + "Hello", which is added together and results in a single string, "Title: Hello"

    The final line of our function sets the current form's title to our string variable newWindowTitle. The keyword "this" refers to whichever object we are currently in. Since, our code currently resides in Form1, "this" refers to Form1.

    I have found out that the Form1.Text refers to the form's title. Therefore, when we set this.Text = newWindowTitle; we will be changing the form's title to "Title: Whatever Is In The Text Box"



    Next
    Okay, so now go back to the tab Form1.cs [Design] then double click on button2 to create a method stub for button2. Enter the following code.

    Code:
    private void button2_Click(object sender, EventArgs e)
    {
        //This button changes the text on the label
        //based on check box value.
        if (checkBox1.Checked == true)
        {
            label1.Text = "Check box is checked!";
        }
        else
        {
            label1.Text = "You didn't check the checkbox.";
        }
    }
    
    Code Explaination

    Okay. Another fun feature of programming. The If/Else statement. It should be pretty logical. We check here if our checkBox1 is checked by accessing the checkBox1.Checked variable and seeing if it equals (==) true.

    If it does equal true, that means it is checked. Then we go execute the code between the braces. This code for the true case reads label1.Text = "Check box is checked!" Similar to setting the text of the form title, this will set the text for the label to our string.



    Next
    So, now press the green arrow again to compile and run. (Or hotkey F5)



    Finally
    If everything is correct you should be able to test out your form and the buttons. Here is a sample result that I came up with by playing with the form.




    [Next Time]
    • We will go over Form Object Properties and how to set them.
    • Once again we will explore and practice more coding examples.

    [Later]
    • If I get good feedback, I will continue these tutorials all the way up to making a simple map hack using some common offsets.
    • Please feel free to post your feedback, comments, questions, and other things you might like to see.
    • Thanks for reading guys!

    [C# Tutorials]
    Last edited by Wynthar; 04-15-2009 at 05:55 PM.

  2. The Following 4 Users Say Thank You to Wynthar For This Useful Post:


  3. #2
    Morphex's Avatar
    Morphex is offline Mentor
    Morphex is on a distinguished road
    Join Date
    Dec 2008
    Location
    Norway, Ski
    Posts
    132
    Downloads
    0
    Uploads
    0
    Rep Power
    2
    Reputation
    39
    Really nice tut for beginners ^^
    Visit us at morphex-gaming.ucoz.com

  4. #3
    jWindz is offline Newbie
    jWindz is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    1
    Downloads
    0
    Uploads
    0
    Rep Power
    1
    Reputation
    0
    Very helpful.
    So detaild tutorial, and with images. Good job!

  5. #4
    wtfdudes's Avatar
    wtfdudes is offline Hacker
    wtfdudes will become famous soon enough
    Join Date
    Dec 2008
    Location
    Austria
    Posts
    192
    Downloads
    4
    Uploads
    0
    Rep Power
    2
    Reputation
    90
    Yeah, very helpful indeed.

    Just skimmed over your tuto and as far as I can see it's plain and correct.
    Definitely worth a .


  6. #5
    dog_keeper's Avatar
    dog_keeper is offline Teh Sexy One.

    dog_keeper has much to be proud of dog_keeper has much to be proud of dog_keeper has much to be proud of dog_keeper has much to be proud of dog_keeper has much to be proud of dog_keeper has much to be proud of dog_keeper has much to be proud of dog_keeper has much to be proud of dog_keeper has much to be proud of
    Join Date
    Dec 2007
    Location
    The one and only person who lives far far away from all the people here on d3scene!
    Posts
    2,306
    Downloads
    17
    Uploads
    5
    Rep Power
    9
    Reputation
    1147
    thanks. Really nice =]
    http://i291.photobucket.com/albums/ll304/dog_keeper/dog_keeper.jpg

  7. #6
    risker is offline Banned User
    risker is a splendid one to behold risker is a splendid one to behold risker is a splendid one to behold risker is a splendid one to behold risker is a splendid one to behold risker is a splendid one to behold
    Join Date
    Oct 2008
    Location
    Australia
    Posts
    2,852
    Downloads
    2
    Uploads
    2
    Rep Power
    0
    Reputation
    672
    ;D
    Finished 2#
    Going for 3#

    +Rep

+ Reply to Thread

Similar Threads

  1. [TuT] (C#) Introduction to C# Programming Part 1
    By Wynthar in forum Development
    Replies: 6
    Last Post: 05-06-2009, 04:41 PM
  2. Evolution of Viruses
    By risker in forum Internet Guides
    Replies: 13
    Last Post: 12-29-2008, 06:51 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Dboyz Interactive Skillhackers