使用 ComboBox、UserControl 和一些表单来更改语言(针对整个项目) [英] Using a ComboBox, UserControl and some forms to change language (for entire project)

查看:25
本文介绍了使用 ComboBox、UserControl 和一些表单来更改语言(针对整个项目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始之前,我已经进行了研究,似乎找不到任何东西.请注意,我对 UserControl 非常陌生,所以这可能就是它被证明很困难的原因.

I have a combobox in Form1 which when selected allows the user to change between a choice of 21 languages.我创建了一个包含标签、按钮和复选框的 UserControl - 添加到名为 Print 的表单中.

I have a combobox in Form1 which when selected allows the user to change between a choice of 21 languages. I have created a UserControl that contains labels, buttons and checkboxes - adds to a form called Print.

我在这里为按钮使用了 get 和 set 方法.当 Form1 中的语言发生变化时,我希望这个按钮(实际上是所有元素)发生变化.

I have used a get and set method here for a button. When the language is changed in Form1, I want this button (all elements really) to change.

using System.Windows.Forms;

namespace Print
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public string LabelPreview
        {
            get
            {
                return Button_Preview.Text;
            }
            set
            {
                Button_Preview.Text = value;
            }
        }
    }
}

表格1:

如果在组合框中选择了字符串值英语,则调用一个方法 - 这是我想为其他形式更改语言的地方.

Form1:

If string value English is selected in the combobox, call a method - here is where I would like to change language for other forms.

private void ComboBoxLang_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedItem = this.comboBoxLang.GetItemText(this.comboBoxLang.SelectedItem);

    if (selectedItem == Language.English)
    {
        ToEnglish();
    }
}

private void ToEnglish()
{
    // Cannot actually implement the UserControl, It can't find the method above.
    // When I've tried to implement UserControl in Print, it can't seem to find it either.
    // I've tried:
    // Print.UserControl1.(_LabelPreview doesn't show_);
    // ^ It might be the completely wrong thing to do so excuse me.
}

我很困惑...我是在 Print(添加 UserControl 的地方)或/和 Form1 中编程吗?!我不希望设计出现在 Form1 中,只是想让其他表单知道选择了什么语言.

注意:我在翻译时一直使用Unicode*

Note: I have been using Unicode when translating*

推荐答案

所以我想出了一个适合我的解决方案!我已经从 Printer.cs 表单中复制过来,在该表单中我使用了一个参数来表示选择的语言,将 strTextBox 初始化为等于 label1 并包含一个 if 语句来查看语言是否为英语(也与 UserControl 一起使用以获取标签等).

So I've come up with a solution that works for me! I've copied across from the Printer.cs form where I have used a parameter to represent the language chosen, initiated strTextBox to equal label1 and included an if statement to see if the language is English (also working with UserControl to get the value of labels etc.).

public Printer(string strTextBox)
{
    InitializeComponent();
    label1.Text = strTextBox;

    if (label1.Text == Language.English)
    {
        UserControl111.Label_Option_Multi = "Please select an option:"; //Simple test
    }
}

表格1

private void Print_Click(object sender, EventArgs e)
{
    string selectedItem = this.ComboBox_Lang.GetItemText(this.ComboBox_Lang.SelectedItem);

    Printer p = new Printer(selectedItem);
    p.Show();
}

用户控制

public string Label_Option_Multi
{
    get
    {
        return Label_Option.Text;
    }
    set
    {
        Label_Option.Text = value;
    }
}

结果,如果我在 Form1.s 中选择英文然后打开 Printer.cs,标签显示为英文并进行相应的翻译.

As a result, if I select English in Form1.s then open up Printer.cs, the label displays English and translates accordingly.

这篇关于使用 ComboBox、UserControl 和一些表单来更改语言(针对整个项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