在这里输入图像描述在这里输入图像描述问题一:代码重置整个表单。只需要重置组合框实际上在Application.Restart();
部分我需要一个类似的代码
this.Controls.Clear();
InitializeComponent();
问题2:组合框也不会自动显示所选语言。当程序被打开时,它是一个空的组合框。我想我想利用startindex特性,我不确定。
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (ComboBox1.SelectedIndex)
{
case 0:
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
break;
case 1:
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
break;
}
this.Controls.Clear();
InitializeComponent();
等待您的回答,提前感谢。
当表单的选定索引发生变化时触发的事件处理程序中的这行代码看起来像是在重置表单
this.Controls.Clear();
尝试删除或注释该行,并尝试更改组合框项,使您的新方法看起来与下面的方法相似
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (ComboBox1.SelectedIndex)
{
case 0:
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
break;
case 1:
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
break;
}
}
当窗体加载时,您的组合框为空,那么您可以定义一个方法来填充您的组合框,并从form loaded事件处理程序调用它。
//inside the constructor of your form, register the event handler that is triggered when the form is loaded
public MyForm{
Load += OnLoad;
}
//then in the Load event handler fill the combo box
private void OnLoad(object? sender, EventArgs e)
{
FillComboBox();
}
void FillComboBox(){
var items = new string[]{"English","French","Chinese","Hindi"};
foreach(var item in items){
//assuming you have a definition for the combo box
comboBox.Items.Add(item);
}
}