Keeping it in comma order - vb.net

I am trying to get the value to stay in a comma order like 123,456,789 when subtracting the value. Here is the current code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim health As Integer
health = lblHPPlayer.Text
lblHPPlayer.Text = Val(health) - 1000
End Sub
'That is the value of what I wanna change*
lblHPPlayer.Text = "9,850,000"

Create a form-level property called Health. You can make it private. Now any time you need to update Heath then also update the text in lblHPPlayer. That way you never need to parse the text - it only ever just shows you the value of a proper integer property.
Something like this:
Private _health As Integer
Public Property Health() As Integer
Get
Return _health
End Get
Set(ByVal value As Integer)
_health = value
lblHPPlayer.Text = _health.ToString("N0")
End Set
End Property
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Health -= 1000
End Sub

Use the numeric format specifier.
lblHPPlayer.Text = (Val(health) - 1000).toString("N0")

Related

Visual basic empty text box throws exception

The code below is a program to calculate the BMI using text boxes. I am having an issue however that when I clear one of the text boxes it will throw an exception and freeze the program. I was wondering if anyone had an answer on how to prevent this. I already tried setting my variables to 0 and 1 to see if that was the issue but it does not appear to be.
Private Sub tboxWeight_TextChanged(sender As Object, e As EventArgs) Handles tboxWeight.TextChanged
Weight = 0
Weight = Convert.ToInt64(tboxWeight.Text)
End Sub
Private Sub tboxHFeet_TextChanged(sender As Object, e As EventArgs) Handles tboxHFeet.TextChanged
Height_feet = 0
Height_feet = Convert.ToInt64(tboxHFeet.Text)
Get_BMI(1)
End Sub
Private Sub tboxHInch_TextChanged(sender As Object, e As EventArgs) Handles tboxHInch.TextChanged
Height_Inches = 0
Height_Inches = Convert.ToInt64(tboxHInch.Text)
Get_BMI(1)
End Sub
Private Sub tboxAge_TextChanged(sender As Object, e As EventArgs) Handles tboxAge.TextChanged
Age = Convert.ToDouble(tboxAge.Text)
End Sub
Function Get_BMI(ByVal j As Integer) As Double
BMI = (Weight / (Height_Inches + (Height_feet * 12) ^ 2) * 703)
tboxBMI.Text = Convert.ToString(BMI)
Exit Function
End function
It is because you set a textbox into an integer field, so when the textbox is empty it will throw exception because the textbox doesn't contain a number.
Try using If else statement for each textboxes.
String.IsNullOrEmpty function will be sufficient.
Good/Best practice says, you need to validate the data before performing calculation i.e. Get_BMI(). Below code snippet will help you.
Dim textBoxValue As String
If Not String.IsNullOrEmpty(textBoxValue) Then
If IsNumeric(textBoxValue) Then
End If
End If

Cannot figure out how to display correct output (Code included)

I cannot seem to figure out how to display the correct output. When I run my query it is supposed to say "eBook" for the correct format. However, I can only get it to display the assigned number corresponding to the "eBook" format which is 1. I need it to display "eBook" instead of the number. It is probably a really easy fix but I am new to VB and just need another set of eyes and a helping hand.
Private Sub btnEBooks_Click(sender As Object, e As EventArgs) Handles btnEBooks.Click
Dim Q = From book In books
Let format = book.Format
Let formatName = GetFormatName(book.Format)
Where format = 1
Order By book.Title
Select book.Author, book.Format, book.Title, book.YearPublished
dgvOutput.DataSource = Q.ToList
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Function GetFormatName(FNum As Integer) As String
If FNum = 1 Then
Return "eBook"
End If
End Function

Case Statement not working with String Literals

