2016-10-12 1 views
0

Je suis novice en programmation et j'apprends le C#. Je travaille sur un programme qui crypte et déchiffre un texte entré dans une zone de texte. Je veux m'assurer que les zones de texte et mot de passe ne sont pas vides lorsque l'utilisateur clique sur un bouton pour crypter ou décrypter le texte. Donc, j'utilise les opérateurs logiques conditionnels & & et! = Pour évaluer les zones de texte avant que le code pour crypter le texte soit exécuté. Il semble que j'obtiens de mauvais résultats lorsque je compare la valeur de la propriété text des textboxes à une chaîne vide. Lorsque je clique sur les boutons chiffrer ou déchiffrer sans aucune donnée dans les zones de texte, l'instruction: if (text! = "" & & encryptPassword! = "") Se comporte comme si chaque test est vrai et exécute le code crypté ou déchiffré quand même. J'ai essayé d'utiliser "égal", jouant avec les parenthèses, et inversant l'ordre en vain. S'il vous plaît aider.Pourquoi Si les instructions utilisant les opérateurs conditionnels logiques && et! = Me donnent le mauvais résultat?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace CryptoMakerII 
{ 
    public partial class CryptoMakerII : Form 
    { 

     public CryptoMakerII() 
     { 
      InitializeComponent(); 

     } 

     private void UpdateControls(string crypto) 
     { 
      if (crypto == "Encrypt") 
      { 
       lblEncryptPassword.Visible = false; 
       txtEncryptPassword.Visible = false; 
       btnEncrypt.Visible = false; 
       txtDecryptPassword.Visible = true; 
       btnDecrypt.Visible = true; 
       lblDecryptPassword.Visible = true; 
       lblText.Text = "Encrypted Text"; 

       //txtDecryptPassword.Text = " "; 

       //txtEncryptPassword.Text = " "; 

      } 
      else 
      { 

       lblEncryptPassword.Visible = true; 
       txtEncryptPassword.Visible = true; 
       btnEncrypt.Visible = true; 
       txtDecryptPassword.Visible = false; 
       btnDecrypt.Visible = false; 
       lblDecryptPassword.Visible = false; 
       lblText.Text = "Text to Encrypt"; 
       txtDecryptPassword.Text = " "; 
       txtEncryptPassword.Text = " "; 





      } 
     } 
     private void btnEncrypt_Click(object sender, EventArgs e) 
     { 
      DES_Crypto desCrypto = new DES_Crypto(); 
      string text = txtText.Text; 
      string encryptPassword = txtEncryptPassword.Text; 

      if (text != " " && encryptPassword != " ") 
      { 
       string encryptedText = desCrypto.EncryptString(text, encryptPassword); 
       txtText.Text = encryptedText; 
       UpdateControls("Encrypt"); 

      } 
      else 

      { 
       MessageBox.Show("Please enter text to encrypt and password"); 
      } 

     } 



     private void btnDecrypt_Click(object sender, EventArgs e) 
     { 
      DES_Crypto desCrypto = new DES_Crypto(); 
      if (txtText.Text != " " && txtDecryptPassword.Text != " ") 

       if (txtDecryptPassword.Text == txtEncryptPassword.Text) 
        { 
         string decryptedText = desCrypto.DecryptString(txtText.Text, txtDecryptPassword.Text); 
         txtText.Text = decryptedText; 
         UpdateControls("Decrypt"); 
        } 
       else 
       { 
        MessageBox.Show("The password is incorrect!"); 
       }    
       else 
       MessageBox.Show("Please enter password to decrypt"); 
     } 
    } 

} 
+0

Avez-vous essayé 'si (Crypto)'. .. –

+1

Non, Michal. Mais user3144325 a vu mon erreur et m'a corrigé. J'aurais dû utiliser la méthode string.IsNullOrEmpty (string). Merci! – CPung

Répondre

2

Vous vérifiez si la chaîne est exactement égale à 1 caractère d'espace, ne vérifiant pas si elle est vide. C# a construit dans la méthode pour vérifier si une chaîne est vide ou non:

string.IsNullOrEmpty(str) 

Ainsi, au lieu de

if (txtText.Text != " " && txtDecryptPassword.Text != " ") 

Essayez

if (!string.IsNullOrEmpty(txtText.Text) && !string.IsNullOrEmpty(txtDecryptPassword.Text)) 
+0

Ça a marché! Merci beaucoup!! – CPung