私はプログラミングが初めてで、C#を学んでいます。私は、テキストボックスに入力されたテキストを暗号化して解読するプログラムに取り組んでいます。ユーザーがボタンをクリックしてテキストを暗号化または復号化するときに、テキストとパスワードのテキストボックスが空でないことを確認したい。したがって、私は論理条件付き演算子& &と!=を使って、テキストを暗号化するコードが実行される前にテキストボックスを評価しています。私は、テキストボックスのテキストプロパティの値を空の文字列と比較すると、間違った結果を得ているようです。テキストボックスにデータを入れずに暗号化ボタンまたは復号化ボタンをクリックすると、if(text!= "" & & encryptPassword!= "")のように動作し、いずれの場合も暗号化または復号化コードを実行します。私は "equals"を使用して試してきました。かっこで遊んでいました。助けてください。なぜ論理条件演算子&&と!=を使用した文が私に間違った結果を与えているのか?
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");
}
}
}
を試してみてください、あなたは '(暗号)場合'みました。 .. –
いいえ、私はしませんでした。しかし、user3144325は私のエラーを見て、私を修正しました。私はstring.IsNullOrEmpty(文字列)メソッドを使用する必要があります。ありがとう! – CPung