Statusbar + Basics [HUGE] 07-05-2013, 11:35 PM
#1
NOTE:This tutorial is very detailed and it includes a lot of information for many basic things. If you don't really want to read them, just skip them
Ever wanted to make a status bar under your progressbar that shows some progress of your application? Well of you don't know how, here is an opportunity to learn.
I've seen a lot of different ways of doing it, but I personally think that this is the easiest method of doing this simple job.
We are going to use a short declaration of an array and simple looping to display the message in the status bar(label).
First let's learn how to declare an array with multiple strings in one line(for those that don't know how). Because I know that a lot of newbies will do this to declare an array:
But this is the method that you wanna use:
See what I did there, I saved 4 lines of code. I did this by making a unidentified string array and making it equal to the strings in the curly brackets separated by commas.
Now, lets learn how to call the elements of the array one after another. We can use a For Loop, we can use timer or we can use the value of a progressbar, trackbar and many different INTEGER values.
Using a For Loop:
Code:
Using a timer:
Using another value:
Keep in mind that this is most likely going to result in an error, because the source of the "outside" value is unpredictable, it might fail and this snippet will fail. But if you still want to use this then just add the code in a Try-Catch statement. Try-Catch statement is something like an if statement that checks if there is an error, if there is then do something below Catch. But if we leave it blank, it won't do anything, so we can use it for making error free applications(for those who don't know).
Here is an example of usage of this code for the snippet above:
And finally let's learn something about the Select statement. This statement are used instead of multiple if statements. If you for example need to do some action for each letter in the alphabet, you need to check if the variable is the desired letter with an if statement, that is a lot right. But with this statement you can set one variable to be checked, and then you can add multiple cases to be checked. Here is an example:
OK, now you probably have gained some knowledge of arrays, for loops, something about the Select statement and the Try statement. With this knowledge you are most likely going to understand what are we going to do now. In this example we are going to use a timer. Let's start:
[color=#1E90FF][size=x-large]Sorry for any errors or mistakes, if there are any![Sad Sad](https://sinister.ly/images/smilies/set/sad.png)
I hope you liked this tutorial.
By: Electroalek
![Smile Smile](https://sinister.ly/images/smilies/set/smile.png)
Ever wanted to make a status bar under your progressbar that shows some progress of your application? Well of you don't know how, here is an opportunity to learn.
I've seen a lot of different ways of doing it, but I personally think that this is the easiest method of doing this simple job.
We are going to use a short declaration of an array and simple looping to display the message in the status bar(label).
First let's learn how to declare an array with multiple strings in one line(for those that don't know how). Because I know that a lot of newbies will do this to declare an array:
Code:
Dim x(3) As String
x(0) = "Test"
x(1) = "LOL"
x(2) = "Hey"
x(3) = "etc..."
But this is the method that you wanna use:
Code:
Dim x() As String = {"Test", "LOL", "Hey", "etc..."}
Now, lets learn how to call the elements of the array one after another. We can use a For Loop, we can use timer or we can use the value of a progressbar, trackbar and many different INTEGER values.
Using a For Loop:
Code:
Code:
Dim x() As String = {"Test", "LOL", "Hey", "etc..."} 'First we declare the array with the method that I explained above
For y As Integer = 0 To x.Length - 1 'Then we start a For Loop that will go from 0 to the array's length - 1, you ask why, well because the array starts from 0 and that means that the length will always be plus one, that's why eliminate it.
MsgBox(x(y)) 'Now we call the strings in the array with the "id" of the loop.
Next 'And then we just end the loop
Code:
'This code is in a timer, with a variable that you want(doesn't matter)
'Declare an integer y outside of the timer, because if you declare it inside, it will always reset it self to 0 when the timer ticks.
' - Dim y As Integer - outside the timer sub
Dim x() As String = {"Test", "LOL", "Hey", "etc..."}' Again we declare the same array the same way
If y < status.Length - 1 Then
y += 1
End If
MsgBox(x(y))
Using another value:
Code:
'Imagine that y is the value that is the value that is set by completely other process.
Dim x() As String = {"Test", "LOL", "Hey", "etc..."} 'Again we declare the array
If y < status.Length - 1 Then 'We must make sure that we display the MessageBox only if the "outside" value is smaller than the arrays length. I explained above why I subtract 1 from the length.
MsgBox(x(y)) 'If the "outside" value is smaller than the arrays length then display the messagebox with it's respective "id"
End If
Keep in mind that this is most likely going to result in an error, because the source of the "outside" value is unpredictable, it might fail and this snippet will fail. But if you still want to use this then just add the code in a Try-Catch statement. Try-Catch statement is something like an if statement that checks if there is an error, if there is then do something below Catch. But if we leave it blank, it won't do anything, so we can use it for making error free applications(for those who don't know).
Here is an example of usage of this code for the snippet above:
Code:
'Read the above snippet for an explanation of this code
Try
Dim x() As String = {"Test", "LOL", "Hey", "etc..."}
If y < status.Length - 1 Then
MsgBox(x(y))
End If
Catch
End Try
And finally let's learn something about the Select statement. This statement are used instead of multiple if statements. If you for example need to do some action for each letter in the alphabet, you need to check if the variable is the desired letter with an if statement, that is a lot right. But with this statement you can set one variable to be checked, and then you can add multiple cases to be checked. Here is an example:
Code:
Dim x As String = "a"
Select Case(x) 'We set the main variable in the brackets
Case "a"
MsgBox("The variable is now at the letter a") 'These are the cases that I was talking about, see how easy it is to declare one
Case "b"
MsgBox("The variable is now at the letter b")
Case "c"
MsgBox("The variable is now at the letter c")
Case Else 'This is some what a special case, because if no other case from this statement occurs this will be executed, something like else in an if statement.
MsgBox("The variable was not a letter")
End Select
[color=#87CEEB]And this is how it would look like if you'd use if statements:[/color]
If x = "a" Then
MsgBox("The variable is now at the letter a")
End If
If x = "b" Then
MsgBox("The variable is now at the letter b")
End If
If x = "c" Then
MsgBox("The variable is now at the letter c")
End If
If x <> "a" Or "b" Or "c" Then 'This is the Case Else
MsgBox("The variable was not a letter")
End If
We need to check separately for each letter with a separate if statement, and look what happens when you want to use the Select Case. That's why the Select Case statement makes your life easier.
Code:
Dim count As Integer 'We declare a blank integer first outside the timer
Sub timer() 'This is the timer sub
Dim messages() As String = {"Status1", "Status2", "Status3", "Status4"} 'We declare an array with four messages
If count < messages.Length - 1 Then 'First we check if the count value is smaller that the length of the array
Label1.Text = messages(count) 'If yes, then put the first status on the label
ProgressBar1.Value = count 'We also can use the count value to set a progressbar's value
Else
count = 0 'You can add this code if you want the process to cycle again and again
End If
Select Case(count) 'This is optional, but you can use a Select Statement to do the actions respectively with the status.
Case 0
FirstAction() 'We just now call some subs
Case 1
SecondAction()
Case 2
ThirdAction()
Case 3
FourthAction()
End Select
End Sub
[color=#1E90FF][size=x-large]Sorry for any errors or mistakes, if there are any
![Sad Sad](https://sinister.ly/images/smilies/set/sad.png)
I hope you liked this tutorial.
By: Electroalek
![[Image: mU9i19G.gif]](http://i.imgur.com/mU9i19G.gif)