Handy Hints Semester 1, 2008

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


Baker, Judith


Comment Keyboard Shortcuts

Comment blocks are great to comment/uncomment a whole section of code easily in VB.
Once you know the shortcut keys for doing so, it is even easier, as you don't even have to reach for the mouse.

  • Ctrl + K + C = Comment Block the currently selected code
  • Ctrl + K + U = Uncomment Block the currently selected code
These shortcuts will comment/uncomment the current line if nothing is selected Comment blocks are great to comment/uncomment a whole section of code easily in VB.

Wrap Label Text on Forms


Bick, Tanis


Bignell, Jason

Ctrl + Space is your friend. this handy shortcut can be used at anytime to look through all possible endings for what you have typed. However, it is so much more. it can also be used to look through varibles, object,methods etc, simpl by clicking it when on an empty line. but wait, there's more! If there is only one thing that starts with what you have typed, it will complete it for you. (note: while scrolling through it, if you press tab, it will fill in the selected item for you!)

The local window.... wow, why did i ever use quick watch? It is a very similar to the quick watch window, only it automaticly adds all current varibles. Handy no?

Dennison, Jennifer



Dixon, Steven



Gilbert, Mary-Anne : - )


Create New Text File In Visual Studio


Checking Input in Text Boxes




Hewton, Grant

Setting Properties in Form Design for Multiple Controls
eg. To disable multiple textboxes you would highlight and select all the required textboxes and then simply change the properties of all of them at once!

Using the Tag property so you can restrict the use of the control in question
procedure(ByVal sender as system.object)

procedure(sender)
calling procedure passing sender as a system.object
this allows you to case sender.tag in your procedure
Select Case sender.tag
case "1"
do this stuff
case "2"
do this stuff
End Select
sender.tag = "" 'takes the tag away so the control wont be used twice

Jones, Alisha

Dim fname = " Bill "

MessageBox.Show("Hello" & Trim(fname) & "Gates") ' removes all spaces

MessageBox.Show("Hello" & LTrim(fname) & "Gates") ' removes spaces to the left of the word

MessageBox.Show("Hello" & RTrim(fname) & "Gates") ' removes spaces from the right of the word


the Trim function removes any spaces on the left or right of the word variable.



Randomize()
Dim randomNumber = Int(100 * Rnd())
MessageBox.Show("Random number: " & randomNumber)

Rnd() displays a random number each time it is clicked on. You need a number to limit the range of random numbers


Keating, Tristan

Tab Order

When you are waiting to redo the Tab Order for a page, instead of redoing the tab order via the Properties task pane, use View » Tab Order. Numbers will appear, and then you click the controls in the order you like. Be sure to do every control. To stop Tab Order, turn it back off with View » Tab Order.

Adding Handlers

For those that seek to create new controls to forms at run-time, here's a great way to add functionality. This command is the key:

AddHandler btnNew.Click, AddressOf btnNew_Click()

Where btnNew.Click is the click event of the new button and btnNew_Click() is a sub.


Kennett, Adam

~ Using the directional arrows keys, you can move the selected object pixel by pixel for accurate assembly.
~ In the code view, click on the tab window. Then click add split. This will create a split half way down the screen allowed you to work or look at two different part of code in the code view. (ca you upload pics onto her, or do i just upload somewhere else and copy the url here??)


Kenny, Owen

An easy way to convert numeric data to currency is the FormatCurrency() function.
Example: You have to display the result of a simple equation to calculate an employees pay in a text box
using their hourly rate and the hours worked.

Dim hoursWorked as double
Dim payRate as double

txtPay.Text = FormatCurrency( hoursWorked * payRate )

The result will display in the text box txtPay in currency format. Saves you having to put in your own format specification!

Kirkpatrick, Keiran

Use regions to organise your code.

Example:

#Region "Example statements"
Private Sub Me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Code goes here
End sub
#End Region

This will allow you to sort your code into expandable/collapsible groups using the +/- icons on the left.

Shared Methods
Methods that do not access instance data or instance methods can be marked as Shared. This will allow the compiler to generate code that is more optimized for performance.

Ledo, Aiden

Shortcut Keys

Description

F2

Brings up the 'Object Browser' window

F3/F4/F6/F11

Brings up the 'Properties Window' for the current form/control

F5

Executes the current project

Ctrl + F5

First checks for syntax errors and then executes the project

F7

Opens the code window for the current control

F8

Executes the project in 'Step mode'. Keep pressing F8 to execute the next statement; Press F5 to come out of step mode

F9

Inserts break point at the current line in the code window. Press F9 again to toggle the break point

Ctrl + G

Brings up the 'Immediate window', which is useful for debugging purposes

Ctrl + Spacebar

While writing code, this combination helps you see the available options/methods/properties based on what you are typing

Ctrl + J

Same as above...

Shift + F2

Place the cursor over any function call and press Shift + F2 to see the declaration of that function

