I am working on a calculator project, done with 2 operand but struggling to code and solve the 3 operand calc. The problem is when I do for example, 10+10= (answer 20) works fine but 10+10+10= (also answer 20). Seems it forget the 1st variable.
Below is my code on vb2010, please advise if I miss something. Thank you.
Dim first As Double
Dim second As Double
Dim third As Double
Dim answer As Double
Dim process As String
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
first = Val(TextBox1.Text)
TextBox1.Text = ""
formula.Text = first & "x"
process = "*"
End Sub
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
first = Val(TextBox1.Text)
TextBox1.Text = ""
formula.Text = first & "+"
process = "+"
End Sub
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
first = "0"
second = "0"
answer = "0"
formula.Text = ""
TextBox1.Text = ""
End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
If process = "+" Then
second = Val(TextBox1.Text)
formula.Text = first & "+" & second & "="
answer = first + second
TextBox1.Text = answer
End If
If process = "*" Then
second = Val(TextBox1.Text)
formula.Text = first & "x" & second & "="
answer = first * second
TextBox1.Text = answer
End If
If process = "/" Then
second = Val(TextBox1.Text)
formula.Text = first & "÷" & second & "="
answer = first / second
TextBox1.Text = answer
End If
If process = "-" Then
second = Val(TextBox1.Text)
formula.Text = first & "-" & second & "="
answer = first - second
TextBox1.Text = ""
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
first = Val(TextBox1.Text)
TextBox1.Text = ""
formula.Text = first & "-"
process = "-"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
first = Val(TextBox1.Text)
TextBox1.Text = ""
formula.Text = first & "÷"
process = "/"
End Sub
Related
I already have a code but I do not know how to add the MDAS function. Here is my code:
Public Class Form1
'variables-----------------------------------------------------------------------------
Dim inp As Double = 0
Dim oprtn As String
Dim expr As Boolean = False
'numbers and decimal input-----------------------------------------------------------------------------
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Number1.Click, Number9.Click, Number8.Click, Number7.Click, Number6.Click, Number5.Click, Number4.Click, Number3.Click, Number2.Click, Number0.Click, Period.Click
Dim num As Button = sender
If ((TextBox1.Text = "0") Or (expr)) Then
TextBox1.Clear()
TextBox1.Text = num.Text
expr = False
ElseIf (num.Text = ".") Then
If (Not TextBox1.Text.Contains(".")) Then
TextBox1.Text = TextBox1.Text + num.Text
End If
Else
TextBox1.Text = TextBox1.Text + num.Text
End If
End Sub
'operators button-----------------------------------------------------------------------------
Private Sub oprtrs(sender As Object, e As EventArgs) Handles PlusSign.Click, MinusSign.Click, MultiplySign.Click, DivideSign.Click
Dim operations As Button = sender
If (inp <> 0) Then
Equals.PerformClick()
expr = True
oprtn = operations.Text
Label1.Text = inp & " " & oprtn
Else
oprtn = operations.Text
inp = Double.Parse(TextBox1.Text)
expr = True
Label1.Text = inp & " " & oprtn
End If
End Sub
'equals button-----------------------------------------------------------------------------
Private Sub Equal_Click(sender As Object, e As EventArgs) Handles Equals.Click
Label1.Text = ""
Select Case oprtn
Case "+"
TextBox1.Text = (inp + Double.Parse(TextBox1.Text)).ToString()
Case "-"
TextBox1.Text = (inp - Double.Parse(TextBox1.Text)).ToString()
Case "*"
TextBox1.Text = (inp * Double.Parse(TextBox1.Text)).ToString()
Case "/"
TextBox1.Text = (inp / Double.Parse(TextBox1.Text)).ToString()
End Select
inp = Double.Parse(TextBox1.Text)
oprtn = ""
End Sub
'other buttons-----------------------------------------------------------------------------
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Backspace.Click
If TextBox1.Text < " " Then
TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1 + 1)
Else
TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1)
End If
End Sub
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Clear.Click
TextBox1.Text = "0"
End Sub
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles ClearEverything.Click
TextBox1.Text = "0"
Label1.Text = ""
End Sub
Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Prcent.Click
Dim percent As Double
percent = Convert.ToDouble(TextBox1.Text) / Convert.ToDouble(100)
TextBox1.Text = System.Convert.ToString(percent)
End Sub
Private Sub Button21_Click(sender As Object, e As EventArgs) Handles SqrRoot.Click
Dim squared As Double
squared = Math.Sqrt(TextBox1.Text)
TextBox1.Text = System.Convert.ToString(squared)
Label1.Text = "Sqr(" & System.Convert.ToString(squared) & ")"
End Sub
Private Sub Button22_Click(sender As Object, e As EventArgs) Handles RaiseTwo.Click
Dim expo2 As Double
expo2 = Convert.ToDouble(TextBox1.Text) * Convert.ToDouble(TextBox1.Text)
TextBox1.Text = System.Convert.ToString(expo2)
End Sub
Private Sub Button23_Click(sender As Object, e As EventArgs) Handles RaiseThree.Click
Dim expo3 As Double
expo3 = Convert.ToDouble(TextBox1.Text) * Convert.ToDouble(TextBox1.Text) * Convert.ToDouble(TextBox1.Text)
TextBox1.Text = System.Convert.ToString(expo3)
End Sub
Private Sub Button24_Click(sender As Object, e As EventArgs) Handles OneOver.Click
Dim ov1 As Double
ov1 = Convert.ToDouble(1.0 / Convert.ToDouble(TextBox1.Text))
TextBox1.Text = System.Convert.ToString(ov1)
End Sub
End Class
Thats pretty much all I have done, it works as intended, BUT it does not follow MDAS which prioritizes Multiplication first followed by Division then Addition then Subtraction. My design goes like this:
I started with the interface first then code, I haven't really designed it yet. This is just to give you an idea what is already coded or included in the code. Every button functions as should be.
This is what David is talking about.
There is very little code needed.
Public Class ExpressionCalculator
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles ButtonPlus.Click, ButtonMultiply.Click, ButtonMinus.Click, ButtonDot.Click, ButtonDivide.Click, Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button10.Click, Button1.Click
Dim tb = DirectCast(sender, Button)
TextBox1.Text &= tb.Text
End Sub
Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click
Dim table = New DataTable()
Dim result = table.Compute(TextBox1.Text, Nothing)
TextBox1.Text = result.ToString
End Sub
End Class
As the title suggest, I need help on changing the code on an old project I'm planning on revamping for an upcoming one. I need to change the Financial.DDB to Financial.SLN without an error occuring concerning there being too many arguments. Also I think the Main_Form_Load contents should be replaced with lstUsefulLife.Items.Add("2") to ("20"). Can anyone help? Here's the code.
Public Class Main_Form
Private Sub Main_Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
i = 3
Do
lstUsefulLife.Items.Add(i.ToString())
i = i + 1
Loop While i <= 20
End Sub
Private Sub btnSchedule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSchedule.Click
Dim i As Integer
Dim amount As Double
txtSchedule.Text = ""
txtSchedule.Text = "Year" & vbTab & "Depreciation" & vbNewLine
Dim years As Integer
years = Convert.ToDouble(lstUsefulLife.SelectedItem)
i = 1
Do
amount = Financial.DDB(Convert.ToDouble(txtAsset.Text), Convert.ToDouble(txtSalvage.Text), years, i)
txtSchedule.Text = txtSchedule.Text & i & vbTab & amount.ToString("N2") & vbNewLine
i = i + 1
Loop While i <= years
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub
End Class
Here is my code in vb.net for automatically changing image using timer but code doesn't work...
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.BackgroundImage = Image.FromFile("C:\images\\" + Label1.Text + "1.jpg")
Dim i As Integer = Convert.ToInt32(Label1.Text)
i += 1
If (i > 4) Then
i = 1
End If
Label1.Text = i.ToString()
End Sub
Move the counter out to class level. Incorporating the comments from dbasnett as well. Also, shouldn't the label reflect the current image number? The way you have it the value in the Label is one "ahead" of the background image.
Try something more like:
Private i As Integer = 1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim FileName As String = "C:\images\" & i.ToString & ".jpg"
Try
Me.BackgroundImage = Image.FromFile(FileName)
Label1.Text = i.ToString()
i += 1
If (i > 4) Then
i = 1
End If
Catch ex As Exception
Debug.Print(FileName)
MessageBox.Show(FileName & vbCrLf & vbCrLf & ex.ToString, "Error Loading Image")
End Try
End Sub
Hi I am trying to make a program that has 6 checkboxes and when one is checked a label displays the actors take in average, but if more than one is checked the label will only show the highest average of the checked boxes.
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
SC = ((Val(425488741) + Val(555909803) + Val(868659354) + Val(966435555) + Val(720388023) + Val(617520987)) / 6)
If CheckBox1.Checked = True Then
Label3.Text = "Sean Connery $" & SC
Exit Sub
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
GL = 513445231
If CheckBox2.Checked = True Then
Label3.Text = "George Lazenby $" & GL
Exit Sub
End If
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
RM = ((Val(785677477) + (426826774) + (666367656) + (624527272) + (481005579) + (405873493) + (316186616)) / 7)
If CheckBox3.Checked = True Then
Label3.Text = "Roger Moore $" & RM
Exit Sub
End If
End Sub
Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
TD = ((Val(362876056) + (271586451)) / 2)
If CheckBox4.Checked = True Then
Label3.Text = "Timothy Dalton $" & TD
Exit Sub
End If
End Sub
Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
PB = ((Val(499954330) + (465588535) + (504705882) + (546490272)) / 4)
If CheckBox5.Checked = True Then
Label3.Text = "Pierce Brosnan $" & PB
Exit Sub
End If
End Sub
Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
DC = ((Val(640803677) + (586090727)) / 2)
If CheckBox6.Checked = True Then
Label3.Text = "Daniel Craig $" & DC
Exit Sub
End If
End Sub
In the future, when asking a question, please do not include all the unnecessary details. If understand your problem correctly, this is one possible simple solution:
Dim incomes = New Integer() {100, 2000, 500}
Dim names = New String() {"John", "Tim", "Allan"}
Private Sub CheckedListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
Dim maxIncome As Integer = 0
Dim name As String = ""
For i = 0 To incomes.Length - 1
If CheckedListBox1.GetItemChecked(i) And incomes(i) > maxIncome Then
maxIncome = incomes(i)
name = names(i)
End If
Next
Label1.Text = name & " $" & maxIncome
End Sub
For this code to work you need to create a CheckedListBox with 3 items in it's collection. However, this approach might lead to a very bad code once there are many values. In that case you should use a database or at least a structure like:
Structure People
Dim name As String
Dim income As Integer
Dim checkBox As CheckBox
End Structure
Also the is no need to write Exit Sub anywhere in your code, it does nothing.
Details: Saulius is correct in that you should use a database and that you should use a checkedlistbox instead to make it much easier. However, I will just assume you want to use separate checkboxes for whatever reason.
Solution:
Create an Actor Class
Public Class Actor
Property Name As String
Property TotalValue As Double
End Class
In your main form
Public Sub DisplayHighestPaidActor(ByVal actorName As String, ByVal isChecked As Boolean)
If isChecked Then
'Add the Actor
selectedActors.Add((From actor In allActors
Where actor.Name = actorName).FirstOrDefault())
'Order the Actors
selectedActors = (From actor In selectedActors
Order By actor.TotalValue Descending).ToList()
Else
'Remove the Actor
selectedActors.Remove((From actor In allActors
Where actor.Name = actorName).FirstOrDefault())
'Order the Actors
selectedActors = (From actor In selectedActors
Order By actor.TotalValue Descending).ToList()
End If
If (selectedActors.Count > 0) Then
'Display the highest value
lblHighestPaidActor.Text = selectedActors.Item(0).Name.ToString() _
+ " $" + selectedActors.Item(0).TotalValue.ToString()
Else
lblHighestPaidActor.Text = ""
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
DisplayHighestPaidActor("Sean Connery", CheckBox1.Checked)
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
DisplayHighestPaidActor("George Lazenby", CheckBox2.Checked)
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
DisplayHighestPaidActor("Roger Moore", CheckBox3.Checked)
End Sub
I have recently made some changes to one of the subs within a form of my vb project and I am now getting an error whilst attempting to load the form. I have little idea what start index or Parameter name are with relation to a form, so don't where to start looking to solve this issue. Here is the error message I get:
An error occurred creating the form. See Exception.InnerException for details. The error is: StartIndex cannot be less than zero.
Parameter name: startIndex
The Sub that I have made changes to is the last sub in the code below called TextBox1changed_textchanged. I have added all but the last 5 lines as to limit the characters that can be put into the text box. This new code is edited from another forum page so I assume it should work correctly, but I can't be sure as the form will no longer run.
Public Class frmAddQuantity
Private Sub frmFieldMaster_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
cmbStateRateSumRatio.Items.Clear()
cmbStateRateSumRatio.Items.Insert(0, "State")
cmbStateRateSumRatio.Items.Insert(1, "Rate")
cmbStateRateSumRatio.Items.Insert(2, "Sum")
cmbStateRateSumRatio.Items.Insert(3, "Ratio")
End Sub
Private Sub bttAddQUAtoDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttAddQUAtoDatabase.Click
AddQuantity("'" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & cmbStateRateSumRatio.Text & "'")
InitialiseAll()
frmFieldMaster.InitialiseNewParameter()
Me.Close()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
If TextBox2.Text <> "Enter SI Units" Then
If cmbStateRateSumRatio.SelectedIndex <> -1 Then
bttAddQUAtoDatabase.Enabled = True
End If
End If
End Sub
Private Sub cmbStateRateSumRatio_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbStateRateSumRatio.SelectedIndexChanged
If TextBox1.Text <> "Enter Quantity" Then
If TextBox2.Text <> "Enter SI Units" Then
bttAddQUAtoDatabase.Enabled = True
End If
End If
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyz1234567890^-*()."
Dim Text As String = TextBox2.Text
Dim Letter As String
Dim SelectionIndex As Integer = TextBox2.SelectionStart
Dim Change As Integer
Letter = TextBox2.Text.Substring(SelectionIndex - 1, 1)
If Letter = "/" Then
Text = Text.Replace(Letter, "^(-")
SelectionIndex = SelectionIndex - 1
End If
Letter = TextBox2.Text.Substring(SelectionIndex - 1, 1)
If charactersAllowed.Contains(Letter) = False Then
Text = Text.Replace(Letter, String.Empty)
Change = 1
End If
TextBox2.Text = Text
TextBox2.Select(SelectionIndex - Change, 0)
If TextBox1.Text <> "Enter Quantity" Then
If cmbStateRateSumRatio.SelectedIndex <> -1 Then
bttAddQUAtoDatabase.Enabled = True
End If
End If
End Sub
End Class
Tell the debugger to stop when the exception is thrown. Debug + Exceptions, tick the Thrown checkbox for CLR exceptions.
There's a good candidate in your snippet for this exception:
Letter = TextBox1.Text.Substring(SelectionIndex - 1, 1)
You'll need to deal with the possibility that the textbox is empty or the SelectionStart property is 0. Which will always bomb your code with "StartIndex cannot be less than zero".
Private Const AllowedChars = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890^-*()."
Private Sub TxtSIUnit_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSIUnit.KeyPress
If e.KeyChar >= " "c AndAlso Not AllowedChars.Contains(e.KeyChar) Then e.Handled = True
End Sub
Private Sub TxtSIUnit_KeyPress2(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSIUnit.KeyPress
Dim selectstart
If e.KeyChar = "/" Then
e.Handled = True
selectstart = txtSIUnit.SelectionStart
txtSIUnit.SelectedText = "*^-1"
txtSIUnit.Select(selectstart + 1, 0)
End If
End Sub
I have solved the issue above using this code. Instead of using the .textchanged handle I have used .keypress which stopped the issue I was having previously. The text in the textbox was changing as the form was loading, leading to a problem with the start index of the text. Using .keypress mean only user inputs let the code run, avoiding errors on loading.