我在一个命名空间新增了一个类放入一个正则表达式的验证方法去验证txtID.text,当在
txtID输入空格和一些乱七八糟的符号就会出现提示框报错,然后我的问题就是,报错过后就停止程序的运行
,return好像不行,只能停止当前的类的方法,我想直接停止以下所有代码的运行怎么办呢????
namespace WindowsFormsApplication2
{
public class yanzheng //新增一个类放入正则表达式的方法
{
public void zzbds(string a)
{
string s_reg = @"^[A-Za-z0-9]+$";
string s = a;
Regex reg = new Regex(s_reg);
if (reg.IsMatch(s))
{
}
else
{
MessageBox.Show("请输入数字或字母组成的ID");
return ;
}
}
} public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string qx;
private void button1_Click(object sender, EventArgs e)
{
string id = txtID.Text.Trim();
string pw = txtPW.Text.Trim();
yanzheng a = new yanzheng();
a.zzbds(txtID.Text); SqlConnection conn = new SqlConnection("Data Source=ROY;Initial Catalog=wuhu;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from [user] where ID='" + id + "'";
cmd.Connection = conn;
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{ if (pw == sdr["PW"].ToString().Trim())
{
MessageBox.Show("登陆成功");
qx = sdr["QX"].ToString().Trim();
new Form2().Show();
this.Hide();
}
else
{
MessageBox.Show("失败");
}
}
else if (id == "")
{
MessageBox.Show("请输入ID");
}
else if (pw == "")
{
MessageBox.Show("请输入PW");
}
else
{
MessageBox.Show("不存在此用户");
txtID.Text = "";
txtPW.Text = "";
}
conn.Close();
sdr.Close();
}
txtID输入空格和一些乱七八糟的符号就会出现提示框报错,然后我的问题就是,报错过后就停止程序的运行
,return好像不行,只能停止当前的类的方法,我想直接停止以下所有代码的运行怎么办呢????
namespace WindowsFormsApplication2
{
public class yanzheng //新增一个类放入正则表达式的方法
{
public void zzbds(string a)
{
string s_reg = @"^[A-Za-z0-9]+$";
string s = a;
Regex reg = new Regex(s_reg);
if (reg.IsMatch(s))
{
}
else
{
MessageBox.Show("请输入数字或字母组成的ID");
return ;
}
}
} public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string qx;
private void button1_Click(object sender, EventArgs e)
{
string id = txtID.Text.Trim();
string pw = txtPW.Text.Trim();
yanzheng a = new yanzheng();
a.zzbds(txtID.Text); SqlConnection conn = new SqlConnection("Data Source=ROY;Initial Catalog=wuhu;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from [user] where ID='" + id + "'";
cmd.Connection = conn;
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{ if (pw == sdr["PW"].ToString().Trim())
{
MessageBox.Show("登陆成功");
qx = sdr["QX"].ToString().Trim();
new Form2().Show();
this.Hide();
}
else
{
MessageBox.Show("失败");
}
}
else if (id == "")
{
MessageBox.Show("请输入ID");
}
else if (pw == "")
{
MessageBox.Show("请输入PW");
}
else
{
MessageBox.Show("不存在此用户");
txtID.Text = "";
txtPW.Text = "";
}
conn.Close();
sdr.Close();
}
虽然很简单,但是既然你问了。public bool zzbds(string a)
{
string s_reg = @"^[A-Za-z0-9]+$";
string s = a;
Regex reg = new Regex(s_reg);
if (reg.IsMatch(s))
{
return true;
}
else
{
MessageBox.Show("请输入数字或字母组成的ID");
return false
}
}
然后button1中
if(!a.zzbds(txtID.Text))
return;