Sunday, March 27, 2011

1.1 If and Else Statements - SMTP program


If and Elseif

If you have ever programmed before, you will know what these are, if not, don't worry, you will soon understand.

You should be viewing the basic code of your program at the moment, including the message box we made earlier and the "Sub" for button1. Highlight the message box code ( MessageBox.Show("Hello World") ) And delete it.

In the sub for the button type this:


Code:
If TextBox1.Text = "Andy" = True Then
Click Return (Enter)
You should now be on a new line with an indent, type.
Code:
MessageBox.Show("Correct")

This tells the program that if the text (.Text) In text box1 (TextBox1) says "Andy" Then it should display a message box saying "Correct"

Now we need to tell it what to do if the user types something else.
Hit return again and type.
Code:
ElseIf TextBox1.Text = "Andy" = False Then
Hit(Enter)
Code:
MessageBox.Show("Wrong!")

An "End If" should also appear on it's own just before the end of the sub, this tells the program that we done with "If"s

Now do you understand what we have done? When the button is clicked first the program will check if the writing in the box says "Andy" if it does it will show a message box.

However "If" this is not true (Else If) Then it will check to see if it is false, in this case it will show the "Wrong!" Message box.

If you are feeling confident try changing the first bit to this.
Code:
()
            If TextBox1.Text = "Andy" Or "Skill" = True Then
Understand what I did there? I assigned two options to be true.



Email Program
Although If and ElseIf(s) are fun, are you ready to code something useful? Now we get to use some more advanced code, feel free to copy and paste this as not to make errors.

