Textbox and currency formatting - vb.net

I'm trying to format two specific text boxes to show up as Sq. ft. (txt.Squareft.Text) and US currency (txtTotalprice.Text) after a calculation has been made in Visual Studio 2019 as a Windows Form Application and using visual basic code. I am using .NET framework v4.7.2 and utilizing Windows 10. The way it runs now, the numbers that show up in the textboxes are just numbers without the added Sq. ft. at the end and no currency formatting. I will also add that I am very new to VB and programming in general. Any help or suggestions?
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'Variables
Dim decTotalprice As Decimal
Dim decLength As Decimal
Dim decWidth As Decimal
Dim decPrice As Decimal
Dim decSquareft As Decimal
Decimal.TryParse(txtLength.Text, decLength)
Decimal.TryParse(txtWidth.Text, decWidth)
Decimal.TryParse(txtPrice.Text, decPrice)
Decimal.TryParse(txtSquareft.Text, decSquareft)
txtTotalprice.Text = decTotalprice.ToString("C2")
txtSquareft.Text = decSquareft.ToString("N2") & " Sq. ft."
' Calculate the square feet and total price
txtSquareft.Text = txtLength.Text * txtWidth.Text
txtTotalprice.Text = txtPrice.Text * txtSquareft.Text
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
' Clears all the text fields with button click
txtLength.Clear()
txtWidth.Clear()
txtPrice.Clear()
txtSquareft.Clear()
txtTotalprice.Clear()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
' Exits the form
Me.Close()
End Sub
End Class

There are two primary issues here and I feel like I bring them up at least ten times a day. Firstly, you are trying to write code without knowing what that code has to do. You've considered the end result but not the steps to get there. If you had done that then it would be obvious that your code doesn't what it's supposed to. Secondly, you clearly haven't debugged your code, which is the first thing anyone should do when they don't get the expected result. That would also let you see that your code doesn't make sense IF you considered what each line is supposed to be doing as it does it.
If this was a manual task, you would get the input from the user, perform the calculation, then display the result. Is that what you're doing here? No, it is not. First you get the user input. That's a start, but you're doing it wrong. As it stands, you would end with zero for any invalid input but you're just ignoring that. The next thing you do is display the formatted output that you haven't even calculated yet. If you had debugged, you'd have seen that both decTotalprice and decSquareft are zero at that point. You finally do the calculations, but with the raw text input instead of the numbers you already parsed, and then you display the results unformatted. You've even got a comment in your code that says that you're doing the calculation AFTER you've displayed the formatted output.
Stop writing code and think about what the required steps are to get to your desired result. Parse the user input, perform the calculations with the numeric data and not the unparsed text, then display those results with formatting. Once you have a clear idea of what you have to do AND tested that manually, then you can write code to implement that algorithm, rather than some vague idea in your head that involves a final result and little else.
You're certainly not the only person who makes these mistakes but they are elementary mistakes. They happen partly because of bad teaching in some cases, but they also happen because everyone wants to jump into the part that is sexy and fun, i.e. writing the code, but they don't want to do the harder but just as important part of considering what the code actually has to do. When they don't get the expected result, they throw their hands up without ever really having tried to fix it. If you haven't tried to understand what the code has to do, you can't have tried to make it do that.

Related

How to update a group of combo boxes using Loops

