CSC 216 Useful File Programming Tips, Spring 2009
Notre Dame University–Louaize · Computer Programming I · 8 pages
More CSC 216 files
Show the text of this file
Single-line Declarations
Visual Basic permits combining multiple declarations into one statement:
Dim var1 as datatype, var2 As datatype, ..., varN As datatype
When all variables are of the same datatype:
Dim num1, num2, total as double
Is there a number in the textbox?
If IsNumeric(TextBox1.Text) = False Then .....
Is the number between 1 and 10?
If Val(TextBox1.Text) < 1 or Val(TextBox1.Text) > 10 Then
messagebox.show("Please enter a number between 1 and 10")
Exit Sub
Enf If
User Input using InputBox
Dim x as String
x = InputBox("Enter a value", "Input Dialog")
Number only TextBox
Add the following code to a TextBox KeyPress handler:
If Asc(e.KeyChar) < 48 or Asc(e.keyChar) > 57 Then
e.Handled = True
End If
Displaying a form
Enter the following code in Form1:
Dim frmMyForm As New Form2()
MyForm.Show()
Assigning a color
TextBox1.backcolor = System.Drawing.Color.Blue
The intellisence feature of VB will provide you with tens of options as soon as you key in the
last dot.
Good Luck
Assigning a color
TextBox1.backcolor = System.Drawing.Color.Blue
The intellisence feature of VB will provide you with tens of options as soon as you key in the
last dot.
Good Luck
Primitive Data Types
The integral value types:
Byte (1-byte unsigned integer), Short (2-byte signed integer), Integer (4-byte signed integer), and
Long (8-byte signed integer).
These types map to System.Byte, System.Int16, System.Int32, and System.Int64, respectively.
The default value of an integral type is equivalent to the literal 0.
The floating-point value types:
Single (4-byte floating point) and Double (8-byte floating point).
These types map to System.Single and System.Double, respectively. The default value of a
floating-point type is equivalent to the literal 0.
The Decimal type (16-byte decimal value), which maps to System.Decimal. The default value of
decimal is equivalent to the literal 0D.
The Boolean value type, which represents a truth value, typically the result of a relational or
logical operation. The literal is of type System.Boolean. The default value of the Boolean type is
equivalent to the literal False.
The Date value type, which represents a date and/or a time and maps to System.DateTime. The
default value of the Date type is equivalent to the literal # 01/01/0001 12:00:00AM #.
The Char value type, which represents a single Unicode character and maps to System.Char. The
default value of the Char type is equivalent to the constant expression ChrW(0)
Using Try...Catch blocks
To avoid error messages, use Try...Catch blocks in the following manner:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Try
Dim a As Integer
a = TextBox1.Text
MessageBox.Show(a, Me.Text)
Catch ex As Exception
End Try
End Sub
What was the error?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Try
Dim a As Integer
a = TextBox1.Text
MessageBox.Show(a, Me.Text)
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text)
TextBox1.Clear()
End Try
End Sub
Explicit Conversions
Use the methods of class Convert.
Example:
number = Convert.ToDouble(txtInput.Text)
Locking Controls
When designing the user interface of your Windows application, you can lock the controls once
they are positioned correctly, so that you do not inadvertently move or resize them when setting
other properties.
1) Fomat
2) Select Lock Controls
Random Number Generation
Numbers can be generated randomly by using class Random which is located in namespace
System.
Dim randObj As Random = New Random()
Dim x As Integer = randomObject.Next(1, 21)
Method Next, here, produces integers in the range 1-20
Method Next generates an integer value between 0 and 2147483647.
When randObj is created, the current time of day becomes the seed value for the calculation.
You can pass your own seed value as an argument:
Dim randObj As Random = New Random(9)
Good Luck
TextBox/Highlight
TextBox1.Focus()
TextBox1.Select()
Good Luck
'Sub Main' was not found
Renaming the class that contains the form's implementation is done by changing the Name
property of the form. However, this will display an error message saying that form1 is not found.
To overcome this error:
open the solution explorer
right-click on the projects name
select properties
from the start up object drop down select the new form name
Good Luck
Finding all files in a directory
1) Draw a button and a listbox on the form
2) Add the line before public class:
imports system.io
3) declare the variables in the button's click event
Dim fileArray As String( )
Dim myFile As String
3)continue with this code
fileArray = Directory.GetFiles("c:\")
For Each myFile In fileArray
lstData.items.add(myFile)
Next
Good Luck
Finding all drives on a system
1) Add the following line before public class:
imports system.io
2)Use a block of code similar to finding files and implement step 3
3)Use the Directory class to find for you the drives:
Directory.GetLogicalDrives
Good Luck
Openning a Web site, a file, or a folder
System.Diagnostics.Process.Start("C:\")
AppendText
This method appends text in the parameter to whatever exists in the text box at the time:
Textbox1.AppendText("Your grade is " & grade & ControlChars.NewLine)
Listing files in all directory
Dim dir As New DirectoryInfo("c:\")
Dim files As FileInfo() = dir.GetFiles()
Dim file As FileInfo
For Each file In files
ListBox1.Items.Add(file.Name)
Next
--Try replacing Name by extension
PictureBox/Run time
The PictureBox is used for displaying pictures.
The following lingo assumes that the file called image.bmp in the current folder.
PictureBox1.Image = System.Drawing.Image.FromFile(Application.ExecutablePath &
"\image.bmp")
PictureBox/Design time
A bitmap can be added to a PictureBox at design time.
At that point, the bitmap file on disk is not needed anymore.
The bitmap is now stored inside the form binary file called Form1.resx.
PictureBox/Animated Gif
A PictureBox accepts animated gif files.
Data Type/Size/Range
Byte: 1; 0 to 255
Boolean: 2; True or False
Char: 2; 0 to 65535
Short: 2; -32,768 to 32,767
Integer: 4; -2,147,483,647
Single: 4; +/- 3.4E38
Object: 4; Any value
Date: 8; Jan 1, 0001 to Dec 31, 9999
Double: 8; +/- 1.79E308
Long: 8; +/- 9,223,372,038,854,775,807
Decimal: 16; +/-79,228*10^24
String: 2*length; 0 to 2 billion
Examples of Declarations
Dim Married As Boolean
Married = True
Dim MiddleInitial As Char
MiddleInitial = "Z"
Dim BirthDate As Date
BirthDate = #1/1/2002#
Dim Something As Object
Something = "a text message"
Finding all Controls on the Form
For n = 0 to Controls.Count() - 1
Listbox1.Items.Add(Controls.Items(n).Name)
Next
Changing font style
Dim style As FontStyle
style = FontStyle.Bold Or FontStyle.Italic
Dim myFont As Font = New Font("ariel", 14, style)
label1.Font = myFont
Listbox / Inserting an item at a specific location
listbox1.Items.Insert(Index, Item)
Item is the object to be added
Index is the location of the new item
Listbox / How to avoid the insertion of identical objects in the listbox
Dim strItem As String = "Hello"
If not ListBox1.Items.Contains(strItem) Then
Listbox1.Items.Add(strItem)
End If