Financial.DDB in Visual Basic - vba

I am trying to write a program that calculates the yearly depreciation cost with Financial.DDB function. I need to display the year and depreciation for that year in a listbox. Here is what I have so far:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Displays 1-20 in list box
For intUsefullife As Integer = 1 To 20
lstUsefullife.Items.Add(intUsefullife.ToString)
Next intUsefullife
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'set variables
Dim decAssetcost As Decimal
Dim intUsefullife As Integer
Dim decSalvage As Decimal
Dim decSchedule As Decimal
'parse text boxes
Integer.TryParse(lstUsefullife.SelectedItem.ToString, intUsefullife)
Decimal.TryParse(txtAssetcost.Text, decAssetcost)
Decimal.TryParse(txtSalvage.Text, decSalvage)
'converts decSchedule to strings
txtSchedule.Text = decSchedule.ToString
'Name Headers
txtSchedule.Text = "Year" & ControlChars.Tab & "Depreciation" & ControlChars.NewLine
'Calculates depreciation schedule
For intUsefullife = 1 To intUsefullife Step 1
txtSchedule.Text = txtSchedule.Text &
ControlChars.Tab & intUsefullife.ToString
For decSchedule = Financial.DDB(decAssetcost, decSalvage, intUsefullife, intUsefullife)
txtSchedule.Text = txtSchedule.Text &
ControlChars.NewLine
ControlChars.Tab & decSchedule.ToString("C2") & ControlChars.NewLine
Next decSchedule
Next intUsefullife
End Sub
Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
I have no idea where to go from here. All that happens when I hit debug and then calculate is year 1 and $0.00 under the depreciation amount. (the year does not display where I want it to either).

Related

.vb keeps giving 0 as total amount you need to pay

So i had to make a program where you can buy stuff when clicking on the buttons, when the amount totals more than 1k you can roll for a discount. This all goes well until I have to use a label to indicate the total amount you need to pay minus the discount. This label keeps staying 0, no matter what. Could anyone help?
Form1 for the "shop":
Public Class Form1
Dim i As Integer = 1
Dim totaalbedrag As Double
Public Sub Actie()
If Val(TextBox3.Text) > 1000 And i = 1 Then
If MsgBox("Wil je strijden voor korting? Zo niet zal deze pop-up niet meer komen dus maak je keuze!", vbYesNo) = vbYes Then
Me.Hide()
Form2.Show()
Else
i = 0
End If
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label6.Text = Val(Label6.Text) + 1
TextBox1.Text = Val(TextBox1.Text) + 250
TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Actie()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label7.Text = Val(Label7.Text) + 1
TextBox2.Text = Val(TextBox2.Text) + 300
TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Actie()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox4.Text = korting & " procent"
totaalbedrag = totaalbedrag - totaalbedrag * korting / 100
TextBox5.Text = Val(totaalbedrag)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MsgBox("Gefeliciteerd met de aankoop van totaal " & totaalbedrag & " euro!!", vbCritical)
End
End Sub
End Class
Form2 for the gambling:
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
korting = Label1.Text
MsgBox("Gefeliciteerd, je hebt " & korting & " procent korting gekregen!", vbCritical)
Me.Hide()
Form1.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = Int(Rnd() * 10)
End Sub
End Class
Module to have a value as a public one:
Module Module1
Public korting As Integer = 0
End Module
You basically should pay attention to this code of line since I think this is where all goes wrong:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox4.Text = korting & " procent"
totaalbedrag = totaalbedrag - totaalbedrag * korting / 100
TextBox5.Text = Val(totaalbedrag)
End Sub
I am not at all sure of the timer event in Form1. Every time it ticks the discount is reapplied. If the program runs long enouth totaalbedrag will be very small.
I used .ToString("C") or "c" to display the result as currency. Some of displays are probably not currency. Change to the appropriate format string. https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
For Random numbers please switch to the .net Random class.
Public Class Form1
Dim i As Integer = 1
Dim totaalbedrag As Decimal
Public Sub Actie()
Dim tb3Dec As Decimal
If Decimal.TryParse(TextBox3.Text, tb3Dec) AndAlso tb3Dec > 1000 AndAlso i = 1 Then
If MsgBox("Do you want to compete for a discount? If not, this pop-up will not come again so make your choice!", vbYesNo) = vbYes Then
Me.Hide()
Form2.Show()
Else
i = 0
End If
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox4.Text = korting & " procent"
totaalbedrag = totaalbedrag - totaalbedrag * korting / 100
TextBox5.Text = totaalbedrag.ToString("C")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim tb1Dec As Decimal
If Not Decimal.TryParse(TextBox1.Text, tb1Dec) Then
MessageBox.Show("Please enter a valid number in TextBox1.")
Exit Sub
End If
Dim tb2Dec As Decimal
If Not Decimal.TryParse(TextBox2.Text, tb2Dec) Then
MessageBox.Show("Please enter a valid number in TextBox2")
Exit Sub
End If
Label6.Text = (CDec(Label6.Text) + 1).ToString
TextBox3.Text = (tb1Dec + tb2Dec).ToString("c")
TextBox1.Text = (tb1Dec + 250).ToString("C")
Actie()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label7.Text = (CDec(Label7.Text) + 1).ToString("c")
Dim tb2Dec As Decimal
If Not Decimal.TryParse(TextBox2.Text, tb2Dec) Then
MessageBox.Show("Please enter a valid number in TextBox2")
Exit Sub
End If
Dim tb1Dec As Decimal
If Not Decimal.TryParse(TextBox1.Text, tb1Dec) Then
MessageBox.Show("Please enter a valid number in TextBox1")
Exit Sub
End If
TextBox2.Text = (tb2Dec + 300).ToString("C")
TextBox3.Text = (tb1Dec + tb2Dec).ToString("C")
Actie()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MsgBox("Gefeliciteerd met de aankoop van totaal " & totaalbedrag & " euro!!", vbCritical)
End
End Sub
End Class
Public Class Form2
Private rand As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
korting = CInt(Label1.Text)
MsgBox("Gefeliciteerd, je hebt " & korting & " procent korting gekregen!", vbCritical)
Me.Hide()
Form1.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = rand.Next(0, 101).ToString 'returns an integer 0 to 100
End Sub
End Class
This fixes the Option Strict violations but I have no idea it this works for you. Let us know which label is showing 0.

