定义一个公共类方法判断winfrom窗体中的控件

像雾像风又像雨 2017-07-20 03:29:48
定义一个公共类方法判断winfrom窗体中的控件,例如需要判断两个textbox控件是否输入一致,能不能写个公共类方法,然后需要做这种判断的时候直接调用
 string judgepwd = this.txt_NewPwd.Text.Trim();
if (judgepwd.Length < 6)
{
MessageBox.Show("密码必须大于6位");
this.txt_NewPwd.Text = "";
this.txt_AgeinNewPwd.Text = "";
return;
}
if (this.txt_NewPwd.Text != this.txt_AgeinNewPwd.Text)
{
MessageBox.Show("两次密码输入不一致");
this.txt_NewPwd.Text = "";
this.txt_AgeinNewPwd.Text = "";
return;
}
string papwd = (@"[a-zA-Z0-9]");
Regex reg = new Regex(papwd);
Match mc = reg.Match(judgepwd);
if (!mc.Success)
{
MessageBox.Show("密码必须为数字加字母的组合");
this.txt_NewPwd.Text = "";
this.txt_AgeinNewPwd.Text = "";
return;
}

类如这几种方法,要怎么定义一个公共类,用的时候直接调用呢?
毕竟我新手。。。
...全文
77 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
好的哦 2017-07-20
  • 打赏
  • 举报
