Visual Basics Design a form that mimics the following pattern: First, write some
ID: 3697882 • Letter: V
Question
Visual Basics
Design a form that mimics the following pattern:
First, write some functionality into the form class that allows basic addition to be performed. The user provides two addends and when the AddThem() button is pressed, the result is placed in the third text box.
For example....
Notice, that if you type invalid input in the two boxes on the left, the program will throw an exception!
Design exception handling code in your class that prevents the program from crashing. If the user types input that does not contain any digits, assume a value of zero. However, if the use types in a mix of digits and non-digits, the program should clean out all of the non-digits and work with the number that is left behind. Consider the following examples to clarify what you are supposed to be doing...
You will need to make use of string manipulation functions to accomplish this. Use the exception handling mechanism to handle the System.InvalidCastException object that gets thrown when the user types something in the box that cannot be easily converted to an integer representation. You may assume that all things being added in this program are integers, so treat the decimal point an just another problem character.
Explanation / Answer
Please go throught the below link and download the application
design code
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'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.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.btnAdd = New System.Windows.Forms.Button()
Me.textSum = New System.Windows.Forms.TextBox()
Me.label2 = New System.Windows.Forms.Label()
Me.label1 = New System.Windows.Forms.Label()
Me.textTwo = New System.Windows.Forms.TextBox()
Me.textOne = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'btnAdd
'
Me.btnAdd.Location = New System.Drawing.Point(203, 153)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.Size = New System.Drawing.Size(75, 23)
Me.btnAdd.TabIndex = 11
Me.btnAdd.Text = "Add Them"
Me.btnAdd.UseVisualStyleBackColor = True
'
'textSum
'
Me.textSum.Location = New System.Drawing.Point(358, 87)
Me.textSum.Name = "textSum"
Me.textSum.Size = New System.Drawing.Size(100, 20)
Me.textSum.TabIndex = 10
'
'label2
'
Me.label2.AutoSize = True
Me.label2.Location = New System.Drawing.Point(326, 90)
Me.label2.Name = "label2"
Me.label2.Size = New System.Drawing.Size(13, 13)
Me.label2.TabIndex = 9
Me.label2.Text = "="
'
'label1
'
Me.label1.AutoSize = True
Me.label1.Location = New System.Drawing.Point(185, 90)
Me.label1.Name = "label1"
Me.label1.Size = New System.Drawing.Size(13, 13)
Me.label1.TabIndex = 8
Me.label1.Text = "+"
'
'textTwo
'
Me.textTwo.Location = New System.Drawing.Point(220, 87)
Me.textTwo.Name = "textTwo"
Me.textTwo.Size = New System.Drawing.Size(100, 20)
Me.textTwo.TabIndex = 7
'
'textOne
'
Me.textOne.Location = New System.Drawing.Point(54, 87)
Me.textOne.Name = "textOne"
Me.textOne.Size = New System.Drawing.Size(100, 20)
Me.textOne.TabIndex = 6
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(513, 262)
Me.Controls.Add(Me.btnAdd)
Me.Controls.Add(Me.textSum)
Me.Controls.Add(Me.label2)
Me.Controls.Add(Me.label1)
Me.Controls.Add(Me.textTwo)
Me.Controls.Add(Me.textOne)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Private WithEvents btnAdd As System.Windows.Forms.Button
Private WithEvents textSum As System.Windows.Forms.TextBox
Private WithEvents label2 As System.Windows.Forms.Label
Private WithEvents label1 As System.Windows.Forms.Label
Private WithEvents textTwo As System.Windows.Forms.TextBox
Private WithEvents textOne As System.Windows.Forms.TextBox
End Class
.vb code
Public Class Form1
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
Dim textOneGetNumbers, textTwoGetNumbers
If textOne.Text = "" Then
MessageBox.Show("Please Enter First TextBox Number")
textOne.Focus()
Return
End If
If textTwo.Text = "" Then
MessageBox.Show("Please Enter Second TextBox Number")
textTwo.Focus()
Return
End If
Dim j As Integer = 0
Dim pos = 0
Dim textOneValue As String = "", textTwoValue As String = ""
If textOne.Text.Contains(".") OrElse textTwo.Text.Contains(".") Then
For j = 0 To textOne.Text.Length - 1
If textOne.Text(0) = "."c Then
textOne.Text = textOne.Text.Remove(0, 1)
ElseIf textOne.Text(textOne.Text.Length - 1) = "."c Then
textOne.Text = textOne.Text.Remove(textOne.Text.Length - 1, 1)
End If
Next
For j = 0 To textTwo.Text.Length - 1
If textTwo.Text(0) = "."c Then
textTwo.Text = textTwo.Text.Remove(0, 1)
ElseIf textTwo.Text(textTwo.Text.Length - 1) = "."c Then
textTwo.Text = textTwo.Text.Remove(textTwo.Text.Length - 1, 1)
End If
Next
textOneValue = textOne.Text
textTwoValue = textTwo.Text
Else
textOneGetNumbers = (From t In textOne.Text Where Char.IsDigit(t)).ToArray()
For i As Integer = 0 To textOneGetNumbers.Length - 1
textOneValue = textOneValue & textOneGetNumbers(i).ToString()
Next
textTwoGetNumbers = (From t In textTwo.Text Where Char.IsDigit(t)).ToArray()
For i As Integer = 0 To textTwoGetNumbers.Length - 1
textTwoValue = textTwoValue & textTwoGetNumbers(i).ToString()
Next
End If
textOne.Text = textOneValue
textTwo.Text = textTwoValue
Dim firstValue As Double = Convert.ToDouble(textOneValue)
Dim secondValue As Double = Convert.ToDouble(textTwoValue)
Dim sum As Double = firstValue + secondValue
Dim output As String
pos = sum.ToString().LastIndexOf("."c)
If pos >= 0 Then
output = sum.ToString().Substring(0, sum.ToString().LastIndexOf("."c))
Else
output = sum.ToString()
End If
textSum.Text = output
Catch EX As Exception
MessageBox.Show("Enter Correct Formate")
textOne.Text = "0"
textTwo.Text = "0"
textSum.Text = "0"
End Try
End Sub
End Class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.