Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在.Net中将区域性更改为DateTimepicker或日历控件_C#_.net_Internationalization_Datetimepicker_Culture - Fatal编程技术网

C# 如何在.Net中将区域性更改为DateTimepicker或日历控件

C# 如何在.Net中将区域性更改为DateTimepicker或日历控件,c#,.net,internationalization,datetimepicker,culture,C#,.net,Internationalization,Datetimepicker,Culture,当所需区域性与PC中安装的区域性不同时,如何在.Net中将国际化设置为日期时间选择器或日历WinForm控件 dtp.Format = DateTimePickerFormat.Custom; dtp.CustomFormat = "yyyy-MM-dd"; // or the format you prefer 似乎不可能改变文化。看这个 我认为有绕道 设置事件处理程序“ValueChanged” 代码 dateTimePicker.Format = DateTimePickerFormat

当所需区域性与PC中安装的区域性不同时,如何在.Net中将国际化设置为
日期时间选择器
日历WinForm
控件

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

似乎不可能改变文化。看这个

我认为有绕道

  • 设置事件处理程序“ValueChanged”
  • 代码

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

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

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

    使用telerik radDateTimePicker并在初始化Component()后编写此代码,使用区域性而不是“fa-IR”

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

    设置格式并不是唯一受区域性控制的事情。它控制许多其他事情,例如一周的第一天是星期日还是星期一。格式仅用于显示当前日期,但如果您想选择它,datetimepicker控件将根据区域设置显示完整日期。这不会更改窗口的区域设置,只有当前UI区域性和当前线程的当前区域性(如属性名称所示)。但是DateTimePicker控件只查找窗口的区域设置,所以这无关紧要。GetDateTimeFormats()不生成格式字符串(可用于格式化),而是生成格式化字符串(格式化的结果)。将这样的字符串指定给.CustomFormat没有意义。我会投票否决这个答案,但我自己缺乏声誉…KB文章的链接已经死了…:(他们现在应该已经修复了…8年后了…这两个链接现在都断了。对我来说,这对改变文化有什么帮助?
    Application.CurrentCulture = new CultureInfo("fa-IR"); 
    radDateTimePicker1.Format = DateTimePickerFormat.Custom;
    radDateTimePicker1.CustomFormat = Application.CurrentCulture.DateTimeFormat.ShortDatePattern;