回复
引用 6 楼 u012401478 的回复:
[quote=引用 4 楼 qq_39479818 的回复:] [quote=引用 2 楼 u012401478 的回复:] 可以定义一个方法,把textbox这个控件当做参数。 string a="": public string AAA(textbox){ if (judgepwd.Length < 6) { a = "密码必须大于6位" this.txt_NewPwd.Text = ""; this.txt_AgeinNewPwd.Text = ""; } if (this.txt_NewPwd.Text != this.txt_AgeinNewPwd.Text) { a = "两次密码输入不一致" this.txt_NewPwd.Text = ""; this.txt_AgeinNewPwd.Text = ""; } string papwd = (@"[a-zA-Z0-9]"); Regex reg = new Regex(papwd); Match mc = reg.Match(judgepwd); if (!mc.Success) { a = "密码必须为数字加字母的组合" this.txt_NewPwd.Text = ""; this.txt_AgeinNewPwd.Text = ""; } return a; }
怎么调用呢?[/quote] 调用直接将this.txt_NewPwd作为参数传进去,会返回a,你再到界面messagebox。show(a)[/quote] private void Form1_Load(object sender, EventArgs e) { MessageBox.Show(AAA(this.textBox1, this.textBox2));//调用 } public string AAA(System.Windows.Forms.TextBox tb1,System.Windows.Forms.TextBox tb2) { string a = ""; if (tb1.Text==tb2.Text) { a = "XXXX"; } return a; }同样像楼上的,只是比较像输入的值比较操作的话,把两个参数换成获取的值比较好。
wsh_mar 2017-07-20
  • 打赏
  • 举报
回复
引用 3 楼 qq_39479818 的回复:
[quote=引用 1 楼 wsh_mar 的回复:] 你说的这个是类似于修改密码时候用的吧,需要验证2次密码是否一致。 如果是这样的话,是可以的。 页面处理输入规则。 定义一个方法,挺简单的。 public void xxxx(string str,string str1) { if(str != str1) { //两个值输入的不一样 } }
那么调用的时候怎么调用呢[/quote] 调用的时候如果你是新建了一个Test类。 调用: Test test = new Test() test.XXXX(textBox1.Text,textBox2.Text)
好的哦 2017-07-20
  • 打赏
  • 举报
回复
引用 4 楼 qq_39479818 的回复:
[quote=引用 2 楼 u012401478 的回复:] 可以定义一个方法,把textbox这个控件当做参数。 string a="": public string AAA(textbox){ if (judgepwd.Length < 6) { a = "密码必须大于6位" this.txt_NewPwd.Text = ""; this.txt_AgeinNewPwd.Text = ""; } if (this.txt_NewPwd.Text != this.txt_AgeinNewPwd.Text) { a = "两次密码输入不一致" this.txt_NewPwd.Text = ""; this.txt_AgeinNewPwd.Text = ""; } string papwd = (@"[a-zA-Z0-9]"); Regex reg = new Regex(papwd); Match mc = reg.Match(judgepwd); if (!mc.Success) { a = "密码必须为数字加字母的组合" this.txt_NewPwd.Text = ""; this.txt_AgeinNewPwd.Text = ""; } return a; }
怎么调用呢?[/quote] 调用直接将this.txt_NewPwd作为参数传进去,会返回a,你再到界面messagebox。show(a)
exception92 2017-07-20
  • 打赏
  • 举报
回复
定义个公共方法了,类似:

  public bool isEqual(string value1, string value2)
        {
            return value1 == value2;
        }
调用: if (!isEqual(this.txt_NewPwd.Text ,this.txt_AgeinNewPwd.Text)) 像, string papwd = (@"[a-zA-Z0-9]"); Regex reg = new Regex(papwd); Match mc = reg.Match(judgepwd); 这种代码,封装成一个方法,返回true或者false 直接传入值 判断就行了。 其实 你的那种方式也挺直观的。
  • 打赏
  • 举报
回复
引用 2 楼 u012401478 的回复:
可以定义一个方法,把textbox这个控件当做参数。 string a="": public string AAA(textbox){ if (judgepwd.Length < 6) { a = "密码必须大于6位" this.txt_NewPwd.Text = ""; this.txt_AgeinNewPwd.Text = ""; } if (this.txt_NewPwd.Text != this.txt_AgeinNewPwd.Text) { a = "两次密码输入不一致" this.txt_NewPwd.Text = ""; this.txt_AgeinNewPwd.Text = ""; } string papwd = (@"[a-zA-Z0-9]"); Regex reg = new Regex(papwd); Match mc = reg.Match(judgepwd); if (!mc.Success) { a = "密码必须为数字加字母的组合" this.txt_NewPwd.Text = ""; this.txt_AgeinNewPwd.Text = ""; } return a; }
怎么调用呢?
  • 打赏
  • 举报
回复
引用 1 楼 wsh_mar 的回复:
你说的这个是类似于修改密码时候用的吧,需要验证2次密码是否一致。 如果是这样的话,是可以的。 页面处理输入规则。 定义一个方法,挺简单的。 public void xxxx(string str,string str1) { if(str != str1) { //两个值输入的不一样 } }
那么调用的时候怎么调用呢
好的哦 2017-07-20
  • 打赏
  • 举报
回复
可以定义一个方法,把textbox这个控件当做参数。 string a="": public string AAA(textbox){ if (judgepwd.Length < 6) { a = "密码必须大于6位" this.txt_NewPwd.Text = ""; this.txt_AgeinNewPwd.Text = ""; } if (this.txt_NewPwd.Text != this.txt_AgeinNewPwd.Text) { a = "两次密码输入不一致" this.txt_NewPwd.Text = ""; this.txt_AgeinNewPwd.Text = ""; } string papwd = (@"[a-zA-Z0-9]"); Regex reg = new Regex(papwd); Match mc = reg.Match(judgepwd); if (!mc.Success) { a = "密码必须为数字加字母的组合" this.txt_NewPwd.Text = ""; this.txt_AgeinNewPwd.Text = ""; } return a; }
wsh_mar 2017-07-20
  • 打赏
  • 举报
回复
你说的这个是类似于修改密码时候用的吧,需要验证2次密码是否一致。 如果是这样的话,是可以的。 页面处理输入规则。 定义一个方法,挺简单的。 public void xxxx(string str,string str1) { if(str != str1) { //两个值输入的不一样 } }

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