我正在尝试制作一个按钮,用于检查 textBox1.Text
是否包含在线 Pastebin 上我的原始文本文件中的任何文本以用于测试目的。
这是我的代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
WebClient client = new WebClient();
string reply = client.DownloadString("https://pastebin.com/raw/0FHx1t5w");
}
还有我的按钮
private void button1_Click(object sender, EventArgs e)
{
string textbox = textBox1.Text;
// Compile error on next line:
if (textbox.Contains(reply))
{
}
}
字符串 reply
被标记为红色并表示:
The name 'reply' does not exist in the current context
是不是因为.Contains
不支持字符串检查?
最佳答案
这是一个范围问题。 reply
是在 Form1
的构造函数中定义的。一旦构造函数退出,reply
变量就不再有效。
您可以在类级别而不是在方法中声明变量。
public partial class Form1 : Form
{
// Declare it here
private string reply;
public Form1()
{
InitializeComponent();
WebClient client = new WebClient();
reply = client.DownloadString("https://pastebin.com/raw/0FHx1t5w");
}
}
然后您将能够从按钮单击事件处理程序访问它。
关于c# - 如何制作 If 语句来检查 textBox1.Text 是否包含字符串 reply = client.DownloadString (address);适本地?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49906755/