2016-04-20 428 views
0

我目前停留在數字格式問題,我真的不知道如何解決它。.NET Framework中的區域設置覆蓋

我用一個函數從Support組件:

'StringToFormat looks like "123.456789" 
'FormatInfo looks like "0.000" (must display three decimals) 
Support.Format(StringToFormat, FormatInfo) 

它工作正常exept當未設置我的區域設置正確,它不工作... StringToFormat是來自另一個變量應用程序(通過TCP/IP),我無法更改它。因此,當小數分隔符被設置爲昏迷而不是點時,格式化函數做了奇怪的事情,並沒有正確顯示我的值。

爲了解決這個問題,我寫道,實際上創建了一個新的文化,並確保小數點分隔符是點一個小功能:

Sub UpdateRegionalSettings() 
    'I call this everytime I start my application 
    My.Application.ChangeCulture("x-en-US-custom") 
End Sub 

Sub CreateMyCulture() 
    'This function is called only if the culture doesn't exist yet 
    Dim C As New CultureAndRegionInfoBuilder("x-en-US-custom", CultureAndRegionModifiers.None) 
    C.LoadDataFromCultureInfo(New CultureInfo("en-US")) 
    C.LoadDataFromRegionInfo(New RegionInfo("US")) 
    C.NumberFormat.NumberDecimalSeparator = "." 
    C.NumberFormat.CurrencyDecimalSeparator = "." 
    C.Register() 
End Sub 

然而,這似乎並沒有因爲當我去上班到區域設置並將分隔符更改爲昏迷格式功能再次變得奇怪。它就像這些參數覆蓋任何我可以在我的程序中說...

我的問題是:如何強制Support.Format(和其他人)函數使用點作爲小數點分隔符,而不必手動設置爲控制面板的地區和語言?

+0

我幾個月前在您的情況,以及最快的解決方案是在查看編號時進行驗證,並以我的格式保存在數據庫中。 – CristiC777

+0

使用Decimal.ToString()並將CultureInfo.InvariantCulture作爲格式提供程序傳遞。 –

回答

1

以下是一種僅在運行時更改應用程序文化的方法。在表單加載中,所有文化都會加載,所以您可以對它們進行修改,並在文本框控件中查看雙倍日期時間的結果。

Imports System.Globalization 
''' <summary> 
''' Requires 
''' 1 ComboBox: cboCultures 
''' 2 TextBoxes: TextBox1, TextBox2 
''' 1 DateTimePicker: DateTimePicker1 
''' </summary> 
Public Class Form1 
    ' Used to set cboCultures selected item and for restoring the current culture 
    Private OriginalCultureName As String = "" 
    Private OriginalCultureIndex As Integer = 0 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     OriginalCultureName = Application.CurrentCulture.Name 
     DateTimePicker1.Format = DateTimePickerFormat.Custom 

     cboCultures.DisplayMember = "DisplayName" 
     cboCultures.ValueMember = "Code" 
     cboCultures.DropDownStyle = ComboBoxStyle.DropDownList 

     Dim CultureList = 
      (
       From cultureitem In CultureInfo.GetCultures(CultureTypes.SpecificCultures) 
       Order By cultureitem.EnglishName 
       Select New With 
         { 
          .DisplayName = cultureitem.EnglishName, 
          .Code = cultureitem.Name 
         } 
      ).ToList 

     cboCultures.DataSource = CultureList 

     ' get current culture so we can select it in cboCultures 
     Dim item = 
      (
       From T In CultureList _ 
       .Select(
        Function(cultureitem, indexer) 
         Return New With {.Index = indexer, .Name = cultureitem.Code} 
        End Function) 
       Where T.Name = OriginalCultureName 
      ).First 

     OriginalCultureIndex = item.Index 
     cboCultures.SelectedIndex = OriginalCultureIndex 
    End Sub 

    Private Sub cmdChange_Click(sender As Object, e As EventArgs) Handles cmdChange.Click 
     Application.CurrentCulture = New CultureInfo(cboCultures.SelectedValue.ToString) 
     Demo() 
    End Sub 
    ''' <summary> 
    ''' Show results for date and double 
    ''' </summary> 
    Private Sub Demo() 
     DateTimePicker1.CustomFormat = DateTimeFormatInfo.CurrentInfo.FullDateTimePattern 
     TextBox1.Text = DateTimePicker1.Value.ToString 
     Dim value As Double = 123.456789 
     TextBox2.Text = value.ToString 
    End Sub 
    Private Sub cmdRestore_Click(sender As Object, e As EventArgs) Handles cmdRestore.Click 
     Application.CurrentCulture = New CultureInfo(OriginalCultureName) 
     cboCultures.SelectedIndex = OriginalCultureIndex 
     cmdChange.PerformClick() 
     Demo() 
    End Sub 
End Class 

更新:我使用的字符串格式化按以下和它的工作對我來說

Private Sub Demo() 
    DateTimePicker1.CustomFormat = DateTimeFormatInfo.CurrentInfo.FullDateTimePattern 
    TextBox1.Text = DateTimePicker1.Value.ToString 
    Dim value As Double = 123.456789 
    TextBox2.Text = value.ToString("c2") 
    TextBox3.Text = String.Format("{0:c2}", value) 
End Sub 

enter image description here

enter image description here

+0

不幸的是,它看起來像Support.Format函數沒有考慮到文化改變... –

+0

看到我的答覆的更新部分,我通過字符串格式做C2格式。 –

+0

謝謝你的時間,我實際上刪除了糟糕的復古兼容性功能'Support.Format',因爲它不採取任何操作,而是用戶指定的區域設置。我用'String.Format()'來代替。 –