如何比较C#申请表中的两个文本框? [英] How do I compare two text boxes in C# application form?

查看:96
本文介绍了如何比较C#申请表中的两个文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的初学者,我一直在研究一个程序。我遇到了以下问题:



一个文本框正在从文本文件中读取一个随机单词,而另一个文本框用于输入他的单词。

如果 BOTH 字样相同,那么我想显示一条消息,说明用户是成功的。



到目前为止我使用的代码在控制台上没有显示任何内容。



我尝试过以下代码:



我尝试了什么:



I am a complete beginner in C# and I have been working on a program. I have encountered the following problem:

One text box is reading a random word from a text file, while the other is there for the user to input his word.
If BOTH words are the same, then I want to display a message stating that the user was successful.

The code I have used so far does not display anything on the console.

I have tried the following code:

What I have tried:

private void textBox4_TextChanged(object sender, EventArgs e)
       {
           if (textBox4.Text == textBox3.Text)
           {
               Console.Write("Success");
           }
       }

推荐答案

开始,你应该修剪以避免空白

to start, you should trim to avoid whitespace
if(textBox4.Text.Trim() == textBox3.Text.Trim())



接下来,你确定吗?它们完全相同(大小写等)?


next, are you sure they are exactly the same (case, etc.)?

string t4 = textBox4.Text.Trim();
string t3 = textBox3.Text.Trim();
bool result = t3.Equals(t4, StringComparison.Ordinal);


As NotPolitcallyCorrect表示Winforms不提供控制台O / P尽管你可以在VS IDE的输出窗口中看到它。阅读有关winforms的书籍以获得更多知识。至于你目前的问题。

修改你的代码如下。



As NotPolitcallyCorrect said Winforms don't provide Console O/P although you can see it in Output window in VS IDE. Do read books on winforms to get more knowledge. As for your current problem.
Modify your code as below.

private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (textBox4.Text == textBox3.Text)
            {
                MessageBox.Show("Success");
            }
        }


这篇关于如何比较C#申请表中的两个文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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