[TUT] If Statement
The if statement is a really useful statement which allows the instruction for the program to do something if something else happens. For example, when you press a button, you can tell the program that if the "red program" option checkbox is checked, to make the program red. We'll build on this throughout the tutorial.
The example I meantioned above can be coded out as follows:
Breakdown time! Alright, so you should already know the "Private Sub Command1_Click" line and the "End Sub" line. The first part of out statement is really simple. It's telling your application that if the checkbox check1's value is 1 (checked), to turn the backcolor of form1 to red (visual basic has built-in main colors, so like red, green, blue, black, white, etc. They come in the form of "vb" and the color name.). With the if statement, we need to close it off so that the rest of the code isn't ifified. That's where the "end if" comes in.
So sure, this is simple and can't really do much. So, let's add an "Else" line in, so that when Command1 is clicked, if Check1 isn't checked than the form's background color will be blue instead of red. We can do that like this:
Breaking down time again! Alright so I guess I just have to explain the "Else" line. The Else line allows an instruction that if check1's value isn't 1, to do something else instead. Putting it into speech, it'd be like this: If Check1's value is 1, then make Form1's backcolor red. Otherwise, make it blue.
This is the last esential thing you need to know about the if statement: the ElseIf line. It's extremely basic. What it is is putting 2 if statements together to avoid having too many if statements all over. Change your checkboxes to radio buttons, so we can use the ElseIf line easier. Add 2 radio buttons, one for red and one for blue. Keep your usual Command1. Here's the code for the example we had above, except with radio buttons and the ElseIf line:
Now, when Command1 is clicked, Form1's backcolor is changed to red if option1's value is true. But, if option2's value is true, then the form's backcolor will be blue.
-----
Tutorial brought to you by Blupig
If (lol jokes) you liked my tut, please give rep if it's available (I'm posting this on several forums
)
The if statement is a really useful statement which allows the instruction for the program to do something if something else happens. For example, when you press a button, you can tell the program that if the "red program" option checkbox is checked, to make the program red. We'll build on this throughout the tutorial.
The example I meantioned above can be coded out as follows:
Code:
Private Sub Command1_Click
If Check1.Value = 1 Then
Form1.Backcolor = vbRed
End If
End Sub
So sure, this is simple and can't really do much. So, let's add an "Else" line in, so that when Command1 is clicked, if Check1 isn't checked than the form's background color will be blue instead of red. We can do that like this:
Code:
Private Sub Command1_Click
If Check1.Value = 1 Then
Form1.Backcolor = vbRed
Else
Form1.Backcolor = vbBlue
End If
End Sub
This is the last esential thing you need to know about the if statement: the ElseIf line. It's extremely basic. What it is is putting 2 if statements together to avoid having too many if statements all over. Change your checkboxes to radio buttons, so we can use the ElseIf line easier. Add 2 radio buttons, one for red and one for blue. Keep your usual Command1. Here's the code for the example we had above, except with radio buttons and the ElseIf line:
Code:
Private Sub Command1_Click
If Option1.Value = True Then
Form1.Backcolor = vbRed
ElseIf Option2.Value = True Then
Form1.Backcolor = vbBlue
End If
End Sub
-----
Tutorial brought to you by Blupig
If (lol jokes) you liked my tut, please give rep if it's available (I'm posting this on several forums

















