简体   繁体   English

如果在C#的文本框中输入了字符,该语句应该是什么?

[英]What should the statements be if a character has been entered in a textbox in C#?

Question 1: 问题1:

As the code states, if spaces are entered in the textbox then the button remains disabled, but if a character or string of characters has been entered when what should be wrote to enable the button? 如代码所示,如​​果在文本框中输入空格,则该按钮将保持禁用状态,但是如果输入了一个字符或字符串,则应写入什么内容才能启用该按钮? I think there should be an if statement but I don't know the statement. 我认为应该有一个if语句,但我不知道该语句。

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
UTF8Encoding utf81 = new UTF8Encoding();
textBox1.Text = BitConverter.ToString(md5.ComputeHash(utf81.GetBytes(textBox30.Text)))

SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
UTF8Encoding utf82 = new UTF8Encoding();
textBox2.Text = BitConverter.ToString(sha1.ComputeHash(utf82.GetBytes(textBox30.Text)))

if (string.IsNullOrWhiteSpace(textBox30.Text))
{
btnHash3.Enabled = false;
}
else
{
btnHash3.Enabled = true;
}

Question 2 问题2

Also on a slightly different note, how do I enable a button once two files are read in from two filestreams and displayed inside two labels? 另外,还有一点不同,一旦从两个文件流中读取了两个文件并在两个标签中显示它们,如何启用按钮?

{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();


FileStream file1 = new FileStream(lblBrowse1.Text, FileMode.Open, FileAccess.Read);
FileStream file2 = new FileStream(lblBrowse2.Text, FileMode.Open, FileAccess.Read);


                byte[] hash1 = md5.ComputeHash(file1);
                byte[] hash2 = md5.ComputeHash(file2);

                file1.Seek(0, SeekOrigin.Begin);
                file2.Seek(0, SeekOrigin.Begin);

                byte[] hash3 = sha1.ComputeHash(file1);
                byte[] hash4 = sha1.ComputeHash(file2);

                file1.Seek(0, SeekOrigin.Begin);
                file2.Seek(0, SeekOrigin.Begin);

                file1.Close();
                file2.Close();

                textBox1.Text = BitConverter.ToString(hash1).Replace("-", "");
                textBox2.Text = BitConverter.ToString(hash2).Replace("-", "");
                textBox6.Text = BitConverter.ToString(hash3).Replace("-", "");
                textBox7.Text = BitConverter.ToString(hash4).Replace("-", "")

                if (textBox1.Text == textBox2.Text
                 && textBox6.Text == textBox7.Text)

                {
                    MessageBox.Show("These two files are identical.");
                }
                else
                {
                    MessageBox.Show("These two files are different.");
                }
            }       

Any help would be much appreciated. 任何帮助将非常感激。

To answer your first question, use .Contains to check for spaces: 要回答您的第一个问题,请使用.Contains检查空格:

bthHash3.Enabled = !myString.Contains(" ");

Since you are just setting a boolean, I collapsed the if into one line. 由于您只是设置一个布尔值,因此将if折叠为一行。 To answer your second, it slightly depends on if you are in a multi-threaded environment. 要回答第二个问题,这在一定程度上取决于您是否处于多线程环境中。 Of course, you could always write the following: 当然,您始终可以编写以下内容:

ReadMyFile(file1);
ReadMyFile(file2);
myButton.Enabled = true; 

Which works since ReadMyFile should block while reading, so the enabled line won't be hit until all the reads are complete. 之所以可行,是因为ReadMyFile在读取时会阻塞,因此在所有读取完成之前不会选中启用的行。 If you are threaded, then do this: 如果您有线程,请执行以下操作:

int completeCount = 0;

void ThreadedRead()
{
    //Read file synchronously
    completedCount++;
    CheckReadCompletion();
}

void CheckReadCompletion()
{
   if (completedCount == 2)
      myButton.Enabled = true;
}

You would start "ThreadedRead" for each file you need to read. 您需要为每个需要读取的文件启动“ ThreadedRead”。 Please let me know if I can clarify anything! 请让我知道是否可以澄清任何事情!

You wouldn't need to do this in the above scenario (because you are just setting the enabled flag) but with complex enough behavior, make sure to put a lock around completedCount and the call to CheckReadCompletion. 在上面的场景中,您不需要这样做(因为您只是在设置enabled标志),但是由于行为足够复杂,请确保对completedCount和对CheckReadCompletion的调用进行锁定。 You could modify it to this: 您可以对此进行修改:

int completeCount = 0;
object completionLock = new object();

void ThreadedRead()
{
    //Read file synchronously

    lock (completionLock)
    {
       completedCount++;
       CheckReadCompletion();
    }
}

void CheckReadCompletion()
{
   if (completedCount == 2)
      myButton.Enabled = true;
}

Actually, you don't need an if statement, you can just use the result of a condition as the value for the Enabled property. 实际上,您不需要if语句,只需将条件的结果用作Enabled属性的值即可。 Trim the string and check if the length is greater than zero to find out if it has any non-space characters: 修剪字符串,并检查长度是否大于零,以查找其是否包含任何非空格字符:

btnHash3.Enabled = textBox30.Text.Trim().Length > 0;

To wait for two results before enabling a button, first create a counter, for example: 要在启用按钮之前等待两个结果,请首先创建一个计数器,例如:

int fileCounter = 0;

After the code that adds the file content to a label, increase the counter and set the button status: 在将文件内容添加到标签的代码之后,增加计数器并设置按钮状态:

fileCounter++;
someButton.Enabled = fileCounter == 2;

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

相关问题 C#文本框限制输入的字符和快捷方式 - C# Textbox limit entered character and shortcut 从文本框中读取输入,然后添加使用按钮编写的内容。 C#VS 2012 - Reading input from a textbox and then adding what has been written using a button. C# VS 2012 c#-如果已创建SQL查询,则在文本框中显示消息 - c# - Display message on textbox if SQL query has been created 网页无法识别已在文本框中输入文本 - web page doesn't recognize text has been entered in textbox 尽管输入了值,为什么textbox.Text为空? - Why is textbox.Text empty although a value has been entered? 如何隐藏标签,直到输入了文本框信息 - How to hide label until textbox information has been entered 使用C#确定输入文本框的字符数 - Determine number of characters entered into textbox with C# 需要帮助编写代码来检查访问数据库中是否已输入日期 C# - Need help writing a code that checks if a date has been entered in the access database or not C# 在c#asp.net中单击了哪些HyperLink? - What HyperLink has been clicked in c# asp.net? 从列表中获取随机输入,该列表已放入带有 C# 和 Selenium 的文本框中 - getting a random input from a list that has been placed into a textbox with C# and Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM