Question

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

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

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.

Was it helpful?

Solution

To answer your first question, use .Contains to check for spaces:

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

Since you are just setting a boolean, I collapsed the if into one line. 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. 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. 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. 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;
}

OTHER TIPS

Actually, you don't need an if statement, you can just use the result of a condition as the value for the Enabled property. 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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top