|
-
January 23rd, 2009, 05:38 AM
#1
FormatNumber!! Not working correctly...
Here is another one for the books ...
I use FormatCurrency, and FormatNumber for the GUI data entry modules...
In .Lostfocus I use FormatCurrency, and in .Gotfocus i use FormatNumber..
Code:
Private Sub TxtAmounts_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtMaxBulk.GotFocus, TxtMaxSale.GotFocus _
, TxtMinSale.GotFocus, TxtComm.GotFocus
Dim TmpTxtBox As TextBox = CType(sender, TextBox)
If TmpTxtBox.Text <> "" And Not TmpTxtBox.ReadOnly Then TmpTxtBox.Text = FormatNumber(TmpTxtBox.Text) ' Error here
End Sub
Private Sub TxtCredits_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtMaxBulk.LostFocus, TxtMaxSale.LostFocus _
, TxtMinSale.LostFocus
Dim TmpVal As Double
Dim TmpTxtBox As TextBox = CType(sender, TextBox)
If TmpTxtBox.Text = "" Then
TmpVal = 0
Else
TmpVal = FormatNumber(TmpTxtBox.Text)
End If
TmpTxtBox.Text = FormatCurrency(TmpVal)
End Sub
However I now need to use a Percentage ..
and if i Use FormatPercent .. FormatNumber does not work ..
Code:
Private Sub Txtpercent_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtComm.LostFocus
Dim TmpVal As Double
Dim TmpTxtBox As TextBox = CType(sender, TextBox)
If TmpTxtBox.Text = "" Then
TmpVal = 0
Else
TmpVal = FormatNumber(TmpTxtBox.Text)
End If
TmpTxtBox.Text = FormatPercent(TmpVal , 0, TriState.True, TriState.False, TriState.False)
End Sub
I simply get
Conversion from string "0%" to type 'Double' is not valid.
I thought that FormatNumber is supposed to return the numeric in any number format ... Why cant it handle percents..    
Gremmy..
Last edited by GremlinSA; January 23rd, 2009 at 06:51 AM.
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
January 23rd, 2009, 06:27 AM
#2
Re: FormatNumber!! Not working correctly...
This posts reminds me of another bug I found with TimeSpan that requires a workaround, ... but anyways.
FormatNumber returns a string though, so why are you setting it to a double?
Is this a Strict or Explicit project?
Code:
TmpVal = CDbl(FormatNumber(TmpTxtBox.Text))
Although it does say:
Returns an expression formatted as a number.
I guess they mean string expression.
FormatPercent does too:
Returns an expression formatted as a percentage (that is, multiplied by 100) with a trailing % character.
The only other thing I can think of is that your regional settings may be causing an issue.
Last edited by TT(n); January 23rd, 2009 at 06:32 AM.
-
January 23rd, 2009, 07:04 AM
#3
Re: FormatNumber!! Not working correctly...
even when i tried with
Code:
TmpString = FormatPercent(0.1, 0)
Dim tmpval As String = FormatNumber(TmpString)
i still get the same error ...
System.InvalidCastException was unhandled
Message="Conversion from string "10%" to type 'Double' is not valid."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.Strings.FormatNumber(Object Expression, Int32 NumDigitsAfterDecimal, TriState IncludeLeadingDigit, TriState UseParensForNegativeNumbers, TriState GroupDigits)
at CashFlow3_Vendors.FrmUtility.TxtAmounts_GotFocus(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnGotFocus(EventArgs e)
---SNIP----
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.FormatException
Message="Input string was not in a correct format."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)
InnerException:
I'm using the Double type with FormatCurrency, and FormatPercent, not with the FormatNumber... It seams that Internally FormatNumber uses the Double type...
I've checked the reginal setings, and everything look right there..
Also i have to rely on reginal settings because the application will be used in several country's, and each has there own methods of displaying currencies, and i dont feel like writing code to Format and Unformat each different currency..
Gremmy..
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
January 23rd, 2009, 07:11 AM
#4
Re: FormatNumber!! Not working correctly...
Ive resorted to writing a wrapper function to find the percents and format them appropriately ..
Code:
Public Shared Function FormatNum(ByVal Expresion As String) As String
If Expresion <> "" Then
If Expresion.EndsWith("%") Then
Return FormatNumber(Expresion.Substring(0, Expresion.Length - 1), 0)
End If
Return FormatNumber(Expresion, -1)
Else
Return "0"
End If
End Function
This works .. but it's not right ...
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
January 23rd, 2009, 07:25 AM
#5
Re: FormatNumber!! Not working correctly...
I get the same error in your example, but I didn't expect it to take a percent sign as a valid number format(expression). I see. It should be able to accept percents, and format them as a number, but I don't see any examples online that do that.
You're right it's not.
Code:
Private Function FormatNum(ByVal oString As Object) As String
If oString.ToString.IndexOf("%") <> -1 Then
oString = oString.ToString.Replace("%", "")
Return (CDbl(oString) / 100).ToString
End If
Return FormatNumber(oString)
End Function
Last edited by TT(n); January 23rd, 2009 at 07:54 AM.
-
January 23rd, 2009, 07:54 AM
#6
Re: FormatNumber!! Not working correctly...
I'm following very strict rules for entry, NOTHING is left to chance. The entry module is going to be used by the biggest of idiot's you can find.
Every entry has a keycode filter on it so that no stray keypresses can slip in. also the client's requirement is to have all currency's etc, to be displayed properly formatted at all times.. (except when editing the value)
Hence the need to format & strip formating, in the LostFocus & GotFocus ...
However i always thought that Percentage was a numeric format .. Go figure..
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
January 23rd, 2009, 08:00 AM
#7
Re: FormatNumber!! Not working correctly...
Your intention was to convert percentage to decimal right?
Then make sure to divide by 100, unless you just want to drop the "%", in your function.
I think percents are a numeric format in excel or other MS apps, so you're right in that way I believe.
-
January 23rd, 2009, 08:36 AM
#8
Re: FormatNumber!! Not working correctly...
haha .. remember,I said 'used by the biggest of idiot's you can find' ..
Do you think they will know that 0.1 is 10%.. they will punch in 10 and expect it to be 10% not 1000%.. (
So I have to compensate for this in the "Txtpercent_LostFocus" sub with..
Code:
TmpTxtBox.Text = FormatPercent((TmpVal / 100))
at least this way i can also ignore decimal inputs, less to cause problems. we only interrested in whole percentages..
BTW. Your sample is alot safer than mine, I'll feel better using it..
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|