【问题标题】:How to change culture to a DateTimepicker or calendar control in .Net如何在 .Net 中将区域性更改为 DateTimepicker 或日历控件
【发布时间】:2008-10-28 01:27:51
【问题描述】:

当期望文化与 PC 中安装的文化不同时,如何在 .Net 中将国际化设置为 DateTimepickerCalendar WinForm 控件?

【问题讨论】:

    标签: c# .net internationalization datetimepicker culture


    【解决方案1】:

    似乎不可能改变文化。请参阅此KB article

    【讨论】:

    • KB 文章的链接已失效... :( 他们现在应该已经修复了... 8 年后...
    • 这两个链接现在都关闭了
    【解决方案2】:

    基于之前的解决方案,我认为更好的是:

    dateTimePicker.Format = DateTimePickerFormat.Custom;
    dateTimePicker.CustomFormat = Application.CurrentCulture.DateTimeFormat.ShortDatePattern;
    

    【讨论】:

    • 为我返回“dd-MMM-yy”,这对改变文化有何帮助?
    【解决方案3】:
    System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("fr");
    System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
    

    【讨论】:

    • 这不会更改 Windows 的区域设置,只会更改当前 UI 文化和当前线程的当前文化(如属性名称所示)。但是 DateTimePicker 控件只查找 Windows 的区域设置,所以没关系。
    【解决方案4】:

    对于日期时间选择器

    dtp.Format = DateTimePickerFormat.Custom;
    dtp.CustomFormat = "yyyy-MM-dd"; // or the format you prefer
    

    【讨论】:

    • 设置格式并不是唯一受文化控制的事情。它控制着许多其他事情,例如一周的第一天是星期日还是星期一。
    • 格式仅用于显示当前日期,但如果您想选择它,datetimepicker 控件将根据区域设置显示完整日期。
    【解决方案5】:

    我觉得有弯路。

    1. 设置事件处理程序“ValueChanged”
    2. 代码

      dateTimePicker.Format = DateTimePickerFormat.Custom;
      string[] formats = dateTimePicker.Value.GetDateTimeFormats(Application.CurrentCulture);
      dateTimePicker.CustomFormat = formats[0];
      

    【讨论】:

    • GetDateTimeFormats() 不产生格式字符串(可以用来做格式化),而是格式化字符串(格式化的结果)。将这样的字符串分配给 .CustomFormat 是没有意义的。会否决答案,但我自己缺乏声誉......
    【解决方案6】:

    使用 Telerik radDateTimePicker 并在 InitializeComponent() 之后编写此代码,并使用您的文化而不是“fa-IR”。

    Application.CurrentCulture = new CultureInfo("fa-IR"); 
    radDateTimePicker1.Format = DateTimePickerFormat.Custom;
    radDateTimePicker1.CustomFormat = Application.CurrentCulture.DateTimeFormat.ShortDatePattern;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      • 2010-10-07
      相关资源
      最近更新 更多