0% found this document useful (0 votes)
31 views

Visual Basic Notes

Uploaded by

Surender Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Visual Basic Notes

Uploaded by

Surender Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

blinking text is great way to call attention to something on the screen.

It's easy to implement


blinking text using Visual Basic's Timer control. This example shows you how to make a Label
control blink, but the same approach could be used for other controls as well.
When you place a Timer control on a form, it does not display when the program runs -  it's
available "behind the scenes." The Timer control has two important properties. The Interval
property determines how often the Timer "ticks." The value is in milliseconds, so a setting of 500
will give you a twice per second Timer which I think is a good rate for blinking text. The
Enabled property determines whether the Timer is running (Enabled = True) or stopped (Enabled
= False).
The actual blinking is done in the Timer event procedure, which fires each time the Timer
"ticks."  One approach is to toggle the Label control's ForeColor property between black, which
makes it visible, and the value of its BackColor property, which makes the text invisible. You
must check the current value of the ForeColor property and then set it accordingly, as shown
here:
Private Sub Timer1_Timer()
If Label1.ForeColor = Label1.BackColor Then
    Label1.ForeColor = vbBlack
Else
    Label1.ForeColor = Label1.BackColor
End If
End Sub
You could also implement a different type of blinking, for example having the text alternate
between red and green:
Private Sub Timer1_Timer()
If Label1.ForeColor = vbRed Then
    Label1.ForeColor = vbGreen
Else
    Label1.ForeColor = vbRed
End If
End Sub
Accept Only Uppercase Letters in a Text Box
Problems can arise in some situations because users enter text with different
capitalization. This problem can be avoided by converting all text to uppercase as it is
entered. This tip shows you how.
Letters and other characters are represented numerically in Visual Basic. The
lowercase letters a-z are represented by the values 97-122 while the uppercase letters
A-Z are coded as 65-90. Thus, each uppercase letter's value is 32 less that the
corresponding lower case letter's value. To force input to uppercase all you need do is
check each character that is input and, if its value is between 97 and 122, subtract 32
from it. You would do this in the Text Box control's KeyPress event procedure:
Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii >= 97 And KeyAscii <= 122 Then


KeyAscii = KeyAscii - 32
End If
Selecting all Text when a Text Box gets the Focus
When the focus is moved to a Text Box that contains text, Visual Basic's default is to position
the caret (editing cursor) at the start of the text. In some applications it might be more
appropriate to have all of the control's text selected when it receives the focus. One advantage to
this is that the existing text will be deleted if the user starts typing something new. This tip shows
you how to implement this behavior.
A Text Box has two properties that determine what text, if any, is selected: 
 SelStart specifies the character position where the selection starts.
 SelLength specifies the length of the selection, in characters.
 You can select all text in a Text Box by setting SelStart to 0 and SelLength to the length of the
text. Here's a short procedure that does this: 
Sub SelectAllText(tb As TextBox) 
  tb.SelStart = 0
  tb.SelLength = Len(tb.Text)
End Sub
 Then, for each Text Box that you want to behave this way, call the procedure from the GotFocus
event procedure, passing the name of the Text Box as the argument:
Private Sub Text1_GotFocus() 
  SelectAllText Text1 
End Sub
This works when the Text Box gets the focus either by tabbing or by a mouse click.

One of the places where Visual Basic really shines is in the handling of dates using
the Date data type. You can display a date in any one of several standard date formats,
but internally it is represented as a serial number specifying the number of days since
December 31, 1899, with negative values used for dates before then. The decimal
portion of the value can be used to represent a time of day, but that is beyond the
scope of this tip.

The way dates are represented makes it really easy to compare dates, but some Visual
Basic programmers don't realize how easy it is and will go to unnecessary complexity
to perform a comparison. There's no need to extract the year, month, and day
individually for comparison--rather you can simply compare dates directly using the
standard comparison operators.

For example, the date June 30, 2004 is represented by the serial number 38168.
Comparing it with another date to see if it is earlier, the same, or later is really just
comparing the serial numbers to see if one is smaller, the same, or larger than the
other. Here's an example that executes one block of statements if the date stored in the
type Date variable MyDate is earlier than January 1, 2000 and another block if it is the
same or later than that date.
If MyDate < #1/1/2000# Then
  ' Statements here are executed if MyDate is earlier.
Else
  ' Statements here are executed if MyDate is the same or later.
End If
This is just one example of the flexibility of Visual Basic's Date data type.

You might also like