I have a form with combo boxes (cmbPort#) to select up to 8 serial ports. I want to first clear the item list for each, populate them with currently available system ports, and then add the option "Off" to each list. Finally, to set each combo box according to defaults saved in string spName(). I created a GroupBox (gBox1) and dragged each cmbPort onto it but I'm not sure how to reference the controls on it. I'm using VB 2015.
Can you help with VB.NET code to use loops ("For Each" or similar) to do this more efficiently?
Private Sub frmProp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmbPort1.Items.Clear()
...
cmbPort8.Items.Clear()
For Each sp As String In My.Computer.Ports.SerialPortNames
cmbPort1.Items.Add(sp)
...
cmbPort8.Items.Add(sp)
Next
cmbPort1.Items.Add("Off")
...
cmbPort8.Items.Add("Off")
cmbPort1.Text = spName(1)
...
cmbPort8.Text = spName(8)
End Sub
Loops are an incredibly useful tool to master. I pitched here some code so you can get the idea, but I was working out of IDE so you might have to apply some fixes to this code to make it work as you want it to.
The main idea is that you shouldn't have to write a line more than once. If you multiply the same operation on several lines of code, you create potential problems for the future. Loops and subs are really helpful to prevent this kind of issues.
Private Sub frmProp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'This is for later
Dim cmbIndex As Integer = 1
'You basically do the same operations for every Combobox in your list, son only one loop needed
For Each cmbPort As ComboBox In {cmbPort1, cmbPort2, cmbPort3 [...] cmbPort8} 'this is a way to declare an array
'Clear
cmbPort.Items.Clear()
'Add SerialPortNames
For Each sp As String In My.Computer.Ports.SerialPortNames
cmbPort.Items.Add(sp)
Next
'Add "Off"
cmbPort.Items.Add("Off")
'This last one is a little bit trickier because you want to match numbers
'This is the place where you get errors when something doesn't go as planned
'If you need to keep it inside the loop here's a way to achieve that, but honestly I would't do that
'Instead I would suggest that you take this part out of the loop and do it manually
If spName(cmbIndex) IsNot Nothing Then cmbPort.Text = spName(cmbIndex)
cmbIndex += 1
Next
End Sub
You shouldn't consider efficiency into this equation, as this operation will not be called all the time, only on load. I mean: you should always do things in the best way you can, but optimization is sometimes the enemy of good, readable code.

Saving Database issue

So basically, I believe I am using the correct code yet the database will still not update. It will work for the current session, however, when I stop and restart the program, it appears that the data has not been updated in the database.
The really interesting part is that I am using the same method to update the database elsewhere, which when used and session restarted, the database has been updated.
p.s. I also have the same adapters and binding sources set up etc on both forms
I am so confused, help pls
Code that I believe is correct but is not working: (updating on another form so I have one place where all forms update hence FRMMain. etc)
Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
Dim CurrentPoints As Integer
Dim UpdatedPoints As Integer
CurrentPoints = FRMMain.MyDBDataSet.Tables("TBLPupil").Rows(looopcount)(15)
UpdatedPoints = CurrentPoints + stfPoints
FRMMain.MyDBDataSet.Tables("TBLPupil").Rows(looopcount)(15) = UpdatedPoints
FRMMain.TBLPupilTableAdapter.Update(MyDBDataSet.TBLPupil)
FRMMain.TBLPupilTableAdapter.Fill(MyDBDataSet.TBLPupil)
End Sub
Code that I am using in another form that that DOES work:
Private Sub BtnYes_Click(sender As Object, e As EventArgs) Handles BtnYes.Click
Dim Points As Integer = FRMPupil.Pointss
Dim Cost As Integer = FRMPupil.RewardCost
Points = Points - Cost
FRMPupil.LePoints = Points
MyDBDataSet.Tables("TBLPupil").Rows(FRMLogin.DBLocation)(15) = Points
FRMMain.TBLPupilTableAdapter.Update(MyDBDataSet.TBLPupil)
FRMMain.TBLPupilTableAdapter.Fill(MyDBDataSet.TBLPupil)
Me.Hide()
End Sub
My code is correct but is not working.
No, if it is not working, then it is not correct!
There are different things you can do: DRY, Dont Repeat Yourself. You are repeating the code for updating points at several places in your code. This is error prone. Write it once and re-use it, e.g. by applying the the Repository Pattern. It makes it easier to detect errors and correct them. It allows you to re-use code that has already been tested in other scenarios (on another form).
Debug, debug, debug. Place breakpoints in the not working methods and see what happens. Do all the variables have the expected values? E.g., does looopcount have the same value as FRMLogin.DBLocation? There must be a difference somewhere. See: Navigating through Code with the Debugger or the more recent article Debug your Hello World application with Visual Studio 2017.

Simplify one-type syntaxis in vb.net

I have the next piece of code:
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged,
TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged, TextBox7.TextChanged,
TextBox8.TextChanged, TextBox9.TextChanged, TextBox10.TextChanged, TextBox13.TextChanged, TextBox14.TextChanged,
TextBox15.TextChanged, TextBox18.TextChanged, TextBox19.TextChanged, TextBox20.TextChanged
Double.TryParse(TextBox1.Text, Component.Methane.Mole)
Double.TryParse(TextBox2.Text, Component.Ethane.Mole)
Double.TryParse(TextBox3.Text, Component.Propane.Mole)
Double.TryParse(TextBox4.Text, Component.iButane.Mole)
Double.TryParse(TextBox5.Text, Component.nButane.Mole)
Double.TryParse(TextBox6.Text, Component.neoPentane.Mole)
Double.TryParse(TextBox7.Text, Component.nHexane.Mole)
Double.TryParse(TextBox8.Text, Component.nHeptane.Mole)
Double.TryParse(TextBox9.Text, Component.N2.Mole)
Double.TryParse(TextBox10.Text, Component.CO2.Mole)
Double.TryParse(TextBox13.Text, Component.iPentane.Mole)
Double.TryParse(TextBox14.Text, Component.nPentane.Mole)
Double.TryParse(TextBox15.Text, Compress.Avol)
Double.TryParse(TextBox18.Text, Compress.AF)
Double.TryParse(TextBox19.Text, Compress.AP)
Double.TryParse(TextBox20.Text, Compress.AT)
End Sub
Is there any possibility to make this procedure more compact, beatiful and smarter ? Use some kind of control, loop or something else? It would be greate to make it shorter and avoid typing. I am quite new in programming, any help is very appreciated!
Thanks in advance!
If your range of textboxes should only contain numbers, create a new Control that inherits from Textbox and alter the Textbox to only accept valid numbers. See an example here:
https://www.tigraine.at/2008/10/28/decimaltextbox-for-windows-forms/
The other thing I would point you is to look into Databinding. Databinding is something where a WinForm or WPF Control will "push" the value contained within the Control back to a property on an object. Windows has made a nice and quick write up on it here:
https://msdn.microsoft.com/en-us/library/2b4be09b.aspx
Finally, for "beautiful code" give things meaningful names. Currently your textbox have the default name and this can result in some very confusing problems later on in life. A quick and easy naming convention I follow is , for example, instead of "TextBox15" you could have TextboxCompressAvol (Or, txbxCompressAvol).

Referring To MessageBox VB.NET

I'm writing a program that kids can use to code from within my application instead of a console-based approach
So I have a lesson that teaches them about message boxes, they are given a sample line of code and what it creates and are told to create their own by entering code into a textbox to create their own that says "Hey Dude!", the problem is, I'm having trouble getting the system to check if what they entered was correct or not... eg:
Private Sub btnShowMsgBox_Click(sender As Object, e As EventArgs) Handles btnShowMsgBox.Click
If txtUserInput.Text = MessageBox.Show("I can code!") Then
MessageBox.Show("I can code!")
Else
MessageBox.Show("That's not quite right, try again!")
End If
I've tried adding " " around the relevent code on the first line of the if statement but no joy, as well as trying a variable approach
So basically the problem is the program is getting confused and doesn't understand why I'm checking to see if code for a message box is present, the syntax of the messagebox code doesn't fit well either
Does anyone have any suggestions as to how I would get this working? The logic is so straightforward but it's driving me nuts!
Thanks a million in advance
I believe you want the code to look more like...
Private Sub btnShowMsgBox_Click(sender As Object, e As EventArgs) Handles btnShowMsgBox.Click
If txtUserInput.Text = "MessageBox.Show(""I can code!"")" Then
MessageBox.Show("I can code!")
Else
MessageBox.Show("That's not quite right, try again!")
End If
The double quotes will return as single quotes within the string between the single quotes.

Decimal.TryParse is failing on TextBox.Leave and TextBox.LostFocus

This has got to be one of the most frustratingly stupid bugs I have ever encountered, and I just want to see if anybody else has run into this before.
Here's the deal. I have a TextBox in a Windows Forms application in VB 2008 (.NET 3.5) where a user can key an estimate amount. I am allowing them to key dollars and cents, and I want to round to the nearest dollar. The original code had the rounding down when the data was written back to a table, and that worked fine - I have this code in a "Save" routine that fires when the user moves to a different screen or record:
Dim est As Decimal : Decimal.TryParse(txtEstimateAmount.Text.Trim, est)
Dim estimatedAmount As Integer = Math.Round(est)
I decided that it might be nice to actually do the rounding as soon as they leave the field instead, so they're not surprised when they reload the screen and find that 1822.60 is now 1823. So I took the exact same code and added it to the TextBox.Leave event handler. And the weirdest thing happened: instead of the variable est being populated with 1822.60 after the parse, it gets set to -1! What the...?
Debugging the handler shows that the value goes into the parser correctly, and if I do the parsing manually via the Immediate window, it parses correctly, but when I let the code do it, it invariably gets set to -1. What's even weirder is that any number gets parsed as -1, not just decimals, and any non-number gets parsed as 0 (which is correct).
Has anybody else ever run into this before? I tried moving the code to the TextBox.LostFocus event instead, but with the same results. I have no idea what in the heck is going on, and obviously there are workarounds galore for this, but it just makes no sense whatsoever.
EDIT: Here's the full event handler (current behavior for which is to put -1 in the TextBox):
Private Sub txtEstimateAmount_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEstimateAmount.Leave
' Take any dollars-and-cents amount and round to the nearest dollar
Dim est As Decimal
est = Decimal.TryParse(txtEstimateAmount.Text.Trim, est)
txtEstimateAmount.Text = If(est <> 0, Math.Round(est).ToString(), String.Empty)
End Sub
First off, you really need to do this with a Validating event handler so that you can catch junk and avoid ignoring the return value of TryParse. Like this:
Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim est As Decimal
If TextBox1.Text.Length = 0 then Exit Sub '' optional
If Not Decimal.TryParse(TextBox1.Text.Trim, est) Then
e.Cancel = True
TextBox1.SelectAll()
Else
TextBox1.Text = est.ToString("N0")
End If
End Sub
Explaining -1 is difficult. TryParse normally writes 0 if it cannot parse the text. Watch out for changing the UI thread's CurrentCulture property. And any changes made to the format settings in Control Panel + Region and Language applet.
I don't think the code you've posted is the code you're running. What is happening is:
Dim est As Decimal = Decimal.TryParse(txtEstimateAmount.Text.Trim, est)
Dim estimatedAmount As Integer = Math.Round(est)
I would do a clean and a rebuild or try rewriting it in a different format, maybe with a boolean to get the result of the tryparse.
EDIT now that I've seen your actual code. You are indeed putting the true/false from the tryparse result into the est decimal. Delete the est=. Est gets loaded because it is passed by reference into tryparse.