VB Handy Hints 2007

Semester 1


Shane Burgoyne

MSGBOX
If your using a messagebox.show thingy and your just using it while your testing something or whatever, and do not need anything special, instead just use:

Code:
msgbox("message")

Its alot shorter. Its much quicker to type, but its old fashion from the ol' VB 6 days, so apparently its going to get removed, but if your just running tests or something, its much quicker and easier to use.

MOVING STUFF WHILE A FORM IS ALREADY RUNNING
One of the sexy things about using visual studio is that you get an interface which you can setup nicely and can have a good easy to look at form with command buttons, text boxes, ect.

One of the things that alot of people dont know about is that you can move these items while the form is already running. To do this, you just set a new location point for that item.

This is done with:
Code:
item.Location = New Point(X,Y)


With item being the item, x being the x co-ordinants and y being the y co-ordinants.

These values can of course be numbers and names written in in the code, or can be variables.

Example:
The form has a command button named cmdMove and another named cmdMakeItMove. With the code for the onclick comamnd for cmdMakeItMove being:
Code:
cmdMove.Location = New Point(10,10)

This will move the button cmdMove to the co-ordinants 10X and 10Y.

Example 2:
The form as a command button named cmdMove and another named cmdMakeItMove. With the code for the onclick command for cmdMakeItMove being:
Code:
Dim i,xValue, YValue as integer

For i = 1 to 100
xValue = cmdMove.Location.X
yValue = cmdMove.Location.Y
cmdMove.Location = New Point(xValue + 1, yValue + 1)
Next


This example moves cmdMove 100 places down and 100 places right when the user presses cmdMakeItMove. This also introduces a new piece of code,
Code:
value = item.Location.X

This gets the current value of the X co-ordinant from the item called item and assigns value to it.


There are plenty of other things you can do with the New Point and Location functions. Be sure to mess around with it....

Michael Doorn


Mine are the best!!
The above line was shamelessly stolen from Gregg Tomlinzon-Bell..

Opening a form by using a button
Dim <variable> As New <formname>
<variable>.showdialog
Where I wrote <variable> you should realise that you can use any variable name, as with <formname>

Splitting strings
Dim <input> As String
Dim <array>() As String
Dim i As Integer = 0
 
<input> = InputBox("Enter a word", "Enter a word", "jack be nimble jack be quick")
 
<array> = <input>.Split(" ")
 
For i = 0 To <array>.Length - 1
    MessageBox.Show(<array>(i))
Next
First of all.. where I wrote <array> and <input> you can, again, use any variable name.

This method will split the string into a new string every time it encounters a certain character, which in this case is a single space.

You could use this to split a series of sentences (seperate at "."). Another way it could be used is to count words by seperating it at the spaces as I have done here, and then finding the array size.

Sharon Janitz


Daniel Kelly


Erin Madson


One
Math.Pi holds the full value of Pi (3.14159265), you could just type out the number if you wanted, but I can never remember it (just used 3.14), so this helped back in set 11 (well I think it was set 11) when doing the circle stuff.

Imports System.Math

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sngPi, sngTwo As Single
sngPi = 180 * Math.Pi
sngTwo = 180 * 3.14
MessageBox.Show(Format(sngPi, "###,#00.00") & vbNewLine & Format(sngTwo, "###,#00.00"))
End Sub
End Class

sngPi will return the result 565.49, and sngTwo returns 565.20, not much of a difference but for more complex calculations it might keep the results more accurate.

Two
When doing the with thing the name of the list box (or combo bow, or whatever) can be left at the top so you don’t have to repeat it everytime.

With lstFruit.Items
.Add("Apple")
.Add("Cherry")
.Add("Grape")
.Add("Kiwi Fruit")
.Add("Watermelon")
.Add("Orange")
End With


Sean O'Donoghue


Suzanne Purves

Property - FormatString - can be used to format the characters within list boxes and combo boxes etc.
The options available include currency (so that the list or combo box will display two decimal points in numbers like 6.50 [without formatting VB will drop the trailing zero]), Date/Time (many different formats available in this option), and custom.

Ivor Sibley

Handy Hint 1
lstName2.Items.AddRange(lstName1.Items)
Adds all items from the listBox lstName1 to the listBox lstName2.

Ivor Sibley

Handy Hint 2
When designing a form, to align buttons, text boxes etc., highlight the relevant items, then go to menu -> Format -> Align or Centre In Form and align them horizontally, vertically, left, right, etc.

Karen Sims

To create a space Dim strspace = space(?) and put the amount of spaces you want in the brackets. When using 'strspace' it will automatically put in the number of spaces you have entered in the brackets..........hope this makes sense!

MID FUNCTION
To extract a certain number of characters from the middle of a string use the Mid Function.
Returns a substring from a string, specified as the starting position (counting from 1) and the number of characters.

Dim TestString As String = "Mid Function Demo"
Mid(TestString, 1, 3) 1 = first letter in and 3 how many letters long
Returns "Mid"

Mid(TestString, 14, 4) 14 =fourteen letter in and 4 four letters long
Returns "Demo"

Mid(TestString, 5) 5 = fifth letter in and then unlimited length
Returns "Function Demo"


Gregg Tomlinzon-Bell


Mine are the worst!!

#Region "Buttons"

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
MessageBox.Show("You could send the error info to a webservice, or an html form that takes the error data" & _
vbCrLf & "and sends an email, stores to a data source or both." & vbCrLf & vbCrLf & _
"This form is only one example of what to do with errors.", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

#End Region

this creates a group called Buttons which makes your code more neat & tidy which you can minimize if you don't need to view it. =)

ToolTip
In Order to have a message over an object, button, whatever when you hover over you use a VB function known as "Tooltip"

in order to do this you have to make the button function, or whatever object you have opted as a MouseHover, and NOT a 'Click' function. I will select a radio button for an example.


Private Sub radiobutton1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles radiobutton1.MouseHover
Me.ToolTip1.Show("Your Message", Me.radiobutton1, 5, Me.radiobutton1.Height \ 2, 5000)
End Sub

place the naming standard to where it applies. this is virtually similiar to a messagebox.show, it has a similiar format which is shown here in the bracketts.

The "Me.radiobutton1.height \ 2 " is how far below you want to tooltip message to display beneth it. 1 is furthest away and the higher the figure the closer it is on top on the button. 2 is too close for a radiobutton though and 5 is virtually on top of it. decimal places don't work, i haven't figured out what the 5000, or the 5 means yet. but it works =)