1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[PART I] xxf8xx's Intro to Custom Coding Web Bots in C#

Discussion in 'C, C++, C#' started by xxf8xx, Nov 8, 2012.

  1. xxf8xx

    xxf8xx Supreme Member

    Joined:
    Nov 30, 2009
    Messages:
    1,321
    Likes Received:
    596
    Occupation:
    IM
    First off, you guys should know I am in no way an expert coder or anything like that. I am simply trying to share my knowledge, and get some more people into coding. You'll have to realize that coding can be very boring at times, especially the learning process, it can however, be very fun and very rewarding if you stick with it. I really hope some people do learn a few things here and decide to stick with it. Let's resurrect the coding section of BHW!

    I realize there are a lot of people out there that are looking to learn how to code their own simple bots. I'm not going to lie here, if you want to go this route it's going to take a lot more than just reading what I put up here. It's going to take a lot of dedication and a lot of research and long hours from you. Anyway, this first part is not even going to touch base with bot programming. Instead, I'm going to try to explain some VERY basic programming concepts, and get you making your first "Hello World" application. Hello World is always the first application which you will make when learning a new programming language, which in this case, is C#.

    Anywho, I want to keep this as exciting as possible, so I'll explain everything as we go.


    First things first:

    You will need a few things before we get started. Firstly, we will be coding in Windows. Have a Windows machine ready to go. Secondly we will need to install the newest version of the .net framework (simple Google search). Lastly, you will be needing a copy of Microsoft Visual Studio 2010 (you can use something else, but this is what I use). Keep in mind VS2010 is NOT free, so you will have to go ahead and get a torrent of it. I also realize 2012 is out, but to avoid problems and for me to help you best I highly recommend 2010. I will only help people who have 2010 installed as I cannot predict certain things going wrong with 2012.

    Moving forward...

    HURRAY! If you made it this far you hopefully have the newest version of the .NET Framework installed as well as Visual Studio 2010! Congratulations, you can code your first application now!
    As mentioned, we're going to be coding a simple program called Hello World, which all it will do, is display the words "Hello World!".
    First off I just want to mention that you should always run VS2010 as administrator to avoid security conflicts.

    Anyway...

    Start by creating a new C# Solution as follows:
    Click File -> New -> Project...
    Then on the left side locate Visual C# and click on "Windows Forms Application". Then type in the name of the program under "Name:". Call yours Hello World to find it easier later.

    New_CS_Solutions.png

    Click OK!
    What you will see now is what your app is about to look like. You will notice some tools on the left side which are controls you can add to your program. The ones we are mostly concerned about are:
    "Label" - Simply display some text
    "TextBox" - Used to enter text into
    "Button" - Used to start/stop a predefined task of some kind

    Start by dragging a button and a label onto your app. It should look something like this:

    button_and_label.png

    Now let's make some changes to these objects. Single click on button1. If you accidently double click, just click the design tab at the top of VS to get back. Then in the properties window on the right, look for "Text" and change it's value from "button1" to "Go1" or something similar. Now look for (Name). Change it's value to "btnGo". btn Simply stands for button. It is to make our code look cleaner and easier to tell what things are.

    If you'd like, you can go in and change the text and name of label1, however, I will be skipping this portion. It follows the same steps as the button.

    Now we're ready to actually CODE something. Double click on your button. This will bring you to the code screen. It will be overwhelming at first, so let me try to explain a few things.

    first_code.png

    First off, if you don't have line numbers turned on, Google how to turn them on, you'll need them.
    Let's explain a few things here. Everything that starts with the word using is simply importing some commands into your program allowing you to code certain instructions. All of these default imports are needed for very basic instructions, or even for your program to be created.
    Now we'll look at namespace. The word after here should be what you named your program. I accidently didn't name mine so it's the default name. The namespace is simply a container which holds all of your items inside of it.
    Notice the curly braces. Many times while you are coding something, all of the logic needs to be grouped together. This is where you will need curly braces. Whatever code is inside these braces can only be seen from within these braces. You'll learn more on this as we go.
    Public partial class. This is going to hold everything within a single window of your application. In this case, our window is still named Form1.
    The next bit is simple creating our window to be displayed to the screen.
    Now let's get to private void.... We're going to skip what all of this stuff means for now. All you need to know right now is that this is your button's click event handler. Whatever you code in here will happen when you click the button. Crazy huh!?
    Start out by clicking inside the brackets here and type
    string strHello = "Hello World!";

    Now we will need to explain variables. A variable is simply something you want to store in memory inside your program. In this case we are using a string, which is used to store plain text. The word after, strHello, is what I am naming my variable, and everything INSIDE the quotes is what the text will actually be. At the end of the line we use a semi-colon to signify it is the end of our line of code.

    Also, notice how I started the name of my variable with str. This is to signify it is a string to again making our lives easier later on.

    Now let's go ahead and display this text in our label! Type the following:
    label1.Text = strHello;

    label1 is the name of your label. By typing .Text after, you are changing it's Text property, just like you would from manual editor. Then we just type an ='s with our variable name to set it to what's inside our variable.

    Pretty simple stuff right? Hit the big green play button at the top. This is the debug button. Hopefully after clicking this your program will load up, and when you click the button, you will see "Hello World!", or in my case...
    hello_bhw.png


    Hopefully this was a decent introduction to programming for you. But don't stop here! This was just a basic (and I mean it when I say basic) tutorial. Go google some things about basic programming. Go and learn!

    In the next portion of this tutorial we will go over how to download a webpage's source code. Yes, it will be a lot to take in, so get yourselves ready and do some research. I highly recommend looking into basic programming terms and concepts such as classes, methods, variables, etc...

    Cheers,
    xxf8xx
     
    • Thanks Thanks x 4
    Last edited: Nov 8, 2012