I'm trying to make a NumericUpDown box set the decimal points of a textbox and update everytime it is clicked up or down. I have tried it 2 different ways:
Me.txtCalc.Text = FormatNumber(Me.txtCalc.Text, Me.NumericUpDown1.Value)
This way works but it isn't keeping the decimal values when i increase the value. So if the textbox said 2 and is really supposed to be 2.987899. I change the NumericUpDown box from 0 to 6 and all it does is display: 2.000000.
I've also tried it this way (Which i think I'm doing wrong..)
Me.txtCalc.Text = Math.Round(CDec(Me.txtCalc.Text), CInt(Me.NumericUpDown1.Value))
This was the only way I could get it to do anything. And when I try this, everytime I click the vlaue to go up, it does absolutely nothing. But if I set it to 7 first and then add some values like: 2.987899; when i click from 7 down to 1, it removes one point at a time like it should... it just doesnt go back up. at all.
I assume I have to use Math.Round because that's the only way it will keep the decimal values...
Hopefully I explained this so you guys understand.
Any help would be appreciated.
Me.txtCalc.Text = FormatNumber(Me.txtCalc.Text, Me.NumericUpDown1.Value)
this not only formats the value, it resets the starting value for any next iteration. Assuming a start of 2.987899 going down in decimal places changes the VALUE:
2.98789
2.9878
2.987
..
2.
When "2" gets assigned to the TB, the starting value for going up is 2. you lost the rest of the decimals. You need to store in a decimal variable and format THAT to go up and down as you want:
Dim decVal As Decimal = 2.987899
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
' now, I am not changing the underlying VALUE, just the format
TB1.Text = FormatNumber(decVal, NumericUpDown1.Value)
End Sub
I'm not sure how you get different results with Math.Round because the code shown will still assign a new value to the TB with fewer decimals and therefore lost.
Related
The code I use is the following:
' (is on top of the listing directly after Public class form1)
Dim Score as Integer
...
Score=Val(Txtbox5.text) "Txtbox5 is control where I put in the value."
Lbl2.text=score "Lbl2 is the label where the score must display."
Txtbox5.text="" "Makes the Textbox empty."
Then the problem occurs: when I enter a new value in Txtbox5.text the I want this value update
the value in the label with the value that is already in the label by the score.
The code I use therefor is Lbl2.text=score +score but whats happend is the score in the label is double???
So what I want is: when I have a value in the label say 2 and I insert a new value in the textbox
say 3 then I want to see in the label a value of 5.
But I have tried everything but nothing works
Is there somebody who can help me with this problem??
If I understand you correctly you want to sum the current value of the TextBox5 to the Score variable and then update the label
This could be simply resolved without any thinking with
Score += Val(Textbox5.Text)
Lbl2.text=Score
Txtbox5.Text=""
But you could meet some problems because in your textbox the user can type anything, not just a number. In that context the Val function from the Microsoft.VisualBasic compatibility assemble doesn't tell you anything, simply converts the non numeric string to zero. This could be acceptable or not depending on your requirements. If you want to have a message for your user when the input is not good you could write
Dim currentInput as Integer
if Not Int32.TryParse(Txtbox5.Text, currentInput) Then
MessageBox.Show("You should input a numeric value!")
Else
Score = Score + currentInput
Lbl2.text=Score
Txtbox5.Text=""
End If
I have tried everything but nothing works
That's not strictly true..
Lbl2.Text = (Convert.ToInt32("0"&Lbl2.Text) + Convert.ToInt32(Txtbox5.Text)).ToString()
ps;
Your life would be a lot simpler if you used a couple of numericupdown controls, one readonly and no border etc to look like a label. Then life would be like:
resultNumericUpDown.Value += inputNumericUpDown.Value
Whenever you're asking the user for numbers, using a NUD saves a lot of headaches
i have a piece of code that is to be repeated several times throughout my form when certain buttons are pressed. on the clicking of the buttons it should be adding to the decimal value inside the textbox by the value that varies from button to button.my code so far looks like this:
Function EditPrice(ByVal a As Decimal, ByVal b As Decimal)
Dim num1 As Decimal
num1 = a + b
Return num1
End Function
Private Sub btnMinusMushroom_Click(sender As Object, e As EventArgs) Handles btnMinusMushroom.Click
EditPrice(CDec(txtEditCost.Text) + "0.5")
End Sub
if that isn't clear what the code should be doing, the variable of "a" in the function "EditPrice" is the decimal value inside the textbox "txtEditCost" and the decimal value of .5 is the value that should be added to the textbox value.
the function name is underlined red saying "argument not specified for parameter 'b' of 'public function EditPrice(ByVal a As Decimal, ByVal b As Decimal) as object' ".
i dont know what that means or how to fix the problem, any ideas?, thanks
There are a number of issues with your code. To help you avoid such issues in future, I strongly suggest that you set Option Strict On. That will disallow narrowing implicit conversions and late-binding, which will force you to give more consideration to the data types you're using. You can set it On for the current project in the project properties. You can also set it On in the IDE options so that it will be On by default for all future projects.
The first issue is one that Option Strict On would flag, i.e. you have declared a function without a return type. This:
Function EditPrice(ByVal a As Decimal, ByVal b As Decimal)
should be this:
Function EditPrice(ByVal a As Decimal, ByVal b As Decimal) As Decimal
The second issue relates to the naming of that function. You have named it EditPrice but, if you look at the actual code of the method, the name has nothing at all to do with it. There's no price there and there's no editing. All it does is add two numbers of type Decimal and return the result. There's nothing about that that requires the numbers to represent a price and nothing is edited because nothing is changed by the method.
The third issue is with how you're calling the method. The method is declared as taking two Decimal arguments yet what are you doing?
EditPrice(CDec(txtEditCost.Text) + "0.5")
How does that make sense? You get a String from a TextBox and convert that to a Decimal and then you add another String to that and then pass that single result to the method. That's all kinds of wrong. Firstly, why are you adding a String to a Decimal? If you want to add the number 0.5 to something then use a number, not a String. If you want to add to a Deciaml then use a `Decimal number. Secondly, why are you adding the two numbers there and passing the result to the method when the whole point of the method is to add the two numbers? That code should be more like this:
EditPrice(CDec(txtEditCost.Text), 0.5D)
That's still a problem though because, as I said, the EditPrice method doesn't actually edit anything. It just adds two inputs and returns the result, but you don't actually use that result. You presumably want to display that result somewhere. If it's in the same TextBox again then you need to do that:
txtEditCost.Text = EditPrice(CDec(txtEditCost.Text), 0.5D).ToString()
Note the ToString call there too. That will be required by Option Strict On because that Text property is type String and that method returns a Decimal.
The code is still a bit dodgy even with those changes. What's the point of a method that adds two numbers and returns the result when you could do that inline with a simple addition operator? If a method is to be named EditPrice then it actually ought to edit a price. In that case, it would make more sense to have it take a single number to add to the current price and then have the method make the change, e.g.
Private Sub EditPrice(valueToAdd As Decimal)
Dim currentValue As Decimal
'This will get the current value or zero if there is no valid value.
Decimal.TryParse(txtEditCost.Text, currentValue)
Dim newValue As Decimal = currentValue + valueToAdd
txtEditCost.Text = newValue.ToString()
End Sub
Note that that is a Sub rather than a Function because it actually makes a change rather than return a value that other code uses to make a change. In this case, the name actually describes what the method does. Method names should ALWAYS describe what the method actually does.
The following image depicts the full gridview I have:
I'm able to get the values of the second column (project_ID) in the back end as follows:
Dim val As String = grdProjects.SelectedRow.Cells(1).Text.ToString
For instance if I press the Select of the third row I'll get back the value 3.
My problem appears when the gridview is updated and has less records as the one in the following image.
If I press the second record I get the value 2 instead of the value 4 that I'd like to get.
Any suggestions please on how to get the actual value of the cell instead of the number of the selected row?
I might be wrong but in VB.net datagrids SelectedRow does not exist, SelectedRows do exist.
Anyway, try this way and let me know if it work:
On the RowEnter or CellClick event use the E.RowIndex to get what you want. Do as follow:
Private Sub DataGrid1_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGrid1.RowEnter
Dim CellValue As String = DataGrid1.Rows(e.RowIndex).Cells("Project_ID").Value.ToString
End Sub
Notice that for the .cells() I use the name of the column, this is better because if in the future you add columns this code will not need to be changed. Easier code maintenance always pay! Also. Cell().Value instead Cell().Text
Nandostyle
I am making a database with access 2007. I have a form for the call center to enter customer info; Name, Address, Phone Number, ect.
There is a field for credit card numbers and while we are supposed to enter them as first 4 numbers and last for number ie.1234xxxxxxxx4321
I want to make sure if they do enter them in that it keeps the first and last 4 numbers but changes other characters to "x" when the field loses focus. Could anyone point me in the right direction of how to do this?
Thanks in advance for all help in this matter.
If you are only storing the first 4 and last 4 digits then the following works. Modify the function validCreditCardNumber() to have whatever checks you want to apply.
Function validCreditCardNumber(creditCardNumber) As Boolean
If Len(creditCardNumber) = 12 Then
validCreditCardNumber = True
Else
validCreditCardNumber = False
End If
End Function
Private Sub cbxCreditCardNumber_LostFocus()
If validCreditCardNumber(cbxCreditCardNumber) Then
cbxCreditCardNumber.Text = Left(cbxCreditCardNumber.Text, 4) & "xxxxxxxx" & Right(cbxCreditCardNumber.Text, 4)
End If
End Sub
If you want to store the entire number but only hide the digits from the screen then I think a input masks are what you are looking for.
All you need is something like this in your form code.
Private Sub txtCC_LostFocus()
txtCC.Text = Left(txtCC, 4) & String(8, "x") & Right(txtCC, 4)
End Sub
Then what you see is what will get stored in the DB. ie.1234xxxxxxxx4321
I'm going to assume you don't want to actually keep the whole CC# in your DB. That is a huge no-no unless you spend massive time & money to meet PCI compliance. Here's some info on PCI: http://www.pcicomplianceguide.org/pcifaqs.php
There is no input mask available for partial masking of a string entry. You will need to use a custom function to change the data entered. The best way to do this would probably be to link the field in the table to a hidden textbox. You can then have a visible textbox apply some function to the value in the hidden textbox to change the characters to "X" in the LostFocus event of the visible textbox. However, it's also going to have to write the data to the hidden textbox when you change it.
I have a form with a status strip, which contains a progress bar an a label. Both of these are used to show the user the status/progress of several background workers.
My problem is that sometimes the label is longer than the form is wide (it contains Parameter names that vary quite widely in length). The form has a constant width and is not re-sizable by the user. When this issue occurs the label just appears as blank, I would instead like to cut the label to the length of the form and concatenate "..." on the end.
Can anyone give me some advise on where to start with this? i have tried Google and SO searches and have been unable to come up with anything similar. I essentially need to find the length of the string as it will display on the form, but I don't know where to start with that.
First thing to do is to change the StatusStrip.LayoutStyle from Table to Flow. Which will prevent the label from disappearing. Next, you still want the user to have a chance to read the full text of the label even though it is truncated. Set the StatusStrip.ShowItemToolTips property to True and the label's AutoToolTip to True.
Getting the label's text to not overlap the grip is an uglier problem to fix but one you don't have since you made your form un-resizable. Set the form's SizeGripStyle property to Hide.
This will fix your problem, no code required.
You can try something like this:
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
If Label1.Text.Length > iMaxLblLenght Then
Label1.Text = Label1.Text.Substring(0, iMaxLblLenght) & "..."
End If
End Sub