Hi all I am trying to learn VB and am having trouble with some code I am using. I would like my program to output a specific number based on if a check box is checked using case statements but my code is not working.
Public Class frmBTPW
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btncalc.Click
Dim dblhdr As Double
Dim dblfdr As Double
Dim dbltdr As Double
dblhdr = 24
dblfdr = 35
dbltdr = 50
Select Case "Power Wash Rental"
Case "Half Day Rental"
If chkhd.Checked = True Then
txtrc.Text = "poop"
End If
Case "Full Day Rental"
If chkFD.Checked = True Then
txtrc.Text = dblfdr
End If
End Select
End Sub
Private Function Button1_Click() As CheckBox
Throw New NotImplementedException
End Function
End Class
Help would be greatly appreciated.My code isn't outputting anything in the text-box.
Beyond case statements, respectfully I think you should read up on the distinction between a literal value and a variable. "Power Wash Rental" is nothing more than a series of characters, AKA a string: (In this case "P" followed by "o" etc.) Likewise, "Half Day Rental" is a series of characters, "H" followed by "a" etc.)
"Power Wash Rental" is a literal string. So is ""Half Day Rental" and of course they will never match.
Whereas:
Dim A as string
A = TextBox1.text
Now, A is a variable. It is a string which contains whatever series of characters (text) is typed into the textbox.
This is a simple way to do it.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
chkhd.tag = 24 ' store values in the check boxes
chkfd.tag = 35 ' using the tag property
chktd.tag = 50 ' and later add up the values
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btncalc.Click
dim total as double = 0
total += IF(chkhd.checked, cdbl(chkhd.tag), 0)
total += IF(chkfd.checked, cdbl(chkfd.tag), 0)
total += IF(chktd.checked, cdbl(chktd.tag), 0)
msgbox(total)
End Sub
However, I think you might want radio buttons instead of checkboxes.
Checkboxes can all be checked. Radio buttons can only have one at a time.
This solution allows you to keep your price with the checkbox -- you could do this in the form designer instead of form load.
I would recommend reading up on Case Statements. Currently you will never get anywhere as your using a string to what, nothing. You also do not need a case for this... Also if the first condition is true and the last one is as well, the last one win's for setting the text, didn't know if you had this there for a reason or not?
If chkhd.Checked = True Then
txtrc.Text = "poop"
End If
If chkFD.Checked = True Then
txtrc.Text = dblfdr
End If
As others have stated your Case statement isn't working because you are using string literals to compare "Power Wash Rental" to "Half Day Rental" which will always be false. Plutonix was also correct in saying that a ComboBox for the rental duration should be used. The only reason not to be is if you were calculating cumulative rental days/amounts; however in that situation you should be using some sort of NumericUpDown for your multiplier against a time duration.
Here is an example that should help you get started. You could make the structure into a type of keyed collection or make it a wrapper class for a dictionary object which would make be easier to use in code. The following may not be exactly plug-and-play with your project, however it should help give you some ideas on how to handle the situation.
Option Strict On
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.ComboBox1.Items.AddRange({PowerWashRentals.halfDayText, PowerWashRentals.FullDayText, PowerWashRentals.TwoDayText})
AddHandler ComboBox1.SelectedValueChanged, AddressOf Me.ComboBox1_SelectedChanged
End Sub
Private Sub ComboBox1_SelectedChanged(sender As Object, e As EventArgs)
Dim cBox As ComboBox = DirectCast(sender, ComboBox)
Select Case cBox.SelectedItem.ToString
Case PowerWashRentals.halfDayText
Label1.Text = PowerWashRentals.HalfDayPrice.ToString
Case PowerWashRentals.FullDayText
Label1.Text = PowerWashRentals.FullDayPrice.ToString
Case PowerWashRentals.TwoDayText
Label1.Text = PowerWashRentals.TwoDayPrice.ToString
End Select
End Sub
End Class
Public Structure PowerWashRentals
Public Const HalfDayPrice As Double = 24
Public Const FullDayPrice As Double = 35
Public Const TwoDayPrice As Double = 50
Public Const halfDayText As String = "Half Day Rental"
Public Const FullDayText As String = "Full Day Rental"
Public Const TwoDayText As String = "Two Day Rental"
End Structure

parse rounds to whole