How to sort and get a desired number in an array

So, as the title says, I need to be able to sort an array of randomly generated numbers and also have the user choose the number they want to roll, say they want to roll a 4, they will input a 4 in a textbox and there will be a label somewhere on the form telling the user how many times that number showed up, I just can't seem to figure it out though, how to sort and how to get that probability, any help?
Here is my code so far:
Public Class Chcked
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Public Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerate.Click
' Initialize the random-number generator.
Dim strOutput As String
Dim intArray(Val(txtFirst.Text)) As Integer
Dim Counter As Integer
Dim Random As Integer
For Counter = 1 To Val(txtFirst.Text)
Randomize()
Random = Int(Rnd() * 6) + 1
intArray(txtFirst.Text) = Random
strOutput = intArray(txtFirst.Text)
txtSequence.Text = " " & strOutput & " " + txtSequence.Text & " "
Next Counter
End Sub
Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear.Click
txtSequence.Text = " "
End Sub
Private Sub Sort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sort.Click
End Sub
End Class

Depreciation Calculator modifications

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

Bring Window To Top/Active

I need some help with a small little program that I am making for a game. I can't get it to work so it switch to the game and then press the buttons that I want. It finds the window but just don't switch to it or make it active. Am I using the correct function?
Public Class Form1
Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Integer) As Integer
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Dim Hwnd As Long = BringWindowToTop("Aftermath v0.9.0 (build: Apr 15 2015 14:38:12) - final D3D9")
Dim Hnd As Integer = Win32.FindWindow(Nothing, "Aftermath v0.9.0 (build: Apr 15 2015 14:38:12) - final D3D9") & BringWindowToTop(Hnd)
If (Not Hnd = 0) Then
SendKeys.Send("{enter}")
SendKeys.Send("/ginvite " & Chr(34) & TextBox1.Text & Chr(34))
Else
MessageBox.Show("Error")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim curItem As String = ListBox1.SelectedItem.ToString()
SendKeys.Send("/ginvite " & Chr(34) & curItem & Chr(34))
SendKeys.Send("{enter}")
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim curItem As String = ListBox1.SelectedItem.ToString()
SendKeys.Send("/gaccept " & Chr(34) & curItem & Chr(34))
SendKeys.Send("{enter}")
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
For i As Integer = 0 To Me.ListBox1.Items.Count - 1
Me.ListBox1.SetSelected(i, True)
Next
'Dim curItems As String = ListBox1.SelectedItems.ToString()
'SendKeys.Send("/ginvite" & Chr(34) & curItems & Chr(34))
'SendKeys.Send("{enter}")
End Sub
End Class
I would also love if you could help me with my list box. I want it so it prints our each string with Chr(34) in front and behind it.
The code that I currently have (doesn't work):
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
For i As Integer = 0 To Me.ListBox1.Items.Count - 1
Me.ListBox1.SetSelected(i, True)
Next
'Dim curItems As String = ListBox1.SelectedItems.ToString()
'SendKeys.Send("/ginvite" & Chr(34) & curItems & Chr(34))
'SendKeys.Send("{enter}")
End Sub

How do I display the highest average of checkbox when multiple checkboxes checked in VB.NET?

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