使用组合框,用户控件和某些形式的改变语言(整个项目)组合、控件、形式、语言

2023-09-07 02:15:22 作者:-/不管怎样活着就好

在我开始之前,我研究,似乎无法找到任何东西。注意:我是很新的用户控件所以这可能是为什么它被证明是困难。的

我在Form1的组合框被选定的时候,用户可以对21种语言可供选择之间切换。我创建了一个包含标签,按钮和复选框一个用户控件 - 增加了一种叫做打印。

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

Form1中:

如果字符串值英语在组合框中选择,调用一个方法 - 这里就是我想改变语言等形式

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

我很困惑......在打印难道我的程序(其中用户控件添加)或/和Form1中?我不想设计出现在Form1中,而只是想让其他形式知道用什么语言已被选中。

注:我一直在使用统一code转换时*

Note: I have been using Unicode when translating*

推荐答案

所以,我想出了一个解决方案,它为我的作品!我对面的Printer.cs形式,其中我已经使用的参数,重新present语言选择复制,发起strTextBox等于label1和包括if语句,看语言是英语(也正在与用户控件来得到标签等的值。)。

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

Form1中

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.