繁体   English   中英

C#如何检查文本框

[英]C# How to check textbox

我是c#的新手,我有一个问题要问你。

我想用一些单词做一个小翻译。

private void button1_Click(object sender, EventArgs e)
{
    string i;
    i = textBox1.Text;
    if (textBox1.Text == bonjour) ;
    {
         label1.Text = "Hello";
    }

    if (textBox1.Text == Hello) ;
    {
        label1.Text = "bonjour";
    }
}

但标签总是“bonjour”。 我哪里做错了?

这适用于一些变化。

     string i;
        i = textBox1.Text;
        if (textBox1.Text == "bonjour") //Remove the ";" and put quotes around string
        {
            label1.Text = "Hello";
        }

        if (textBox1.Text == "Hello") 
        {
            label1.Text = "bonjour";
        }

如果情况无关紧要,我还建议如下:

        string i;
        i = textBox1.Text;
        if (textBox1.Text.ToLower() == "bonjour") 
        {
            label1.Text = "Hello";
        }

        if (textBox1.Text.ToLower() == "hello") 
        {
            label1.Text = "bonjour";
        }
private void button1_Click(object sender, EventArgs e)
{
    string i;
    i = textBox1.Text;
    if (textBox1.Text == "bonjour")
    {
         label1.Text = "Hello";
    }

    if (textBox1.Text == "Hello")
    {
        label1.Text = "bonjour";
    }
}

在测试结束时你不需要分号。 此外,您需要在您正在测试的字符串周围加上双引号“”。

通过你设置它的方式,你也可以这样做:

private void button1_Click(object sender, EventArgs e)
{
    string i;
    i = textBox1.Text;
    if (i == "bonjour")
    {
         label1.Text = "Hello";
    }

    if (i == "Hello")
    {
        label1.Text = "bonjour";
    }
}

此外,您无法测试案例,因此请使用.ToLower(),如Matt Cullinan所建议的那样。

private void button1_Click(object sender, EventArgs e)
{
    string i;
    i = textBox1.Text;
    if(textBox1.Text == "bonjour");
    {
        label1.Text = "Hello";
    }

    else if(textBox1.Text == "Hello");
    {
        label1.Text = "bonjour";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM