tqtclipper
asked on
Problem with my Visual Basic Code
Hi EE'ers,
I am having some problems with my visual basic code. I am trying to write a liter conversion program (see code below). I am having problems getting the program to do the following:
1. When I hit the reset button the end up in the second box and I need the cursor to end in box 1.
2. If I enter an alpha character the program bombs out on me with the following error:
An unhandled exception of type 'System.InvalidCastExcepti on' occurred in microsoft.visualbasic.dll
Additional information: Cast from string "a" to type 'Double' is not valid.
I need the program to detect the null character....
I know that #2 need some type of IF ESLE THEN statement I am having trouble writing the correct statement.
Thanks,
BTW: I am new to VB
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon tainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextB ox
Friend WithEvents GroupBox1 As System.Windows.Forms.Group Box
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents GroupBox2 As System.Windows.Forms.Group Box
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextB ox
Friend WithEvents TextBox3 As System.Windows.Forms.TextB ox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents btnCompute As System.Windows.Forms.Butto n
Friend WithEvents btnReset As System.Windows.Forms.Butto n
Friend WithEvents btnQuit As System.Windows.Forms.Butto n
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextB ox
Me.btnCompute = New System.Windows.Forms.Butto n
Me.GroupBox1 = New System.Windows.Forms.Group Box
Me.Label1 = New System.Windows.Forms.Label
Me.btnReset = New System.Windows.Forms.Butto n
Me.GroupBox2 = New System.Windows.Forms.Group Box
Me.Label3 = New System.Windows.Forms.Label
Me.TextBox3 = New System.Windows.Forms.TextB ox
Me.TextBox2 = New System.Windows.Forms.TextB ox
Me.Label2 = New System.Windows.Forms.Label
Me.btnQuit = New System.Windows.Forms.Butto n
Me.GroupBox1.SuspendLayout ()
Me.GroupBox2.SuspendLayout ()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Font = New System.Drawing.Font("Micro soft Sans Serif", 9.75!, System.Drawing.FontStyle.R egular, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.TextBox1.Location = New System.Drawing.Point(24, 56)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'btnCompute
'
Me.btnCompute.Enabled = False
Me.btnCompute.Font = New System.Drawing.Font("Micro soft Sans Serif", 9.75!, System.Drawing.FontStyle.R egular, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.btnCompute.Location = New System.Drawing.Point(24, 96)
Me.btnCompute.Name = "btnCompute"
Me.btnCompute.Size = New System.Drawing.Size(100, 32)
Me.btnCompute.TabIndex = 3
Me.btnCompute.Text = "Compute"
'
'GroupBox1
'
Me.GroupBox1.Controls.Add( Me.Label1)
Me.GroupBox1.Controls.Add( Me.btnRese t)
Me.GroupBox1.Controls.Add( Me.btnComp ute)
Me.GroupBox1.Controls.Add( Me.TextBox 1)
Me.GroupBox1.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.R egular, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.GroupBox1.Location = New System.Drawing.Point(24, 16)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(146, 192)
Me.GroupBox1.TabIndex = 4
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Enter Metric Value"
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Micro soft Sans Serif", 9.75!, System.Drawing.FontStyle.R egular, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(24, 32)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(100, 16)
Me.Label1.TabIndex = 5
Me.Label1.Text = "Liters:"
'
'btnReset
'
Me.btnReset.Enabled = False
Me.btnReset.Font = New System.Drawing.Font("Micro soft Sans Serif", 9.75!, System.Drawing.FontStyle.R egular, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.btnReset.Location = New System.Drawing.Point(24, 144)
Me.btnReset.Name = "btnReset"
Me.btnReset.Size = New System.Drawing.Size(100, 30)
Me.btnReset.TabIndex = 4
Me.btnReset.Text = "Reset"
'
'GroupBox2
'
Me.GroupBox2.Controls.Add( Me.Label3)
Me.GroupBox2.Controls.Add( Me.TextBox 3)
Me.GroupBox2.Controls.Add( Me.TextBox 2)
Me.GroupBox2.Controls.Add( Me.Label2)
Me.GroupBox2.Location = New System.Drawing.Point(200, 16)
Me.GroupBox2.Name = "GroupBox2"
Me.GroupBox2.Size = New System.Drawing.Size(146, 192)
Me.GroupBox2.TabIndex = 5
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "Equivalent English Value"
'
'Label3
'
Me.Label3.Font = New System.Drawing.Font("Micro soft Sans Serif", 9.75!, System.Drawing.FontStyle.R egular, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(24, 104)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(100, 16)
Me.Label3.TabIndex = 3
Me.Label3.Text = "Gallons:"
'
'TextBox3
'
Me.TextBox3.Font = New System.Drawing.Font("Micro soft Sans Serif", 9.75!, System.Drawing.FontStyle.R egular, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.TextBox3.Location = New System.Drawing.Point(24, 128)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.ReadOnly = True
Me.TextBox3.TabIndex = 2
Me.TextBox3.Text = ""
'
'TextBox2
'
Me.TextBox2.Font = New System.Drawing.Font("Micro soft Sans Serif", 9.75!, System.Drawing.FontStyle.R egular, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.TextBox2.Location = New System.Drawing.Point(24, 56)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.ReadOnly = True
Me.TextBox2.TabIndex = 1
Me.TextBox2.Text = ""
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("Micro soft Sans Serif", 9.75!, System.Drawing.FontStyle.R egular, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.Label2.Location = New System.Drawing.Point(24, 32)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(100, 16)
Me.Label2.TabIndex = 0
Me.Label2.Text = "Pints:"
'
'btnQuit
'
Me.btnQuit.Font = New System.Drawing.Font("Micro soft Sans Serif", 9.75!, System.Drawing.FontStyle.R egular, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.btnQuit.Location = New System.Drawing.Point(136, 224)
Me.btnQuit.Name = "btnQuit"
Me.btnQuit.Size = New System.Drawing.Size(100, 30)
Me.btnQuit.TabIndex = 6
Me.btnQuit.Text = "Quit"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(369, 271)
Me.Controls.Add(Me.btnQuit )
Me.Controls.Add(Me.GroupBo x2)
Me.Controls.Add(Me.GroupBo x1)
Me.Name = "Form1"
Me.Text = "Metric to English Volume Conversion"
Me.GroupBox1.ResumeLayout( False)
Me.GroupBox2.ResumeLayout( False)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
End
End Sub
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
Dim inpLiter, equivPint, equivGallon 'This line creates the needed variables
inpLiter = TextBox1.Text 'Puts inputted Liter value in the inpLiter variable
equivPint = inpLiter * 2.113 'Converts inputted Liters into the equivalent Pints
equivGallon = inpLiter * 0.264125 'Converts inputted Liters into the equivalent Gallons
TextBox2.Text = equivPint 'Displays equivalent Pints in TextBox2
TextBox3.Text = equivGallon 'Displays equavalent Gallons in TextBox3
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
TextBox1.Text = "" 'Clears data from TextBox1 (Input Liters)
TextBox2.Text = "" 'Clears data from TextBox2 (Equivalent Pints)
TextBox3.Text = "" 'Clears data from TextBox3 (Equivalent Gallons)
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text = "" Then
btnCompute.Enabled = False 'Disables the Compute button before data is entered
btnReset.Enabled = False 'Disables the Reset button before data is entered
Else
btnCompute.Enabled = True 'Enables the Compute button when data is entered
btnReset.Enabled = True 'Enables the Reset button when data is entered
End If
End Sub
Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter
End Sub
End Class
I am having some problems with my visual basic code. I am trying to write a liter conversion program (see code below). I am having problems getting the program to do the following:
1. When I hit the reset button the end up in the second box and I need the cursor to end in box 1.
2. If I enter an alpha character the program bombs out on me with the following error:
An unhandled exception of type 'System.InvalidCastExcepti
Additional information: Cast from string "a" to type 'Double' is not valid.
I need the program to detect the null character....
I know that #2 need some type of IF ESLE THEN statement I am having trouble writing the correct statement.
Thanks,
BTW: I am new to VB
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextB
Friend WithEvents GroupBox1 As System.Windows.Forms.Group
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents GroupBox2 As System.Windows.Forms.Group
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextB
Friend WithEvents TextBox3 As System.Windows.Forms.TextB
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents btnCompute As System.Windows.Forms.Butto
Friend WithEvents btnReset As System.Windows.Forms.Butto
Friend WithEvents btnQuit As System.Windows.Forms.Butto
<System.Diagnostics.Debugg
Me.TextBox1 = New System.Windows.Forms.TextB
Me.btnCompute = New System.Windows.Forms.Butto
Me.GroupBox1 = New System.Windows.Forms.Group
Me.Label1 = New System.Windows.Forms.Label
Me.btnReset = New System.Windows.Forms.Butto
Me.GroupBox2 = New System.Windows.Forms.Group
Me.Label3 = New System.Windows.Forms.Label
Me.TextBox3 = New System.Windows.Forms.TextB
Me.TextBox2 = New System.Windows.Forms.TextB
Me.Label2 = New System.Windows.Forms.Label
Me.btnQuit = New System.Windows.Forms.Butto
Me.GroupBox1.SuspendLayout
Me.GroupBox2.SuspendLayout
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Font = New System.Drawing.Font("Micro
Me.TextBox1.Location = New System.Drawing.Point(24, 56)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'btnCompute
'
Me.btnCompute.Enabled = False
Me.btnCompute.Font = New System.Drawing.Font("Micro
Me.btnCompute.Location = New System.Drawing.Point(24, 96)
Me.btnCompute.Name = "btnCompute"
Me.btnCompute.Size = New System.Drawing.Size(100, 32)
Me.btnCompute.TabIndex = 3
Me.btnCompute.Text = "Compute"
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Font = New System.Drawing.Font("Micro
Me.GroupBox1.Location = New System.Drawing.Point(24, 16)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(146, 192)
Me.GroupBox1.TabIndex = 4
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Enter Metric Value"
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Micro
Me.Label1.Location = New System.Drawing.Point(24, 32)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(100, 16)
Me.Label1.TabIndex = 5
Me.Label1.Text = "Liters:"
'
'btnReset
'
Me.btnReset.Enabled = False
Me.btnReset.Font = New System.Drawing.Font("Micro
Me.btnReset.Location = New System.Drawing.Point(24, 144)
Me.btnReset.Name = "btnReset"
Me.btnReset.Size = New System.Drawing.Size(100, 30)
Me.btnReset.TabIndex = 4
Me.btnReset.Text = "Reset"
'
'GroupBox2
'
Me.GroupBox2.Controls.Add(
Me.GroupBox2.Controls.Add(
Me.GroupBox2.Controls.Add(
Me.GroupBox2.Controls.Add(
Me.GroupBox2.Location = New System.Drawing.Point(200, 16)
Me.GroupBox2.Name = "GroupBox2"
Me.GroupBox2.Size = New System.Drawing.Size(146, 192)
Me.GroupBox2.TabIndex = 5
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "Equivalent English Value"
'
'Label3
'
Me.Label3.Font = New System.Drawing.Font("Micro
Me.Label3.Location = New System.Drawing.Point(24, 104)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(100, 16)
Me.Label3.TabIndex = 3
Me.Label3.Text = "Gallons:"
'
'TextBox3
'
Me.TextBox3.Font = New System.Drawing.Font("Micro
Me.TextBox3.Location = New System.Drawing.Point(24, 128)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.ReadOnly = True
Me.TextBox3.TabIndex = 2
Me.TextBox3.Text = ""
'
'TextBox2
'
Me.TextBox2.Font = New System.Drawing.Font("Micro
Me.TextBox2.Location = New System.Drawing.Point(24, 56)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.ReadOnly = True
Me.TextBox2.TabIndex = 1
Me.TextBox2.Text = ""
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("Micro
Me.Label2.Location = New System.Drawing.Point(24, 32)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(100, 16)
Me.Label2.TabIndex = 0
Me.Label2.Text = "Pints:"
'
'btnQuit
'
Me.btnQuit.Font = New System.Drawing.Font("Micro
Me.btnQuit.Location = New System.Drawing.Point(136, 224)
Me.btnQuit.Name = "btnQuit"
Me.btnQuit.Size = New System.Drawing.Size(100, 30)
Me.btnQuit.TabIndex = 6
Me.btnQuit.Text = "Quit"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(369, 271)
Me.Controls.Add(Me.btnQuit
Me.Controls.Add(Me.GroupBo
Me.Controls.Add(Me.GroupBo
Me.Name = "Form1"
Me.Text = "Metric to English Volume Conversion"
Me.GroupBox1.ResumeLayout(
Me.GroupBox2.ResumeLayout(
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
End
End Sub
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
Dim inpLiter, equivPint, equivGallon 'This line creates the needed variables
inpLiter = TextBox1.Text 'Puts inputted Liter value in the inpLiter variable
equivPint = inpLiter * 2.113 'Converts inputted Liters into the equivalent Pints
equivGallon = inpLiter * 0.264125 'Converts inputted Liters into the equivalent Gallons
TextBox2.Text = equivPint 'Displays equivalent Pints in TextBox2
TextBox3.Text = equivGallon 'Displays equavalent Gallons in TextBox3
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
TextBox1.Text = "" 'Clears data from TextBox1 (Input Liters)
TextBox2.Text = "" 'Clears data from TextBox2 (Equivalent Pints)
TextBox3.Text = "" 'Clears data from TextBox3 (Equivalent Gallons)
End Sub
Private Sub TextBox1_TextChanged(ByVal
If TextBox1.Text = "" Then
btnCompute.Enabled = False 'Disables the Compute button before data is entered
btnReset.Enabled = False 'Disables the Reset button before data is entered
Else
btnCompute.Enabled = True 'Enables the Compute button when data is entered
btnReset.Enabled = True 'Enables the Reset button when data is entered
End If
End Sub
Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
For Question 2
What is I wanted the user to receive a error message say "Incorrect value" Please enter numeric value or something like that??
What is I wanted the user to receive a error message say "Incorrect value" Please enter numeric value or something like that??
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPr essEventAr gs) Handles TextBox1.KeyPress
If Not IsNumeric(e.KeyChar) Then
MsgBox "Incorrect value. Please enter a numeric value."
e.Handled = True
End If
End Sub
If Not IsNumeric(e.KeyChar) Then
MsgBox "Incorrect value. Please enter a numeric value."
e.Handled = True
End If
End Sub
'Emty the contents of display boxes
Dim btnReset As String
Me.TextBox1.Clear()
Me.TextBox2.Clear()
Me.TextBox3.Clear()
Dim btnReset As String
Me.TextBox1.Clear()
Me.TextBox2.Clear()
Me.TextBox3.Clear()
ASKER