Library code snippets
Speed tips for beginners
Have you ever tried to write a program where you've had to type something over and over? Well with this tutorial, you'll learn about speed saving things like modules, loops, and the with statement.
First off, I'll start with something REALLY easy but really time saving. It's
called the with statement. Have you ever had to modify several properties of
an object at once? If so, without the with statement you'd have a LOT of typing
to do. Your code might look like this. Text1.Text = "BlAh"
Text1.Font = "Arial"
Text1.Enabled = "True"
Text1.Height = 1000
Well, there is an way that makes it much faster. It's called the with statement.
It saves you time by making it so you don't have to type the name of the object
over and over again. You would use it like this. With Text1
.Text = "BlAh"
.Font = "Arial"
.Enabled = "True"
.Height = 1000
End With
See the difference? A little extra code saves you from typing an object's name
over and over. Make sure you remember the "End With". Also, it's a good idea
to indent the property changes inside the with tags, since it's good to get into
the habit of indenting.
The next thing that's really useful is loops. They allow you to repeat a process
over and over, only typing it once. There are different types of loops. I'll
tell keep it simple and tell you about the "Until" loop. Say you had a schedule
and that gave you spaces to write down what you were supposed to be doing at
five minute intervals. Let's say you want to exercise from 3:00 to 3:30. You
wouldn't put "Exercise for 5 minutes" in every slot, would you? You'd probably
put "Exercise until 3:30" at the 3:00 slot and leave the rest blank. That's like
an "until" loop. Here's an example of an "Until" loop: Do Until Text1.Text = "JoeJoeJoe"
Text1.Text = Text1.Text + "Joe"
Loop
That would make Text1.Text say "JoeJoeJoe" and you didn't have to type "Text1.Text
= Text1.Text + "Joe"" 3 times. That's how an until loop works.
One thing that's very useful is modules. First, we need to create a module. Say
you had a function, for example, changing a text box's font. If you wanted to
use it several times, for different fonts, then you could use a module instead
of typing it over and over again. Make a new project in Visual Basic, and add
a text box, and a button. Go to the Project menu and select "Add Module". Select
"Module" on the pop up window and hit Open. Now you'll see a coding window. Modules
are like forms, but they don't have any form, they're just code. Modules allow
you to write code that can be called by other forms, just like Public subs. Copy
this code and paste it in your module. Now copy this code into your module. Sub Changefont(FN As String)
Form1.Text1.Font = FN
End Sub
Notice the "FN As String" in brackets. This defines "FN" as a string, and that
string will receive input from the form using the function when the function
is used. But we'll get to that later. Also, notice how we put "Form1.Text1.Font"
instead of just "Text1.Font". This is because the module is not a part of Form1,
so we have to make it know which Form the textbox it's modifying is on. Now,
go back to your form, and add this to "Private Sub Command1_Click" Changefont (Tahoma)
Notice the "Tahoma" in brackets. That's the name of the font we're changing to.
You can substitute any font for that, but I like Tahoma, so I used it. What this
does is tells your "Changefont" function the variable FN is. For an exercise,
try writing a function that changes a buttons caption.
Hopefully this tutorial will help you write programs more quickly, and save space.
Related articles
Related discussion
-
Problem with migration to C# (CoCreateInstanceEx)
by LRollison (1 replies)
-
VB6 Problem Creating Shortcuts
by rb1177 (0 replies)
-
how can i open a file
by kyawswarhtun (0 replies)
-
how to save any one form what i want?
by blackguy (5 replies)
-
Build an MP3 Player
by soybees (4 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
I don't understand what you're saying, could you rephrase that?
Sorry, can't help you there
Good Morning.
I have one question regarding a VB project. I relatively new to VB but I do have some years of programming experience. Trying to add the masked box to my project I incorrectly added a reference to the msmask32.ocx file. What I should have done was add the MS Mask Edit control 6.0 Component. However adding the reference first stops me from adding the control component. I get this error.
"Name conflicts with existing module, project or object libray"
I have tried to remove the reference but I don't see the C:\WINNT\system\MSMASK32.ocx in the reference list. Do you know where to remove this reference from my project?
Your time is highly appreciated, thank you. - J. Ponce
hi all
i m new to programming environment and thought VB could be a better language to start with.As most our applications we work for client require some customisation, programming beomes essential.Getting to know from friends about VB, i thought to start with VB,but couldn't find a better learning curve method.We work on Maps(graphics) and related databases (attributes), any learning method linking graphics and databases as sample example would be perfect
Hariharan.k
This thread is for discussions of Speed tips for beginners.