Handy Hints Semester 1, 2009

Note: If your Hint is more than a paragraph long, please put it on a separate page.

If you are creating a new page use the following naming convention
eg 2009 S1 Smith Mary HH 1(if you studied in 2009 semester 1 and your name is Mary Smith, Handy Hing 1)


Cardillo Anthony


Dougherty, Ben

Hint 1

Instead of using the different sort algorythm's you can now use the Sort method which will sort an array for you. If you have parrallel arrays
the first array passed to the method will be sorted and it will keep your pairing of data in tact.

Example:
        'declare arrays and variables
        Dim students() As String = {"Ben", "James", "Amy", "Ryan", "mark"}
        Dim ages() As String = {"20", "40", "10", "14", "60"}
        Dim msg As String = ""
 
        'loop through array creating our msg var
        For i As Integer = 0 To students.GetUpperBound(0)
            msg &= students(i) & " " & ages(i) & vbNewLine
        Next
 
        'display results
        MessageBox.Show(msg, "Results", MessageBoxButtons.OK, MessageBoxIcon.Information)
 
        'sort array by students names
        Array.Sort(students, ages)
 
        'reset msg var
        msg = ""
 
        'loop through again re-creating the msg var
        For i As Integer = 0 To students.GetUpperBound(0)
            msg &= students(i) & " " & ages(i) & vbNewLine
        Next
 
        'redisplay our sorted results
        MessageBox.Show(msg, "Results", MessageBoxButtons.OK, MessageBoxIcon.Information)
Hint 2

Ok so are you all sick of typing out all 4 parameters in a message box yet? When doing the sets I use an Ok and Information icon every time, so why not
make a smaller procedure as shown below.

    Sub mb(ByVal text as string, ByVal title as string)
 
        MessageBox.Show(text, title, MessageBoxButtons.OK, MessageBoxIcon.Information)
 
    End Sub
If you wanted to be even more lazy you could have a generic title as well so then all you would have to do would be mb("message Here")

You could also create a Sub for an error message box with the generic title "Error".

    Sub mbe(ByVal text As String)
 
        MessageBox.Show(text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
 
    End Sub

Hint 3

If you have ever built a form with say 10 textboxes on and when the form is submitted you want to validate all 10 textboxes, well naturally you would think that you have to go through each one like so

If txtTextBox1.Text = "" then
 
End If
 
If txtTextBox1.Text = "" then
 
End If
 
If txtTextBox1.Text = "" then
 
End If
 

etc.......

But this leads to messy and tedious code so below is an example on how to loop through controls and validate them on the way.

        For Each ctrl As Control In Me.Controls
            If ctrl.Text = "" Then
                MessageBox.Show("Invalid input for " & ctrl.Name)
                Exit Sub
            End If
        Next

Ok so this validates all the controls on the form which may not be what we are after so how about checking the type of the control inside the loop?

        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is TextBox Then
 
                If ctrl.Text = "" Then
                    MessageBox.Show("Invalid input for " & ctrl.Name)
                    Exit Sub
                End If
            End If
        Next

So now we know how to check different types of controls but say i have 6 Textfields that are string and then another 4 that are phone numbers or numeric values? Well if name the controls correctly i could check against this also like so.

        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is TextBox Then
                'check if it should be string
                If ctrl.Name.StartsWith("txtString") Then
                    'check if its valid value
                    If ctrl.Text = "" Then
                        'if its blank exit sub
                        MessageBox.Show("Invalid Input")
                        Exit Sub
                    End If
                    'check if it should be numeric
                ElseIf ctrl.Name.StartsWith("txtInt") Then
                    'validate that it is numeric
                    If Not IsNumeric(ctrl.Text) Then
                        'if not show error and exit sub
                        MessageBox.Show("Please enter Numeric Values for Phone Numbers")
                        Exit Sub
                    End If
                End If
 
            End If
        Next
You can see that for controls where we want string values entering, we name them txtString and for controls where we want numeric values we name them txtInt. This is only a simple example and you could take it further by looking at the rest of the control name say txtStringAddress which would allow you to give more friendly error messages specific to the correct field.

PS this is only some code i made up and if anyone else has their own validation snippets please share them.

Ben


Eldridge Jordan


Dennis Elliott - (Click for Control Dictionary / Conference / IT Topic Speech)

Handy Hint #1
Reversing a string
The following is a function which allows you to reverse the contents of a string:
*Note* This must be placed in a module.



Function ReverseString(ByVal msg As String) As String
 
        'Declared Variables Here
        Dim Chars() As Char = msg.ToCharArray 'This array is where the characters
        'from the original string will be inserted and reversed
 
        Dim result As String = "" 'Initialised result as 0 to prevent any errors in the for loop
 
        Array.Reverse(Chars) ' This reverse the characters in the array
        For i = 0 To Chars.GetUpperBound(0)
            result &= Chars(i) 'This places the characters in the array back into a string form
        Next
 
        'Declared the variable to be returned to be equal to the string "result"
        ReverseString = result
    End Function
 

Handy Hint #2
Searching for a string within a string.
The following code uses the function already within visual basic 2008 called InStr, Which is passed two strings and the function searches for the second string within the first one.


Dim msg As String
Dim search As String
Dim index As Integer
'Here I declared the string that will be searched. You can use an inputbox here if you wish
msg = "Optional. Specifies the starting position for each search."
msg &= " The search begins at the first character position by default. This parameter "
msg &= "is required if compare is specified"
 
'Here you ask the user what they want to search for.
search = InputBox("Enter the string would like to search for.", "Search", "default")
 
'Here the function InStr is passed. searching within variable "msg" for variable "search"
index = InStr(msg, search) 'The value returned will be an integer for the index
 
If index = 0 Then 'if InStr returned 0, it mean the string was not found
    MessageBox.Show("String not found.")
Else 'Any other result will be the index where the string found started (first found)
    MessageBox.Show("The string was found. it start from character index " & index & ".")
End If
 







Huege De Serville Leroy


Iwanyk Jean


Johnson Darren


Kerton Sean


Meharry Thomas
Click name to link to my page


Osti Tomislav

For Loop Yes or No



For i = 1 To 20 ' this loop goes 20 times
Result = MessageBox.Show("Do you watch or record anything from channel two?", "Channel two", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
If Result = MsgBoxResult.Yes Then
two += 1
End If
This for loop is used for when you get asked a question you get a yes or no question, if you press no then it will not accept the answer and skip the question but if you press yes then it will add it to the tally. So it is basically used for yes and no questions. It can also be altered to instead give the results for cancel or exit as well.

Lower Case

password = LCase(InputBox("What is the password?")) 'This asks for the password, it doesn't matter if it is lower or upper caps
While name <> password
name = LCase(InputBox("Enter the code", vbNewLine & "Enter the Code."))
This is a method so that if you are entering in a word that it will accept it if it is in lower caps or in upper caps. It can be used for many methods so that it can be used for if someone is typing in name that it will still accept the word.


Excel Formulas is the powerpoint

Compount interest is an example of using compound interests in excel

Excel Example is a mathematical equation using excel

Excel Examples1 is many examples using excel

Simple interest is an example of using Simple interests in excel


Passmore Samuel


Reed Karen
To get any numbers contained in a string you can use the Val Function. The Val function uses the following syntax:
Val(String)
The following code gets any numbers contained in String, and puts it into a textbox
Dim sNum As String
sNum = "Year: 2000, date: 12"
' Text1 will contain 200012
Text1.Text = Val(sNum)
 


Steele Andrew



Stevenson Richard


Thompson Daniel


Tweedie Terryn


Vosper Samuel


York Brock
Click my name to browse to my submissions page