This is my first time using visual studio and I'm running into an error with my tax calulator application.
The problem is that visual studio is saying that the multiplication line using "*" and "+" can not be done due to the variables being text boxes. So my question to the community, is should I change the text box to something else? Or where in my code did I mess up.
Public Class Form1
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If (IsNumeric(txtSale) And IsNumeric(txtSalesTaxRate)) Then
lblTax = txtSale * txtSalesTaxRate
lblTotal = txtSale + lblTax
Else
MsgBox("Please enter valid numbers, thank you!")
End If
End Sub
End Class
If you need me to give you the full layout of my application, do ask.
I can see multiple problems in your code. I will explain how the "vs" works.The textboxes that you've made are controls.You can't just take their value so simple.They have many property and one of them is .text which allows you to take the value inside them.Another mistake you've made is what you've tried to do with the textboxes.What you type in those textboxes is ..well text. The program can't tell if the value inside is a number or just text. You must convert that value in a number using Cint.So you're code will look like this:
Public Class Form1
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If (IsNumeric(txtSale.text) And IsNumeric(txtSalesTaxRate.text)) Then
lblTax.text= cint(txtSale.text) * cint(txtSalesTaxRate.text)
lblTotal.text= cint(txtSale.text) + cint(lblTax.text)
Else
MsgBox("Please enter valid numbers, thank you!")
End If
End Sub
End Class
What cint does is to convert every type of data to integer. Also the .text property lets you to set the value of the control (in our case the label caption)
I tried Electric-web's code and ran into a problem with the output. I changed "CInt" to "CDbl" and the tax calculator worked.
Public Class Form1
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If (IsNumeric(txtSale.text) And IsNumeric(txtSalesTaxRate.text)) Then
lblTax.text= cdbl(txtSale.text) * cdbl(txtSalesTaxRate.text)
lblTotal.text= cdbl(txtSale.text) + cdbl(lblTax.text)
Else
MsgBox("Please enter valid numbers, thank you!")
End If
End Sub
End Class
Related
In to my Windows App Form, I have two text boxes and I would like the User to give me only Numbers And if the user gives me letters like for example (hello) when he executes the button I would like to inform the user through a window Validation message " Please enter numerical values ". How can I write it through Basic code?
You may simply use the function IsNumeric() to achieve it.
Look at the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
Else
MsgBox("Please input numbers only!")
End If
End Sub
Output of the form:
TextBox2 verification:
Enjoy!
You can check text box value with help of regex or isnumeric function. you can validate input at leave event of textbox and use error provider control to show warning message.
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
If Not Regex.IsMatch(TextBox1.Text, "\d+") Then
ErrorProvider1.SetError(TextBox1, "only numerics are allowed")
Else
ErrorProvider1.Clear()
End If
End Sub
End Class
please am working on an Annual Crop Cashflow Calculator like an Excel
i have 3 text boxes.
textbox22 textbox31 and textbox71
if i multiply textbox22 by textbox31 it shows in textbox71
but the problem is i need an event jus like excel after calculating and user goes back to empty either textbox31 or textbox22 the answer in texbox71 should be empty...
but with what i have here d previous answer will still be there until user inputs another entry
solution please
below is my code
Private Sub TextBox71_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox22.TextChanged, TextBox31.TextChanged
If String.IsNullOrEmpty(TextBox22.Text) OrElse String.IsNullOrEmpty(TextBox31.Text) Then Exit Sub
If Not IsNumeric(TextBox22.Text) OrElse Not IsNumeric(TextBox31.Text) Then Exit Sub
TextBox71.Text = CDbl(TextBox22.Text) * CDbl(TextBox31.Text)
End Sub
You can add code to the Enter event that runs whenever the user enters a TextBox.
Private Sub MyTextBox_Enter(sender as object, e as EventArgs)
MyTextBox.Clear
SomeOtherTextBox.Clear
End Sub
I am new to Visual Basic. I have installed Microsoft Visual Studio 2010. Created a new Windows Form Application. As an example, I made a simple program which will ask the end user to input 2 numbers and allow them to either add them or subtract the second number from the first one and display the output in a Textbox.
Now, I added another Subroutine which would be executed automatically when the Windows Form loads. This would calculate the width of the output Textbox and the Form Width and display at the bottom.
This is how the code looks like right now:
Public Class Form1
' Run this Subroutine initially to display the Form and Text box width
Private Sub Form_Load()
Label5.Text = TextBox3.Width
Label7.Text = Me.Width
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
TextBox3.Text = a + b
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
TextBox3.Text = a - b
End Sub
End Class
While everything works correctly for the addition and subtraction, it does not display the Form and output Textbox width in the Windows Form.
I think, Form_Load() is not executing properly.
I also tried, Form_Activate() but that did not work either.
Once I am able to do this, I would like to extend this concept to resize the output Textbox along with the Form resize. However, for the purpose of understanding I wanted to see if I can execute Form_Load() successfully.
Thanks.
Form_Load doesn’t execute. For now, it’s just any other method. In order to tell VB to make this method handle the Load event, you need to tell it so:
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Loasd
Label5.Text = TextBox3.Width
Label7.Text = Me.Width
End Sub
(And add the required parameters for the event.)
A few other remarks:
Ensure that Option Strict On is enabled in your project options at all times. This will make the compiler much stricter with your code and flag more errors. This is a good thing since these errors are potential bugs. In particular, your code is very lax with conversions between different data types, these should be made explicit.
Initialise variables when you declare them, don’t assign a value in a separate statement. That is, write this:
Dim a As Integer = Integer.Parse(TextBox1.Text)
(Explicit conversion added as well.)
If you want to make a control fill the form, you can just set its Dock property appropriately in the forms editor, instead of having to program this manually.
You need to add the Handle so the app executes it automatically:
Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
'...
End Sub
I have some code where I am trying to use variables in a tabpage. The first tabpage only has one text box for user entry (miles.text) and a button to do a calculation: traveltime = mileage/speed. The value from miles.text is stored into a variable called mileage while the speed used is stored in a variable called speed (me.speedtextbox.text).
Ordinarily, doing val(variable.text) works like a charm and it's not doing it in this case. When the user enters 100 for the mileage, it should be divided by 65 (the number in the database) and, therefore, the answer should be 1.53 hours. In my case, I'm getting "infinity" and whenever I do anything else with the variable, I get "when casting from a number, the value must be a number less than infinity." But it is! It's only 65 and I double-checked that the dataset said that too, which it does. Not sure why I am getting this error...thank you!
Public Class Form1
Private Property Traveltime As Decimal
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'fooDataSet.testdata' table. You can move, or remove it, as needed.
Me.TestdataTableAdapter.Fill(Me.foouDataSet.testdata)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mileage As Integer
Dim speed As Integer
mileage = Val(miles.Text)
speed = Val(Me.SpeedTextBox.Text)
traveltime = mileage / speed
txttraveltime.text = Traveltime.ToString
End Sub
Private Sub txtrate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttraveltime.TextChanged
End Sub
End Class
So I did a test program where it did only one thing and that was to simply read one data column in a one row database and store it to a local variable and multiply it by 1.60 except now I am getting "reference to a non-shared member requires an object reference" and it doesn't seem to recognize Me.Speed when I declare it. What am I doing wrong?
Public Class Form1
Dim Speed As Object
Dim Me.Speed As New Speed
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Speed = CDec(fooDataSet.testdataRow.Item("speed"))*1.60
Speedtextbox.text = Me.Speed.tostring
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'fooDataSet.testdata' table. You can move, or remove it, as needed.
Me.TestdataTableAdapter.Fill(Me.fooDataSet.testdata)
End Sub
End Class
Before you do anything else, you should do the following:
Open the project's properties (right-click on the Project, then select Properties)
Click on the Compile tab (left-hand side)
Select All Configurations from the dropdown menu
Select On from the Option Explicit menu.
Select On from the Option Strict menu.
Save the project
This will more than likely cause a lot of errors to be displayed, but fixing these errors will substantially improve your application's health.
Now, that that is done, the following code will fix the problems in the button click:
Dim mileage As Integer
Dim speed As Integer
If IsNumeric(Me.Miles.Text) Then
mileage = CInt(Me.Miles.Text)
End If
If IsNumeric(Me.SpeedTextBox.Text) Then
speed = CInt(Me.SpeedTextBox.Text)
End If
If speed <> 0 Then
Traveltime = CDec(mileage / speed)
Else
Traveltime = 0
End If
txtTravelTime.Text = Traveltime.ToString
However, the code as you have it will produce correct results, so there must be something else amiss. Try the above first and if there are still issues, you can update your question with the details.
I would implement the calculation in a separate class and then use object-binding. Here is how the travel time calculator would look like:
Imports System.ComponentModel
Public Class TraveltimeCalculator
Implements INotifyPropertyChanged
Private _miles As Double
Public Property Miles() As Double
Get
Return _miles
End Get
Set(ByVal value As Double)
If _miles <> value Then
_miles = value
OnPropertyChanged("Miles")
OnPropertyChanged("Traveltime")
End If
End Set
End Property
Private _speed As Double
Public Property Speed() As Double
Get
Return _speed
End Get
Set(ByVal value As Double)
If _speed <> value Then
_speed = value
OnPropertyChanged("Speed")
OnPropertyChanged("Traveltime")
End If
End Set
End Property
Public ReadOnly Property Traveltime() As Double
Get
Return If(_speed = 0.0, 0.0, _miles / _speed)
End Get
End Property
#Region "INotifyPropertyChanged Members"
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Private Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
#End Region
End Class
In Visual Studio, add a data source in the Data Sources panel. Choose "Object" and then select the TraveltimeCalculator (it has to be compiled, before you can do that). Now you can drag the speed, mileage and travel time fields from the data sources panel to your form. All the wire-up will happen automatically. VS automatically inserts a BindingSource and a navigator into your form. You will not need the navigator and can safely remove it. The only thing you still have to do is to add the following code in the form load event handler or in the form constructor:
Private Sub frmTravelTime_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TraveltimeCalculatorBindingSource.DataSource = New TraveltimeCalculator()
End Sub
When you enter speeds and mileages, the travel time textbox will automatically be updated. Non-numeric entries will automatically be rejected and all the text-number conversions happen automatically.
I discovered what the problem was.
To store a field from a one-line database to a local variable for calculations, apparently it has to happen in the form1_load event, after the dataadapter fill statement, like so:
Me.TestdataTableAdapter.Fill(Me.foouDataSet.testdata)
speed = Me.fooDataSet.testdata(0).speed
and just DIM speed as Decimal after the Public Class line. The same could be done for any other field you want to work with in a similar kind of single datarow:
yourvarname = Me.yourdatasetname.yourtablename(0).the_database_field_you_want_to_fetch
(Wow! Did I just write something textbooky? LOL)
Then, after the button click, to do a calculation, it is:
traveltime = CDec(miles.Text/ speed)
txttraveltime.Text = traveltime.ToString
making sure to DIM traveltime as Decimal.
Works! The problem was the (0) to indicate row 0 (because it's only one row.) Thank you everyone for your help, especially Competent_Tech. I learned something and I'm happy that I could get back to you guys and share.
I have to make a listbox with a few(8) names in it & double clicking on a name in the listbox will removed the name from it.
I have already add the names into the form using the listbox.items.add method & would display the names in it.
Then I enter the coding for 8 names in double_click procedure(listbox) using the "listbox.items.remove" method.
However, when i try double clicking on a name in the listbox, it would remove all the names instead.
What coding do i need? help appreciated!
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Clear()
ListBox1.Items.Add("1")
ListBox1.Items.Add("2")
ListBox1.Items.Add("3")
ListBox1.Items.Add("4")
ListBox1.Items.Add("5")
ListBox1.Items.Add("6")
ListBox1.Items.Add("7")
ListBox1.Items.Add("8")
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Dim i As Integer = ListBox1.SelectedIndex
If i >= 0 And i < ListBox1.Items.Count Then
ListBox1.Items.RemoveAt(i)
End If
End Sub
End Class
if you are looking at dynamic deletion of items, i think you should check out Jquery,Ajax,DOM
there are a couple of nice tutorials that would help you with that. i just came across this one and found it interesting
http://www.satya-weblog.com/2010/02/add-input-fields-dynamically-to-form-using-javascript.html