winforms 选择组合框项将重置整个表单[重复]

rqdpfwrv  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(120)

此问题已在此处有答案

Change language at runtime in C# winform(2个答案)
4天前关闭。
enter image description hereenter image description here问题1:代码重置整个表单。只需要重置组合框实际上在这部分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();

等待您的回答,提前感谢。

btxsgosb

btxsgosb1#

看起来像是在用事件处理程序内的这行代码重置窗体,该事件处理程序在窗体的选定索引更改时触发

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;
                    
         }
}

如果在窗体加载时组合框为空,则可以定义一个填充组合框的方法,并从FormLoaded事件处理程序调用该方法。

//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);
   }
}

相关问题