Ale*_*lex 4 .net c# vb.net winforms
我为本地企业维护了一些小型 .NET 应用程序。我们有一对协同工作的应用程序:一个“客户端管理器”(C#/.NET 4.?/winforms)和一个“标签打印机”(VB/.NET 2.0/winforms)。
我最近在客户端管理器中添加了一个“打印标签”按钮。这将打开标签打印机并从客户管理器中预填充客户的名称。呜呼!
不幸的是,只有当标签打印机以这种方式打开时,日期才会以“dd/MM/yyyy”格式打印,而不是“MM/dd/yyyy”。
CurrentCulture
和CurrentUICulture
都是en-US
,无论我如何加载。(谢谢,@Jimi!)M/d/yyyy
通过“控制面板 > 区域”使用。(谢谢,@Hans Passant!)这是我在客户端管理器中用于打开标签打印机应用程序的 C# 代码(为了清楚起见添加了注释):
private void btnLabels_Click(object sender, EventArgs e)
{
// Set via a hybrid string/file input in the app's "Options" menu.
string labelAppLocation = Properties.Settings.Default.LabelAppLocation;
if (String.IsNullOrEmpty(labelAppLocation))
{
MessageBox.Show("We're not sure where your label printer app is located! Set this in \"Options >> Manage Lists >> Files\" and try again.");
}
else
{
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = labelAppLocation;
// This class has some formatting helpers for the
// client's name and other demographics we'd like to send later.
LabelArgs newLabelArgs = new LabelArgs();
newLabelArgs.Name = this.clientName.Text;
psi.Arguments = newLabelArgs.ToString();
p.StartInfo = psi;
p.Start();
}
}
Run Code Online (Sandbox Code Playgroud)
这是标签打印机中的 VB 代码,其中日期值被添加到标签:
Private Sub DrawItemLabel(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim g As Graphics = e.Graphics
'Custom font handler
Dim fonts As New ItemLabelFonts
'...set fonts, set other line items...
' `myDate` comes directly from a DateTime input's `.Value`.
Dim strMidLine As String = myDate.ToShortDateString & " " & myClientID & " " & myCounty
fonts.MiddleFont = ChooseMaxFontForWidth(strMidLine, fonts.MiddleFont, maxWidth, g)
'...do some math for spacing before drawing the label...
g.DrawString(myClientName, fonts.BottomFont, Brushes.Black, 0, bottomTop)
End Sub
Run Code Online (Sandbox Code Playgroud)
Process#Start()
正在失去当前的文化并回到dd/MM/yyyy
某个地方的默认设置?(确认不是来自用户的“区域”设置)Load
钩子中没有逻辑来解释这一点。为什么可以通过new Process().Start();
更改日期格式来打开应用程序?
成功!我将标签打印机应用程序从 .NET 2.0 重新定位到 .NET 4.0(与客户端管理器应用程序匹配),清除了新警告,并重建了应用程序。运行v4.0版本的标签打印机解决了问题,没有明显的副作用。
我应该早点完成这个 - 我预计从 2.0 到 4.0 是一项艰巨的任务,但没有构建错误,唯一的警告是隐式转换。这些都没有影响
Date
变量,所以我仍然不清楚为什么2.0 版本表现得如此奇怪,但我很高兴把这个错误放在床上。我不打算在近期内编写任何新的 .NET 2.0 项目!