C# 一个按钮上的多个操作
在WinForms(C#)应用程序中,我创建了一个语言菜单条和一个声音按钮,但我不知道如何使它们可切换 我的意思是,我想让声音按钮在我点击它时停止声音,在我再次点击它时播放声音,并改变它的图标? 类似地,语言菜单,如何使其在第一次点击时将语言更改为“本地化”,并将其文本更改为另一种语言,然后在第二次点击时返回 这是我的密码:C# 一个按钮上的多个操作,c#,winforms,C#,Winforms,在WinForms(C#)应用程序中,我创建了一个语言菜单条和一个声音按钮,但我不知道如何使它们可切换 我的意思是,我想让声音按钮在我点击它时停止声音,在我再次点击它时播放声音,并改变它的图标? 类似地,语言菜单,如何使其在第一次点击时将语言更改为“本地化”,并将其文本更改为另一种语言,然后在第二次点击时返回 这是我的密码: using System.Globalization; using System.Threading; namespace Project { public p
using System.Globalization;
using System.Threading;
namespace Project
{
public partial class Form2 : Form
{}
private void Menu_LanguageSwitch_Click (object sender, EventArgs e)
{
//Switch to EN - what's here?
{
CultureInfo ci = new CultureInfo("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(typeof(Form2));
res.ApplyResources(lbl_Status, "lbl_Status");
Menu_LanguageSwitch.Text = "Francais";
}
//Switch to French
{
CultureInfo ci = new CultureInfo("fr");
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(typeof(Form2));
res.ApplyResources(Menu_LanguageSwitch, "Menu_LanguageSwitch");
res.ApplyResources(label2, "label2");
res.ApplyResources(label3, "label3");
res.ApplyResources(label4, "label4");
res.ApplyResources(label5, "label5");
res.ApplyResources(label6, "label6");
res.ApplyResources(label7, "label7");
res.ApplyResources(label8, "label8");
res.ApplyResources(lbl_Status, "lbl_Status");
Menu_LanguageSwitch.Text = "Francais";
}
}
}
谢谢,请向初学者解释清楚。我是“新手”。简单的方法是使用表单范围变量 外化为形式 CultureInfo ci=新的CultureInfo(“fr”);
单击按钮时,请检查激活的CutureInfo,然后切换到正确的,您需要跟踪程序的当前状态,以便在按下按钮时进行检查。对于简单标志(如“静音”),这可能只是一个布尔值:
private bool isMuted = false;
private void onSoundClick(...)
{
if (isMuted)
{
//Do unmute kind of things
isMuted = false;
}
else
{
//Do mute kind of things
isMuted = true;
}
}
本地化逻辑与此类似,但如果您希望使用两种以上的语言,则需要循环使用列表/队列。欢迎使用StackOverflow!请看,共识是“不,他们不应该”。