.net - Decimal.TryParse Is Failing On TextBox.Leave And TextBox.LostFocus?
Dec 22, 2010
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
[code]....
View 2 Replies
ADVERTISEMENT
Apr 1, 2010
Have a project which, in one place, has a form with a number of textboxes on it to receive user input. My intention is that, when user tabs out of one box into the next in the sequence, a checking subroutine is called via the textbox.lostfocus event. When I run the app, which this procedure is called by tabbing out of, say, txtTitle without entering any data, I get the Msgbox message, but it refers to the next textbox (txtFName).
It would appear that the focus moves into the next textbox before the lostfocus event is dealt with. Consequently, when the Msgbox opens, it takes the focus away from the next textbox and fires its lostfocus even, which is processed. OK-ing the warning here causes the initial lostfocus event to be processed and the Msgbox shows again. Exiting the sub for the second time leaves the focus in the second textbox. If I then use the mouse to move the focus back to the first box, I fire yet another lostfocus event for the second textbox, etc, etc
How can I trap the lostfocus event and deal with it BEFORE the focus moves into a subsequent control?
The relevant piece of code is:
[Code]...
View 1 Replies
Aug 20, 2010
the scenario is the following: I have a textbox ,next to it a button. The user types a value in the textbox, he presses tab, I search this value in the database in the LostFocus event, if there is no match, a browse form shows and the user can select the desired value. After closing the browse form, the focus goes to the next textbox control (and exactly this I want to prevent). The user can click the button next to textbox to choose a value (the browser form appears), but in this case, after closing the form, the focus remains on the textbox control, because before clicking the button, the focus was on it.
So, my question: how can I disable the LostFocus event? Ok, I figured it out: I must use the validating event. It works. But in this case, if I close the form, the event fires up. How can I disable this?
View 2 Replies
Jun 9, 2010
So I have a LostFocus handler for my TextBox, and it gets called whenever I press "m", "o", or "-". Why is that? [Code] Is all that's there. I thought that I might have access keys or something interfering, but I would have to be pressing Alt, "-" wouldn't work, and there's also the fact that I have no access keys on the form. What's happening?
View 7 Replies
May 31, 2012
I am finding it strange that the lostfocus event is always executed once when I launched my application. in my lostfocus event, I have some code that queries the db based on the textbox value. It is supposed to be 13 characters. So when i launched my app and presses the first character in the textbox, it is givin an error. When i debug the application, I found out that the lostfocus event is getting called once when i start the application.
View 4 Replies
Oct 21, 2009
How would I code a TryParse to get the input from customer from a textbox named Strands and convert it to a number (this number will always be a whole complete number, cannot be 5.6 or 4 1/2 or whatever) then have that Strand number be muliplied by the selected item in my listbox1. This listbox has an access database bound to it with 2 columns Size and Area, the Size equals the Area and I set the Value to the Area column but set the Size to show up in the list box. and the Strands textbox has to muliply by the Size picked, but by the Area # not the Size #. And the results of that multiplication showing up in another textbox named total.
View 14 Replies
May 19, 2010
So, I'm trying to make a RichTextBox which makes seperate words colored as you type them (like in Visual Studio itself ).The problem is, it's absolutely not working as I want and absolutely not the same as in Visual Studio.It's constantly running over the code each 500 milliseconds (by a timer), bugging the focus and selecting the words you want to be colored quickly.
Also, when you have something selected in the textbox,it constantly unselects when it runs over the code again (tried to fix that, but failed).So, can please someone improve this code (a lot, it's coded really bad ) so the user doesn't notice the words are getting colored?I put the words into an array with a different array containing the colors of the string array on the same index (Sorry for my "not so good" english.. But I'm sure you understand it ).
[Code]...
View 12 Replies
Aug 23, 2010
I have been working in an Migration Application where i find out some weird problem , Leave Event Fires twice causing validation to fire twice , hence i want to intercept the events to be processed by application..Hence require help in hooking the delegates so that i can get a list of the events that are to be processed .If anybody can help me out by giving some info or code as how to implement hooking in a textbox leave event/
View 1 Replies
Jul 25, 2010
I used textbox.leave so that when user leave the textbox, it will fire a function.
Under some cases, I would like to disable this textbox.leave event.
View 13 Replies
Jan 10, 2012
I've got a (hopefully) quick question. I've got textboxes attached to a binding source on one of my forms, and when I add, then clear, the data for the next entry to be added a small problem arises. If the user has previously entered something into, for example, Textbox3 and the user does not have the data for Textbox3 the user cannot move to the next field unless something is entered into the box.
[Code]...
View 6 Replies
Jul 1, 2009
I'm using MS Access to store the value from textbox. First of all I had created a DateTimePicker that allow the user to select the Date Of Join. After the date is selected the value for the Expiry Date textbox is automatically store the date after 1 year in this format (1/7/2009) the code
[Code]...
View 1 Replies
Dec 21, 2010
1) One Decimal Varibale stores a decimal value
2) The value must be converted in string ( some time the comma is used as decimalplaceholder some time the dot)
3) the user modifies the value
4) i need to riconvert the string back in decimal
how can i do this
so:
Variable 123.34D ----> textBox 123,34 or 123.34 -----> variable 123.34D
View 3 Replies
Jun 28, 2009
how to capture the text that a user enters in a textbox when they press the enter key? I thought there was an event that handles this but I can't quite seem to get anything to work.
View 3 Replies
Oct 26, 2010
I've read you should use the "Leave" event instead of "Lostfocus" for your code. But if you put something like this inthe leave event,Me.TextBox2.Select()The Leave event fires twiceBut if you put that code in the LostFocus event it only fire once. Is it ok to use the LostFocus or should I add additional code to handle the Leave event firing twice?
View 10 Replies
Mar 5, 2009
Here is the code in C# to allow only one decimal point in a textbox:
if textbox5.text.contains(".") && e.keychar="."
{
e.handled=true
}
I have to use it in VB.NET 2003 version, but I can't use the contains property. How can I do this?
View 1 Replies
Mar 28, 2012
I have a textbox aka TXT_Weighting in which I place a value of %.
If I put in a number 0 < number < 100, it works.
However, If I put in 100, I get this error: "error converting Data Type numeric to decimal"
The Database Field has the DataType of decimal(10,2).
I have tried converting it to decimal, string, int, float in the application, but once it saves, gives the same error.
I know it is because of the database field having the type as decimal. But I cant stop the entire application, just so I can change the data type of the field.
How do I save it without getting this error? How do I control it from the application?
[Code]....
View 7 Replies
May 28, 2009
How to convert the textbox.text to a number with decimal points. Once the subroutine have been process, the numbers will be dispalyed on the textbox or label in a format with two decimal numbers.
View 2 Replies
Sep 18, 2009
validation for , allow only 2 digit after decimal point in TextBox in vb.net eg 150.25
View 3 Replies
Jun 25, 2009
i am very sorry for the inconvnience. this is my first post... i have downloded this souces code version of (VB6) . i want to convert to vb.net 2005 or vb.net 2008[u]..... i think this function usefull for others also. i am try to develop Payment system. i will post it after complete. there are so meny controls. i need this one also add to my Payment System.... Text Box Keypress event is not compatibal with Vb.net..
[Code]...
View 6 Replies
Jun 26, 2010
How can I make a textbox in which can be only typed a number like 12.00 or 1231231.00 or 123123
I've done this in a very long way and I'm looking for the best and fastest way.
Also the decimal separator must be culture specific.:
Application.CurrentCulture.NumberFormat.NumberDecimalSeparator
View 4 Replies
Jan 22, 2011
In COBOL, I read a value (a currency amount with 2 decimal places, for example) via a picture clause (and a screen section, of course), like:
WORKING-STORAGE SECTION.
01
VALUE-TO-READ PIC ZZZ,ZZ9.99.
...
[Code]....
The tricky part here is, whenever the decimal sign key is pressed, the input procedure understands that the following number is to be displayed to the right of the decimal point (not left).
Now, I'm trying to do this in VB. Unfortunately, my all attempts failed with MaskedTextBox.
Do I have to hardcode this procedure in my program, or is there an easier way to get around this?
View 2 Replies
Jan 28, 2009
1)How to set the textbox only allow (Numbers , No decimal)?
2)How to set as Default two decimal place for textbox?
View 1 Replies
Oct 4, 2009
My problem is My AmountDueBox.text keeps showing my SubTotalDecimal. I want it to Show My Amount Due decimal (SubTotal + TaxDue - TradeInDecimal). I have text boxes displaying the various elements of the total along the way and they are all right except the final amount due. TotalDecimal- TradeInDecimal.
Public Class VBAutoCenter
'Declare Variables
Const SalesTaxDecimal As Decimal = 0.08
Const StereoDecimal As Decimal = 425.76
Const LeaterDecimal As Decimal = 987.41
Const CompNavDecimal As Decimal = 1741.23
[Code] .....
View 1 Replies
Mar 2, 2010
I need to amend the code below to cater for only two Decimal places. Anyone know how i can do this?
[Code]....
View 3 Replies
Mar 30, 2010
I currently have a calculation that gets performed based on the type of operation a user chooses on my form (+, -, *, or /). I have it check their answer against the correct answer and tell them if they get the answer right or wrong. I need to know how I can check the answer when they choose a division sign that checks only the first two digits of the lbltxtAnswer label. I also currently have a validation that checks for numbers only, so if the user types in a decimal, the error message displays, which I don't want it to do. Here is my calculation and validation [code]...
View 2 Replies
Aug 31, 2010
it should take either decimal values or integers and max length is 16if it takes decimal value as input then it should contain only 14 digits in decimal place and 2 digits in fraction part eg(1234567.12-valid) but (12345678.123 - invalid) and also(123456.12345- invalid)
View 2 Replies
Feb 20, 2012
I got this class from another vb help forum and I'm using it for fields that accepts monetary values. Is there a way to incorporate a '1-decimal' point only rule?ATM, I can enter values like this: 1.2.2.20000..I need to limit the number of decimal points used. (1.0000)[code]
View 7 Replies
Oct 29, 2009
How can i change the format of my textbox into two decimal places "#,###.00"
i used mask textbox and used this format 0,000.00 but i cant input more than 4 digits whole number
View 6 Replies
Mar 7, 2010
I want to take more than one number separated by one space from txtDec textbox and display their binary form again separated by one space in txtBin textbox.
My code below ---
Private Sub DecToBin()
Dim i As Integer = txtDec.Text
Dim binary As String = Convert.ToString(i, 2).PadLeft(8, "0"c)
txtBin.Text = binary
End Sub
View 3 Replies
May 11, 2010
I am developing windows application in vb.net, i have textbox which should allow only numbers with decimal point. for example "99.00"
View 6 Replies