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
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
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
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
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
Code:
MyMailMessage.From = New MailAddress(Your Gmail)
Code:
MyMailMessage.To.Add(TextBox1.Text)
Code:
MyMailMessage.Subject = (The Alliance Mega Thread)
Code:
MyMailMessage.Body = (TextBox2.Text)
Code:
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
Code:
SMTPServer.Port = 587
Code:
SMTPServer.Credentials = New System.Net.NetworkCredential(Your Gmail, Your Gmail Password)
Code:
SMTPServer.EnableSsl = True
Code:
SMTPServer.Send(MyMailMessage)
Well done, this is your first real program, save it, build it, test it (You can send Emails to yourself)
Woah great tutorial. I will be watching this blog.
ReplyDeleteexcellent tutorial, easy to understand
ReplyDeleteuseful stuff!
ReplyDeleteReally good tutorial man. I am surprised that you could make it so simple. Will be referring to here a lot now. Great blog. Good luck with future success ! Also, followed.
ReplyDeletenever programmed, something I've been interested in.
ReplyDeleteKeep up the good stuff...maybe I'll actually take the time to learn something :)
cool man thanks!
ReplyDeletethanks for the protip!
ReplyDeleteif/else is the basic in programming.. not that difficult to understande you try
ReplyDeleteAre you going to be doing C++ tutorials? :)
ReplyDeletegreat post. following you
ReplyDeleteExcellent post man! Was a really interesting read! thanks for sharing!
ReplyDelete