I would like to to save your form we have been using so far, and create a new one (Remember How?) Again a Windows Forms Application, and call this one Gmail. (If you don't have a Gmail account, sign up for one now (Googlemail will do too)

In your form add two text boxes, two labels and a button.

Take text box1 and put it at the top, and just above it put label1, change label1 to "Send To:"

Now take text box2 and put it bellow text box1 with enough room for label2 in between them. Put label2 between them and call it "E-Mail"

Now on text box2 hover over the top right corner and click the arrow, then check the box. You can now drag text box2 down to make it bigger, do so.

Put the button under all of this and call it "Send"

Any idea what we are doing yet? The text inside text box1 is who you want to send to, and the text inside text box2 is your message.

Now double click the button, click just before "Public Class Form1" and type.


Code:
()
Imports System.Net.Mail
Click return.

Remember those imports I talked about? That is one of them.

Now in the sub for button one put this in.
Code:
()

                Dim MyMailMessage As New MailMessage
        MyMailMessage.From = New MailAddress(Your Gmail)
                MyMailMessage.To.Add(TextBox1.Text)
        MyMailMessage.Subject = (The Alliance Mega Thread)
                MyMailMessage.Body = (TextBox2.Text)
                Dim SMTPServer As New SmtpClient("smtp.gmail.com")
                SMTPServer.Port = 587
        SMTPServer.Credentials = New System.Net.NetworkCredential(Your Gmail, Your Gmail Password)
                SMTPServer.EnableSsl = True
                SMTPServer.Send(MyMailMessage)

Now(don) 't panic, let me explain what this does.
Code:
Dim MyMailMessage As New MailMessage
This "Defines" the message.


Code:
MyMailMessage.From = New MailAddress(Your Gmail)
This is who the e-mail is from, type your Gmail in here, eg. MailAddress(Andy@gmail.com)


Code:
MyMailMessage.To.Add(TextBox1.Text)
This tells it to send the message to the e-mail written in Text Box1 (Much like in Ifs and ElseIfs, but sending mail not making a message box.)


Code:
MyMailMessage.Subject = (The Alliance Mega Thread)
This is the subject of the Email, if you wanted you could add a third text box and change that too.
 
Code:
MyMailMessage.Body = (TextBox2.Text)
This line tells the program the the "body" or main part of the e-mail is the text in Text Box2.


Code:
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
This defines the server it will be sent via, in this case Gmail (smtp.gmail.com)


Code:
SMTPServer.Port = 587
The port e-mails are normally sent through.


Code:
SMTPServer.Credentials = New System.Net.NetworkCredential(Your Gmail, Your Gmail Password)
Now this is the login information, enter your Gmail and your password, what it will do is automatically log into your account and send the mail.


Code:
SMTPServer.EnableSsl = True
This will enable SSL


Code:
SMTPServer.Send(MyMailMessage)
And lastly, and most importantly, this will send the message.

Well done, this is your first real program, save it, build it, test it (You can send Emails to yourself)

Saturday, March 26, 2011

1.0 Getting Started - Visual Basic - Creating your first form

Before we get started I would like to give some information on my background. I am currently a computer science student; just finishing up my second year. I code primarily in Java, PHP, HTML, Javascript, and SQL. These Tutorials will be using Visual Basic and the .NET framework because .NET is a native windows language and has no restrictions. Java runs in a "sandbox" so to speak, so registry files and big changes to the computer are very limited. That is why I will be doing mostly tutorials for VB .NET and if this catches on, I could whip up some Java/PHP/etc. tutorials, or even do some requests later on.

Alright, so here we go--

STEP 1: Getting VB Express 2010
Click on the first arrow, select the language and install.

STEP 2: Creating a form with simple functionality
Go ahead and open up VB Express. Select new project, and name is hello world


You should see a window appear inside VB called "Form1" This is the GUI of your program. Look to the left, here you will see your "Tool Box" It is full of every little tool you could wish for. For the sake of user friendliness, click to open the tool box, then right click and select "dock" as shown:



This will be used a lot and it is good to see all the functions we can add. You probably can guess what a lot of these already do.


Now to the bottom right you will see the "Properties Page" This IMHO is what makes VB so great, you don't have to define every parameter with code, You can simply change the settings using a drop down list! Above this you will see your "Solution Explorer" At the moment it will just show "My Project" and "Form1.Vb"

Now I want you to click once on the form, once it is selected go to the properties menu and look through it until you find "Text|Form1" This is the header of your form, change it to "My First Form!"

Next you need to look in the properties for "Back Color|Control" Click the drop down list and choose any colour you like (You can choose them from the other tabs within the list too!)

Once you have chosen a nice colour you are ready to create and run your first program. Go File, Save All. It should save to a directory similar to this.

C:\Users\andy\Documents\Visual Studio 2010 Express\Projects\

Click save. Now go to Build, Build Hello World, look to the bottom left, it should say "Build Successful"

You are almost done creating your first form, go to the directory you saved the project in, and then go:
Visual Studio 2010 Express\Projects\Hello World\bin\Release

Now run the .exe in the "Release" folder.

Well done, you have created and build your first form, easy huh.

STEP 3: A little bit of Code

Now it is time to make your form do something, I assume you still have VB open? If not go to:

C:\Users\andy\Documents\Visual Studio 2010 Express\Projects\Hello World"

And open the ".vbproj" File.

Now lets get started, double click your form, you should now see your first bit of code.


Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

A little bit confusing? Don't worry, I will explain it.


Code:
Public Class Form1
This will be at the top of all projects (Other than imports, but we will get to that later)
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
This is the "Sub" of your form (nasty functions can be placed in here making a program attack your computer once it is executed), and
Code:
End Sub
This the end of that sub, in between those two bits of code we tell the program what to do when "Form1_Load" Form1 loads.

Lastly.
Code:
End Class
This shows we are done with coding and that it is the end of the program.

Lets start adding some of our own stuff shall we? Click the space below Form1_Load and type (I advise you type, don't C&P)

Code:
MessageBox.Show("Hello World")

What this does it first tell the program we want a message box (MessageBox) Then tells it we want that box to be visible (.Show) Lastly we tell it what we want in the box ( ("Hello World") )

Now I want you to go File, Save All. Then Build, Build Hello World.

At the top you should see there are two tabs, Form1.vb and Form1.vb(Design) Go back to the design one.

Go back to the Release directory and run the .exe again, see what happens when "Form1_Load"s?
 Boom! we now have a windows application that runs a little bit of code we made.


This is all for the painfully noobie material. In the next tutorial we will look at adding code like: if and else statements, Windows STMP mailing (email anyone!), and textboxs and button functionality. (the emailing comes in handy if you want to email yourself .txt logs from our homemade keylogger >:D MOAR TO COME IN LESS THEN A WEEK... )