1

我希望我的消息框根据用户选择的语言显示信息。我根据语言按钮单击更改所有按钮和标签文本。但是如何使我的消息框根据此语言按钮单击显示不同的信息?例如,如果我有另一个按钮,当我单击它时,会显示正确的消息框,我希望这个消息框以不同的语言显示以供不同的用户选择。我的所有文本都在资源中。贝娄是我的代码。

private void btnLngEnglish_Click(object sender, EventArgs e)
    {
        CultureInfo ci = new CultureInfo("en-US");
        Assembly a = Assembly.Load("read_display");
        ResourceManager rm = new ResourceManager("read_display.language.languageRes", a);
        button7.Text = rm.GetString("file", ci);
        button4.Text = rm.GetString("timecount", ci);
        button6.Text = rm.GetString("daterange", ci);
        button3.Text = rm.GetString("specdate", ci);
        button1.Text = rm.GetString("phrasesearch", ci);
        button5.Text = rm.GetString("higherval", ci);
        label3.Text = rm.GetString("langsel", ci);
        label5.Text = rm.GetString("rowcount", ci);
        label4.Text = rm.GetString("timeElapsed", ci);
        label1.Text = rm.GetString("filterdate", ci);
        label2.Text = rm.GetString("hide", ci);
    }

    private void btnLangPolish_Click(object sender, EventArgs e)
    {
        CultureInfo ci = new CultureInfo("pl-PL");
        Assembly a = Assembly.Load("read_display");
        ResourceManager rm = new ResourceManager("read_display.language.languageResPL", a);
        button7.Text = rm.GetString("file", ci);
        button4.Text = rm.GetString("timecount", ci);
        button6.Text = rm.GetString("daterange", ci);
        button3.Text = rm.GetString("specdate", ci);
        button1.Text = rm.GetString("phrasesearch", ci);
        button5.Text = rm.GetString("higherval", ci);
        label3.Text = rm.GetString("langsel", ci);
        label5.Text = rm.GetString("rowcount", ci);
        label4.Text = rm.GetString("timeElapsed", ci);
        label1.Text = rm.GetString("filterdate", ci);
        label2.Text = rm.GetString("hide", ci);
    }

编辑:

private void button1_Click(object sender, EventArgs e)
    {
        string searchString = textBox8.Text;
        if (String.IsNullOrEmpty(textBox8.Text))
        {
            //Here I would like to diplay messages in two different language, based on previous language button click. Can I do it??
            MessageBox.Show("Enter value to filter");
        }
        else
        {
            bool found = false;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[1].Value.ToString().Contains(searchString))
                {
                    row.DefaultCellStyle.BackColor = System.Drawing.Color.MediumPurple;
                    row.Selected = true;
                    found = true;
                }
                if (row.Cells[2].Value.ToString().Contains(searchString))
                {
                    row.DefaultCellStyle.BackColor = System.Drawing.Color.MediumPurple;
                    found = true;
                }
            }
            if (!found)
            {
                //Here I would like to do same thing
                MessageBox.Show("Value was not found");
            }
        }
    }
4

1 回答 1

1

首先,我会更改所有这些按钮的名称以反映它们的实际含义。这更像是一条建议。

不管怎样,你不能打电话MessageBox.Show(rm.GetString("messageboxData", ci))吗?

EDIT1(见评论):我的意思是:

class MyClass
{
    CultureInfo currentCultureInfo;
    public MyClass()
    {
        //defaulting to en-US
        currentCultureInfo = new CultureInfo("en-US");
    }

    public void SetLanguageToEnglish()
    {
        currentCultureInfo = new CultureInfo("en-US");
    }

    public void SetLanguageToItalian()
    {
        currentCultureInfo = new CultureInfo("it-IT");
    }

    public string GetTranslation(string s)
    {
        //By the way, you should to the same to 'a' and 'rm', since they don't need to be instantiated each time. But I'll use your code to avoid confusion.
        Assembly a = Assembly.Load("read_display");
        ResourceManager rm = new ResourceManager("read_display.language.languageRes", a);
        return rm.GetString(s, currentCultureInfo);
    }
}
于 2013-07-22T08:31:16.637 回答