C# MessageBox 按钮 - 设置语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/930754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MessageBox buttons - set language?
提问by Frode Lillerud
When you use MessageBox.Show()
you have a selection of MessageBoxButtonsto choose from. The buttons available are an enum, and give you options like "Yes No", "OK Cancel", etc.
当您使用时,MessageBox.Show()
您可以选择MessageBoxButtons以供选择。可用的按钮是一个枚举,并为您提供“Yes No”、“OK Cancel”等选项。
When I am using, for instance, Norwegian message text the user still gets the English "Yes No".
例如,当我使用挪威语消息文本时,用户仍然会收到英文“Yes No”。
Is there a way to change the text of the buttons (in C#) so that the language is correct? Can I override the text, or set the current locale in some way so that I can have "Ja Nei" instead of "Yes No"?
有没有办法更改按钮的文本(在 C# 中)以便语言正确?我可以覆盖文本,或以某种方式设置当前语言环境,以便我可以使用“Ja Nei”而不是“Yes No”?
I do not want to rely on installing a .NET language pack at my client.
我不想依赖于在我的客户端安装 .NET 语言包。
采纳答案by Fredrik M?rk
There is no native support for this in .NET (as far as I know, anyway; please correct me if I'm wrong, anyone). I did come across this CodeProject article, that seem to do the trick with some message hooking and P/Invoke: http://www.codeproject.com/KB/miscctrl/Localizing_MessageBox.aspx
在 .NET 中没有对此的本地支持(据我所知,无论如何;如果我错了,请纠正我,任何人)。我确实遇到过这篇 CodeProject 文章,它似乎通过一些消息挂钩和 P/Invoke 来解决问题:http: //www.codeproject.com/KB/miscctrl/Localizing_MessageBox.aspx
回答by Shoban
I don't think it is possible, but refer to the MSDN article MessageBox.Show Method. You may get some ideas. You can change the text in the message box. What about creating your own message box (new form) and displaying them?
我不认为这是可能的,但请参阅 MSDN 文章MessageBox.Show Method。你可能会得到一些想法。您可以更改消息框中的文本。创建您自己的消息框(新表单)并显示它们怎么样?
回答by Joey
Usually messagebox buttons (as all of Windows) honor the currently set UI language for Windows. So if you've got an English installation and can't change languages (MUI versions or Ultimate for Vista/7) you're out of luck.
通常消息框按钮(与所有 Windows 一样)遵循当前为 Windows 设置的 UI 语言。因此,如果您安装了英文版并且无法更改语言(MUI 版本或 Vista/7 的 Ultimate),那么您就不走运了。
You could implement a messagebox yourself but I'd beg you not to. Simple things like common hotkeys for the buttons, having the ability to use Ctrl+Ins to copy the contents, etc. are the ones I miss the most when people start reinventing square wheels.
您可以自己实现一个消息框,但我恳求您不要这样做。当人们开始重新发明方形轮子时,我最怀念的是像按钮的常用热键、使用 Ctrl+Ins 复制内容的能力等简单的东西。
回答by Wbauspiess
You can create a panel (pnlExitMode
), with property Visible=false
, and put info text as well as buttons (btnYes
, btnNo
) labeled YES and NO (with button captions in "your" language) onto this panel, then place required Yes/No- actions into the button event handling routines.
At decision time (in my case: warning if ini
file not yet written when closing application) set panel to Visible
. Panel with buttons will pop up.
您可以创建一个pnlExitMode
带有属性的面板 ( ),Visible=false
并将信息文本以及标记为 YES 和 NO(带有“您的”语言的按钮标题)的按钮 ( btnYes
, btnNo
) 放到该面板上,然后将所需的 Yes/No- 操作放入按钮事件处理例程。在决定时(在我的情况下:ini
关闭应用程序时如果文件尚未写入则发出警告)将面板设置为Visible
. 将弹出带有按钮的面板。
Example code:
示例代码:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (bIniModified)
{
pnlExitMode.Visible = true; pnlExitMode.BringToFront();
e.Cancel = true;
}
}
private void btnYes_Click(object sender, EventArgs e)
{
SaveToIni();
pnlExitMode.Visible = false; bIniModified = false;
Application.Exit();
}
private void btnNo_Click(object sender, EventArgs e)
{
pnlExitMode.Visible = false; bIniModified = false;
Application.Exit();
}