I am trying to parse a textbox.text with a input value of 15.75. Here is all the code.
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim lnVendorNo, lnInHouseID, lnInventoryPackID As Integer
Dim lcVendProdID, lcVendProdDesc, lcDeliverPack As String
Dim lnDelivPackCost, lnDelivPackCost2 As Short
Integer.TryParse(txtVendorNo.Text, lnVendorNo)
lcVendProdID = txtVendProdID.Text
Integer.TryParse(txtInHouseID.Text, lnInHouseID)
lcVendProdDesc = txtVendProdDesc.Text
lcDeliverPack = txtDeliverPack.Text
txtDeliverPackCost.Text = "15.75"
Decimal.TryParse(txtDeliverPackCost.Text, lnDelivPackCost)
' Value of lnDelivPackCost in watch window is 16 and type is short
lnDelivPackCost2 = Double.Parse(txtDeliverPackCost.Text)
' value of lnDelivPackCost2 in watch window is 16 and type is short
I have another sub with the following code that works just fine.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim value As String
Dim number As Decimal
Dim lnDelivPackCost As Decimal
' Parse a floating-point value with a thousands separator.
value = "1643.57"
If Decimal.TryParse(value, number) Then
Console.WriteLine(number) ' Value of number in watch = 1643.57
End If
txtDeliverPackCost.Text = "15.75"
Decimal.TryParse(txtDeliverPackCost.Text, lnDelivPackCost) ' Value on lnDelivPackCost in watch = 15.75D
End Sub
Can anyone tell me why the parses work on one sub and not the other sub. Is it because of parsing to integers earlier in the sub. I am going bonkers trying to figure this out. Any help would be appreciated.
Larry

Cannot get decimal entries from array to appear in listbox

In my VB class we've been asked to set up an array which is populated by user entries. These entries are decimal type, meant to be gas prices. There are twelve, one per month. The entries are supposed to show up one at a time, as they are entered and processed, in a list box.
I've got them showing up but they're not showing up properly. Instead of 4.55 (or whatever), the entries show up as "Decimal[] Array" (minus the quotation marks, of course).
How can I get the entries to show up properly? The code is below and it's very incomplete as I'm only about a third of the way into the project, so don't sweat that unless you see some horrible problem sticking out like a sore thumb.
Public Class GasPrices
Dim prices(11) As Decimal
Private Sub EnterButton_Click(sender As Object, e As EventArgs) Handles EnterButton.Click
prices(PriceList.Items.Count) = Convert.ToDecimal(PriceText.Text)
PriceText.Clear()
For i = 0 To 11
prices(i) = i
Next i
PriceList.Items.Add(prices)
End Sub
Private Sub PriceList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles PriceList.SelectedIndexChanged
PriceList.Items.Clear()
PriceList.Items.Add(prices)
End Sub
End Class
You're adding the entire array as one "entry". You'd need to add each individual entry instead using syntax like you've got where you access prices(i) in the loop.
Public Class GasPrices
Private prices(11) As Decimal
Private Sub EnterButton_Click(sender As Object, e As EventArgs) Handles EnterButton.Click
If PriceList.Items.Count < 12 Then
Dim price As Decimal
If Decimal.TryParse(PriceText.Text, System.Globalization.NumberStyles.Currency, Nothing, price) Then
prices(PriceList.Items.Count) = price
PriceList.Items.Add(price)
PriceText.Clear()
PriceText.Focus()
Else
MessageBox.Show("Invalid Price!")
End If
Else
MessageBox.Show("12 entries have already been entered!")
End If
End Sub
Private Sub PriceList_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles PriceList.SelectedIndexChanged
If PriceList.SelectedIndex <> -1 Then
Label1.Text = PriceList.SelectedItem
End If
End Sub
End Class
It's quite simple. With Add you add the array itself as single object to the listbox. The listbox's default behavior is to show object.ToString for each entry. And since the object is an array of decimals, you get the unwanted output.
If you want to add the elements of an array to a list, listbox, ... you use the AddRange method instead.