Ctrl + F

Brings up the find dialog box

Ctrl + H

Brings up the replace dialog box

Shift + F3

Finds the previous occurrence of a string

Ctrl + Shift + F9

Clears all break points

Ctrl + Delete

Deletes to the end of the word

Crtl + Y

Deletes the current line of code

Ctrl + Z

Undoes the last change in the code window

Ctrl + Up/Down arrows

Helps you move between procedures/functions in the code window
2)
global vairables:
making global variables makes coding alot easier and faster because you
dont have to mention or dim the varible each time you start a new private
sub, hence a down fall if you do make the global variable it can be altered
by a user that comes by it, hence why we make it private.

3)
commenting your coding:
comment your code as you write your code could be a useful as a revising/learning
tool not only for yourself but others aswell. By commenting your code you
will be able to see what youve done and what it does and you understand your code better.
also looking back at it in time may help you understand what you were doing aswell.

4)
naming your controls:
naming your controls is essential leaving them as the default name is considered slack
in the programmers eye. to name your controls properly helps you remember what each of
those controls are for and what they do. it also shows the individuality between other coders work.


McCool, Joshua

When searching for a string (like you have to in set 16) there are lots of different ways to do so but i found the easiest way to do so was to use one of the below:
1.FindString - use if you are using a textbox that searches for items that begin with the letters you type
example

Dim search As String

search = txtSearch.Text
If lstNames.FindString(search) < 0 Then

MessageBox.Show("Doesn't Exist", "Search Failed")
Else

lstNames.SelectedIndex = lstNames.FindString(search)
End If

2.FindStringExact - this one is self explanatory, it searches for the exact string
example
Dim search As String

search = txtSearch.Text
If lstNames.FindStringExact(search) < 0 Then

MessageBox.Show("Doesn't Exist", "Search Failed")
Else

lstNames.SelectedIndex = lstNames.FindStringExact(search)
End If

3. When using the items.count method for a combobox or list box it is important that you keep the following in mind.
List/Combo boxes start their numbering at 0 whereas the Items.Count begins numbering at 1 and thus can generate an error in your code. To overcome this simply put -1 at the end of the items.count
example

For i = 0 To cbofruit.Items.Count - 1
temp &= cbofruit.Items(i) & vbNewLine
Next

Morrow, Alex



Neighbour, Tony

To save a variable that can exist outside of a sub, set it before anything else and make it public. however, if you want to use it in a module as well as in form code without it changing, be sure to state it in the module rather then in the form code.
If you are going to repeatidly use a bit of code, it may be handy to save it into a sub or function. then you can just call it later with the correct variables it needs (or none if thats the way it is made).
Do not underestimate the importance of the count method. if you make an array, a list box or a combo box. etc you can count how many items are in each. this is handy when using for loops as you will not need to change the loop (or when more advanced, many loops and if's). it just makes the code more flexible so to speak.

Nolan, AJ


When making grid like components in run-time use a 2 dimensional array and a embedded For loop. EG.

Dim example(2, 2) as PictureBox

For i as integer = 0 to 2
For j as integer = 0 to 2
example( i, j ) = new picturebox
example( i, j ).location = new point( i * 64, j * 64)

Form1.control.add(example( i, j ))
next
next

If you are required to validate the contents of an control array (the contents of example in this case) and need to go a specific location that is not x, nor y
but rather xy (that is the line is not verticle, or horizontal, but diagional) then use an embeded while loop.

Dim example as Picturebox
'gets location of sender
Dim currentX as Integer = sender.left / 64
Dim currentY as Integer = sender.top / 64

'sets initial value of destination (where we want to go)
Dim destinatonX as Integer = currentX
Dim destinationY as integer = currentY

'if destination is greater than where we want to go (left top corner) then -1 from destination until we get there
While destinationX > 0 destinationY > 0
destinationX - = 1
destinationY - = 1

Salway, Alexander



Tidball, Jim


Tip 1

While this may appear, to some of you, to be blatantly obvious, Google is your friend when dealing with problems with Visual Basic.Net. Visual Basic has existed, in varies forms, since 1991 therefore, there are many large and well established online locales where you can find information on absolutely everything you may need to know. Many people will probably have asked the same questions you have and the answers these individuals have recieved are freely available.

Also note that if your find a solution to your problem that is only available in visual basic 6 source code, all is not lost. Due to the significant changes Microsoft made for visual basic.net and the large user base visual basic 6 had, they had to be excruciatingly detailed in their explanations of changes between vb6 and vb.net. If you have visual basic 6 source code which could fix your problem, translating this code into visual basic.net source can take little more than a quick visit to the msdn website. _

Tip 2

Microsoft has left a number of legacy (Read: outdated) features in visual basic.net to ease migration of old visual basic 6 code to the .NET framework. It is important that you avoid getting into the habit of using these features as it is likely that in the future they will be removed completely.