0

I can't get the datetimepicker text to show the abbreviation for the month in another language

I changed the regional settings for my PC to Dutch. That changes the month name from May to mei but I don't want to do that.

On start application:

CultureInfo.CurrentCulture = New CultureInfo("en-US", False)


ElseIf TypeOf a Is DateTimePicker Then
        If TableDataString(index) IsNot Nothing And TableDataString(index) <> "" Then
            Dim dt As Date = TableDataString(index).ToString
            TryCast(a, DateTimePicker).Format = DateTimePickerFormat.Custom
            TryCast(a, DateTimePicker).CustomFormat = "dd-MMM-yyyy"
            TryCast(a, DateTimePicker).Text = dt
        Else
            Dim dt As Date
            TryCast(a, DateTimePicker).Format = DateTimePickerFormat.Custom
            TryCast(a, DateTimePicker).CustomFormat = "  -  -  "
            TryCast(a, DateTimePicker).Text = dt
        End If

To change the date in a textbox I can do:

textbox.Text = result.ToString("dd-MMM-yyyy", New Globalization.CultureInfo("nl"))

This is not working

TryCast(a, DateTimePicker).Text = dt.ToString("dd-MMM-yyyy", New Globalization.CultureInfo("nl"))

datum datum2 I like May to show mei

Miguel Terlaak
  • 175
  • 1
  • 13
  • I don't fully understand what you mean, first you say `changes the month name from May to mei but I don't want to do that`. but then at the end you say `I like May to show mei` ? So I don't know what you want or don't want? – K.Madden May 29 '19 at 14:20
  • I don't want to change regional settings. I want to change it whatever the regional settings are. – Miguel Terlaak May 29 '19 at 15:03
  • [Globalizing Windows Forms applications](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/globalizing-windows-forms), [Walkthrough: Localizing Windows Forms](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/y99d1cd3(v=vs.100)), simple instructions here [How to make multi language app in winforms](https://stackoverflow.com/a/32990088/7444103). – Jimi May 29 '19 at 15:05
  • 1
    I can set the form to Dutch with Thread.CurrentThread.CurrentUICulture = New CultureInfo("nl-NL") but that will mess up decimal numbers because we use a comma instead of a dot. I just want the datetimepicker changed not the whole form – Miguel Terlaak May 29 '19 at 15:14

1 Answers1

0

As per the docs, the DateTimePicker class's Text property returns the text representation of the Value property based on the format of the CustomFormat property.

The DateTime object (in this case, the Value property) does not store culture information, and the default ToString method (that is called within the get function of the Text property) will attempt to use the CurrentCulture of the application. You will need to find a way to pass the CultureInfo object into the get function for the Text property of the DateTimePicker object.

That is the reason why setting the text with

TryCast(a, DateTimePicker).Text = dt.ToString("dd-MMM-yyyy", New Globalization.CultureInfo("nl"))

does not work.

You could potentially inherit or extend from the DateTimePicker class to create your own custom implementation that stores and uses the CultureInfo to properly display the date.