CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

关于TextBox问题(十万火急)

楼主ntmit(自由的呼吸)2006-11-11 08:30:37 在 .NET技术 / C# 提问

代码如下:  
  using   System;  
  using   System.Drawing;  
  using   System.Collections;  
  using   System.ComponentModel;  
  using   System.Windows.Forms;  
  using   System.Data;  
  using   System.Data.SqlClient;  
   
  namespace   member  
  {  
  ///   <summary>  
  ///   login   的摘要说明。  
  ///   </summary>  
  public   class   login   :   System.Windows.Forms.Form  
  {  
  private   System.Windows.Forms.TextBox   textBox1;  
  private   System.Windows.Forms.TextBox   textBox2;  
  private   System.Windows.Forms.TextBox   textBox3;  
  private   System.Windows.Forms.Button   button1;  
  private   System.Windows.Forms.Button   button2;  
  private   System.Windows.Forms.Label   label1;  
  private   System.Windows.Forms.Label   label2;  
  private   System.Windows.Forms.Label   label3;  
  ///   <summary>  
  ///   必需的设计器变量。  
  ///   </summary>  
  private   System.ComponentModel.Container   components   =   null;  
   
  public   login()  
  {  
  //  
  //   Windows   窗体设计器支持所必需的  
  //  
  InitializeComponent();  
   
  //  
  //   TODO:   在   InitializeComponent   调用后添加任何构造函数代码  
  //  
  }  
   
  ///   <summary>  
  ///   清理所有正在使用的资源。  
  ///   </summary>  
  protected   override   void   Dispose(   bool   disposing   )  
  {  
  if(   disposing   )  
  {  
  if(components   !=   null)  
  {  
  components.Dispose();  
  }  
  }  
  base.Dispose(   disposing   );  
  }  
   
  #region   Windows   Form   Designer   generated   code  
  ///   <summary>  
  ///   设计器支持所需的方法   -   不要使用代码编辑器修改  
  ///   此方法的内容。  
  ///   </summary>  
  private   void   InitializeComponent()  
  {  
  System.Resources.ResourceManager   resources   =   new   System.Resources.ResourceManager(typeof(login));  
  this.button1   =   new   System.Windows.Forms.Button();  
  this.button2   =   new   System.Windows.Forms.Button();  
  this.textBox1   =   new   System.Windows.Forms.TextBox();  
  this.textBox2   =   new   System.Windows.Forms.TextBox();  
  this.textBox3   =   new   System.Windows.Forms.TextBox();  
  this.label1   =   new   System.Windows.Forms.Label();  
  this.label2   =   new   System.Windows.Forms.Label();  
  this.label3   =   new   System.Windows.Forms.Label();  
  this.SuspendLayout();  
  //    
  //   button1  
  //    
  this.button1.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.button1.FlatStyle   =   System.Windows.Forms.FlatStyle.Flat;  
  this.button1.Font   =   new   System.Drawing.Font("宋体",   9F,   System.Drawing.FontStyle.Regular,   System.Drawing.GraphicsUnit.Point,   ((System.Byte)(134)));  
  this.button1.Location   =   new   System.Drawing.Point(280,   184);  
  this.button1.Name   =   "button1";  
  this.button1.Size   =   new   System.Drawing.Size(56,   24);  
  this.button1.TabIndex   =   3;  
  this.button1.Text   =   "确     定";  
  this.button1.Click   +=   new   System.EventHandler(this.button1_Click);  
  //    
  //   button2  
  //    
  this.button2.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.button2.FlatStyle   =   System.Windows.Forms.FlatStyle.Flat;  
  this.button2.Location   =   new   System.Drawing.Point(368,   184);  
  this.button2.Name   =   "button2";  
  this.button2.Size   =   new   System.Drawing.Size(56,   24);  
  this.button2.TabIndex   =   4;  
  this.button2.Text   =   "取     消";  
  this.button2.Click   +=   new   System.EventHandler(this.button2_Click);  
  //    
  //   textBox1  
  //    
  this.textBox1.BorderStyle   =   System.Windows.Forms.BorderStyle.FixedSingle;  
  this.textBox1.Location   =   new   System.Drawing.Point(272,   104);  
  this.textBox1.Name   =   "textBox1";  
  this.textBox1.Size   =   new   System.Drawing.Size(160,   21);  
  this.textBox1.TabIndex   =   0;  
  this.textBox1.Text   =   "";  
  //    
  //   textBox2  
  //    
  this.textBox2.BorderStyle   =   System.Windows.Forms.BorderStyle.FixedSingle;  
  this.textBox2.Enabled   =   false;  
  this.textBox2.Location   =   new   System.Drawing.Point(272,   128);  
  this.textBox2.Name   =   "textBox2";  
  this.textBox2.Size   =   new   System.Drawing.Size(160,   21);  
  this.textBox2.TabIndex   =   1;  
  this.textBox2.Text   =   "";  
  //    
  //   textBox3  
  //    
  this.textBox3.BorderStyle   =   System.Windows.Forms.BorderStyle.FixedSingle;  
  this.textBox3.Location   =   new   System.Drawing.Point(272,   152);  
  this.textBox3.Name   =   "textBox3";  
  this.textBox3.PasswordChar   =   '*';  
  this.textBox3.Size   =   new   System.Drawing.Size(160,   21);  
  this.textBox3.TabIndex   =   2;  
  this.textBox3.Text   =   "";  
  //    
  //   label1  
  //    
  this.label1.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.label1.Location   =   new   System.Drawing.Point(208,   109);  
  this.label1.Name   =   "label1";  
  this.label1.Size   =   new   System.Drawing.Size(72,   16);  
  this.label1.TabIndex   =   5;  
  this.label1.Text   =   "用户代码:";  
  //    
  //   label2  
  //    
  this.label2.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.label2.Location   =   new   System.Drawing.Point(208,   133);  
  this.label2.Name   =   "label2";  
  this.label2.Size   =   new   System.Drawing.Size(72,   16);  
  this.label2.TabIndex   =   6;  
  this.label2.Text   =   "用户姓名:";  
  //    
  //   label3  
  //    
  this.label3.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.label3.Location   =   new   System.Drawing.Point(208,   157);  
  this.label3.Name   =   "label3";  
  this.label3.Size   =   new   System.Drawing.Size(72,   16);  
  this.label3.TabIndex   =   7;  
  this.label3.Text   =   "用户密码:";  
  //    
  //   login  
  //    
  this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);  
  this.BackgroundImage   =   ((System.Drawing.Bitmap)(resources.GetObject("$this.BackgroundImage")));  
  this.ClientSize   =   new   System.Drawing.Size(448,   229);  
  this.Controls.AddRange(new   System.Windows.Forms.Control[]   {  
      this.textBox3,  
      this.textBox2,  
      this.textBox1,  
      this.button2,  
      this.button1,  
      this.label1,  
      this.label2,  
      this.label3});  
  this.MaximizeBox   =   false;  
  this.MinimizeBox   =   false;  
  this.Name   =   "login";  
  this.Text   =   "会员登录";  
  this.ResumeLayout(false);  
   
  }  
  #endregion  
   
  private   void   button1_Click(object   sender,   System.EventArgs   e)  
  {  
   
  if   (this.CheckUser(this.textBox1.Text.Trim(),this.textBox2.Text.Trim(),this.textBox3.Text.Trim())==true)  
  {  
  winmain   winmain   =   new   winmain();  
  classa.Code   =textBox1.Text;  
  classa.User   =textBox2.Text;  
  winmain.Show();  
  this.Hide();  
  }  
  else  
  {  
  MessageBox.Show("您输入的帐号或密码有误,请重新登录!","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);  
  }  
   
  }  
   
  private   void   button2_Click(object   sender,   System.EventArgs   e)  
  {  
  Application.Exit   ();  
  }  
   
   
  private   void   textBox1_Enter(object   sender,   System.EventArgs   e)  
  {  
  Application.Exit   ();  
  }  
   
   
  private   bool   CheckUser(string   Code,string   UserName,string   Password)  
  {  
  bool   IsIn=false;  
  string   source="server=localhost;"+  
  "uid=sa;pwd=ntdba;"+  
  "database=zyhx";  
  string   select="select   *   from   admin   where   code='"   +   Code.Trim()   +   "'   and   passwd='"   +   Password.Trim()   +   "'";  
  try  
  {  
  SqlConnection   conn   =   new   SqlConnection(source);  
  conn.Open();  
  SqlCommand   cmd   =   new   SqlCommand(select,conn);  
  SqlDataReader   sql   =   cmd.ExecuteReader();  
  while   (sql.Read())  
  {  
  textBox2.Text   =   sql["user"].ToString   ();  
  IsIn=true;  
  }  
  }  
  catch(Exception   ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  
  //conn.Close();  
  return   IsIn;  
  }  
  [STAThread]  
   
  static   void   Main()    
  {  
  login   newmain=new   login();  
  Application.Run(newmain);  
  }  
   
   
  }  
  }  
   
   
  我想实现,当textbox1输入完毕后,textbox2自动从数据库中读取数据并显示。 问题点数:20、回复次数:30Top

1 楼ntmit(自由的呼吸)回复于 2006-11-11 08:32:01 得分 0

各位大虾,急啊~~~~Top

2 楼ntmit(自由的呼吸)回复于 2006-11-11 09:06:03 得分 0

急啊,在线等~~~~Top

3 楼lizhizhe2000(武安侯)回复于 2006-11-11 09:08:07 得分 20

当textbox1失去焦点时从数据库中取值不就行了!Top

4 楼ntmit(自由的呼吸)回复于 2006-11-11 09:10:21 得分 0

问题我不知道如何使TEXTBOX1失去焦点Top

5 楼bjgzxx(食人一族)回复于 2006-11-11 09:23:47 得分 0

那也不知道你什么时候输入完吗?Top

6 楼ntmit(自由的呼吸)回复于 2006-11-11 09:26:31 得分 0

是这样吗?  
   
  private   void   textBox1_Leave(object   sender,   System.EventArgs   e)  
   
  {  
  ……  
  }Top

7 楼free_wang()回复于 2006-11-11 09:28:25 得分 0

upTop

8 楼ntmit(自由的呼吸)回复于 2006-11-11 09:48:57 得分 0

private   void   textBox1_Leave(object   sender,   System.EventArgs   e)  
   
  {  
  textBox2.Text   =   "aa";  
  }  
   
  我加了怎么无效啊?Top

9 楼david_anwei(家九)回复于 2006-11-11 10:19:34 得分 0

private   void   WipTextBox_Leave(object   sender,   System.EventArgs   e)  
  {  
  if(!this.ReadOnly)  
  {  
  textBox2.Text   =   "aa";  
  }  
  }  
   
  Top

10 楼scow(怡红快绿之小橙子|和谐权是第4代人权)回复于 2006-11-11 10:21:55 得分 0

怎么会无效,在textBox1输入,再把焦点移到textBox2或界面上其他控件,这个事件就会触发Top

11 楼ntmit(自由的呼吸)回复于 2006-11-11 11:00:34 得分 0

问题是它没有触发啊。我把代码贴出来,帮我看看。  
  using   System;  
  using   System.Drawing;  
  using   System.Collections;  
  using   System.ComponentModel;  
  using   System.Windows.Forms;  
  using   System.Data;  
  using   System.Data.SqlClient;  
   
  namespace   member  
  {  
  ///   <summary>  
  ///   login   的摘要说明。  
  ///   </summary>  
  public   class   login   :   System.Windows.Forms.Form  
  {  
  private   System.Windows.Forms.TextBox   textBox1;  
  private   System.Windows.Forms.TextBox   textBox2;  
  private   System.Windows.Forms.TextBox   textBox3;  
  private   System.Windows.Forms.Button   button1;  
  private   System.Windows.Forms.Button   button2;  
  private   System.Windows.Forms.Label   label1;  
  private   System.Windows.Forms.Label   label2;  
  private   System.Windows.Forms.Label   label3;  
  ///   <summary>  
  ///   必需的设计器变量。  
  ///   </summary>  
  private   System.ComponentModel.Container   components   =   null;  
   
  public   login()  
  {  
  //  
  //   Windows   窗体设计器支持所必需的  
  //  
  InitializeComponent();  
   
  //  
  //   TODO:   在   InitializeComponent   调用后添加任何构造函数代码  
  //  
  }  
   
  ///   <summary>  
  ///   清理所有正在使用的资源。  
  ///   </summary>  
  protected   override   void   Dispose(   bool   disposing   )  
  {  
  if(   disposing   )  
  {  
  if(components   !=   null)  
  {  
  components.Dispose();  
  }  
  }  
  base.Dispose(   disposing   );  
  }  
   
  #region   Windows   Form   Designer   generated   code  
  ///   <summary>  
  ///   设计器支持所需的方法   -   不要使用代码编辑器修改  
  ///   此方法的内容。  
  ///   </summary>  
  private   void   InitializeComponent()  
  {  
  System.Resources.ResourceManager   resources   =   new   System.Resources.ResourceManager(typeof(login));  
  this.button1   =   new   System.Windows.Forms.Button();  
  this.button2   =   new   System.Windows.Forms.Button();  
  this.textBox1   =   new   System.Windows.Forms.TextBox();  
  this.textBox2   =   new   System.Windows.Forms.TextBox();  
  this.textBox3   =   new   System.Windows.Forms.TextBox();  
  this.label1   =   new   System.Windows.Forms.Label();  
  this.label2   =   new   System.Windows.Forms.Label();  
  this.label3   =   new   System.Windows.Forms.Label();  
  this.SuspendLayout();  
  //    
  //   button1  
  //    
  this.button1.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.button1.FlatStyle   =   System.Windows.Forms.FlatStyle.Flat;  
  this.button1.Font   =   new   System.Drawing.Font("宋体",   9F,   System.Drawing.FontStyle.Regular,   System.Drawing.GraphicsUnit.Point,   ((System.Byte)(134)));  
  this.button1.Location   =   new   System.Drawing.Point(280,   184);  
  this.button1.Name   =   "button1";  
  this.button1.Size   =   new   System.Drawing.Size(56,   24);  
  this.button1.TabIndex   =   3;  
  this.button1.Text   =   "确     定";  
  this.button1.Click   +=   new   System.EventHandler(this.button1_Click);  
  //    
  //   button2  
  //    
  this.button2.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.button2.FlatStyle   =   System.Windows.Forms.FlatStyle.Flat;  
  this.button2.Location   =   new   System.Drawing.Point(368,   184);  
  this.button2.Name   =   "button2";  
  this.button2.Size   =   new   System.Drawing.Size(56,   24);  
  this.button2.TabIndex   =   4;  
  this.button2.Text   =   "取     消";  
  this.button2.Click   +=   new   System.EventHandler(this.button2_Click);  
  //    
  //   textBox1  
  //    
  this.textBox1.BorderStyle   =   System.Windows.Forms.BorderStyle.FixedSingle;  
  this.textBox1.Location   =   new   System.Drawing.Point(272,   104);  
  this.textBox1.Name   =   "textBox1";  
  this.textBox1.Size   =   new   System.Drawing.Size(160,   21);  
  this.textBox1.TabIndex   =   0;  
  this.textBox1.Text   =   "";  
  //    
  //   textBox2  
  //    
  this.textBox2.BorderStyle   =   System.Windows.Forms.BorderStyle.FixedSingle;  
  this.textBox2.Enabled   =   false;  
  this.textBox2.Location   =   new   System.Drawing.Point(272,   128);  
  this.textBox2.Name   =   "textBox2";  
  this.textBox2.Size   =   new   System.Drawing.Size(160,   21);  
  this.textBox2.TabIndex   =   1;  
  this.textBox2.Text   =   "";  
  //    
  //   textBox3  
  //    
  this.textBox3.BorderStyle   =   System.Windows.Forms.BorderStyle.FixedSingle;  
  this.textBox3.Location   =   new   System.Drawing.Point(272,   152);  
  this.textBox3.Name   =   "textBox3";  
  this.textBox3.PasswordChar   =   '*';  
  this.textBox3.Size   =   new   System.Drawing.Size(160,   21);  
  this.textBox3.TabIndex   =   2;  
  this.textBox3.Text   =   "";  
  //    
  //   label1  
  //    
  this.label1.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.label1.Location   =   new   System.Drawing.Point(208,   109);  
  this.label1.Name   =   "label1";  
  this.label1.Size   =   new   System.Drawing.Size(72,   16);  
  this.label1.TabIndex   =   5;  
  this.label1.Text   =   "用户代码:";  
  //    
  //   label2  
  //    
  this.label2.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.label2.Location   =   new   System.Drawing.Point(208,   133);  
  this.label2.Name   =   "label2";  
  this.label2.Size   =   new   System.Drawing.Size(72,   16);  
  this.label2.TabIndex   =   6;  
  this.label2.Text   =   "用户姓名:";  
  //    
  //   label3  
  //    
  this.label3.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.label3.Location   =   new   System.Drawing.Point(208,   157);  
  this.label3.Name   =   "label3";  
  this.label3.Size   =   new   System.Drawing.Size(72,   16);  
  this.label3.TabIndex   =   7;  
  this.label3.Text   =   "用户密码:";  
  //    
  //   login  
  //    
  this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);  
  this.BackgroundImage   =   ((System.Drawing.Bitmap)(resources.GetObject("$this.BackgroundImage")));  
  this.ClientSize   =   new   System.Drawing.Size(448,   229);  
  this.Controls.AddRange(new   System.Windows.Forms.Control[]   {  
      this.textBox3,  
      this.textBox2,  
      this.textBox1,  
      this.button2,  
      this.button1,  
      this.label1,  
      this.label2,  
      this.label3});  
  this.MaximizeBox   =   false;  
  this.MinimizeBox   =   false;  
  this.Name   =   "login";  
  this.Text   =   "会员登录";  
  this.ResumeLayout(false);  
   
  }  
  #endregion  
   
  private   void   button1_Click(object   sender,   System.EventArgs   e)  
  {  
   
  if   (this.CheckUser(this.textBox1.Text.Trim(),this.textBox2.Text.Trim(),this.textBox3.Text.Trim())==true)  
  {  
  winmain   winmain   =   new   winmain();  
  classa.Code   =textBox1.Text;  
  classa.User   =textBox2.Text;  
  winmain.Show();  
  this.Hide();  
  }  
  else  
  {  
  MessageBox.Show("您输入的帐号或密码有误,请重新登录!","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);  
  }  
   
  }  
   
  private   void   button2_Click(object   sender,   System.EventArgs   e)  
  {  
  Application.Exit   ();  
  }  
   
   
  private   void   textBox1_Enter(object   sender,   System.EventArgs   e)  
  {  
  Application.Exit   ();  
  }  
   
   
  private   bool   CheckUser(string   Code,string   UserName,string   Password)  
  {  
  bool   IsIn=false;  
  string   source="server=localhost;"+  
  "uid=sa;pwd=ntdba;"+  
  "database=zyhx";  
  string   select="select   *   from   admin   where   code='"   +   Code.Trim()   +   "'   and   passwd='"   +   Password.Trim()   +   "'";  
  try  
  {  
  SqlConnection   conn   =   new   SqlConnection(source);  
  conn.Open();  
  SqlCommand   cmd   =   new   SqlCommand(select,conn);  
  SqlDataReader   sql   =   cmd.ExecuteReader();  
  while   (sql.Read())  
  {  
  textBox2.Text   =   sql["user"].ToString   ();  
  IsIn=true;  
  }  
  }  
  catch(Exception   ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  
  //conn.Close();  
  return   IsIn;  
  }  
  [STAThread]  
   
  private   void   textBox1_Leave(object   sender,   System.EventArgs   e)  
   
  {  
  textBox2.Text   =   "aa";  
  }  
   
  static   void   Main()    
  {  
  login   newmain=new   login();  
  Application.Run(newmain);  
  }  
   
   
  }  
  }  
  Top

12 楼zwcscq()回复于 2006-11-11 11:01:55 得分 0

是不可能实现的...Top

13 楼ntmit(自由的呼吸)回复于 2006-11-11 11:08:57 得分 0

为什么不能实现Top

14 楼scow(怡红快绿之小橙子|和谐权是第4代人权)回复于 2006-11-11 11:19:46 得分 0

在InitializeComponent里面看不到事件注册,也许丢失了,手动注册一下吧Top

15 楼ntmit(自由的呼吸)回复于 2006-11-11 11:35:14 得分 0

怎么注册啊Top

16 楼ntmit(自由的呼吸)回复于 2006-11-11 11:54:10 得分 0

人呢Top

17 楼tipboy(想做技术边缘人)回复于 2006-11-11 12:10:58 得分 0

用验证控件  
  Top

18 楼ntmit(自由的呼吸)回复于 2006-11-11 12:13:04 得分 0

说详细点,我刚学!Top

19 楼ntmit(自由的呼吸)回复于 2006-11-11 14:57:06 得分 0

郁闷啊Top

20 楼starsong(PB和.NET齐头并进!)回复于 2006-11-11 16:39:45 得分 0

看看InitializeComponent这个里面是否有这个失去焦点的事件,没有就手工加上就ok了Top

21 楼mike_ylp()回复于 2006-11-11 17:19:22 得分 0

TextBox裡面有個Validated事件,在事件內查詢出你要顯示的數據,條件是TextBox內的值Top

22 楼kellynt()回复于 2006-11-11 17:43:36 得分 0

我想实现,当textbox1输入完毕后,textbox2自动从数据库中读取数据并显示。  
  ---------------------------------------------------------------------  
  这个用"焦点"去判断,在winform是很难实现的,且也不稳定,一般的做法是"当textbox1输入完毕后,按回车键执行查询",你的需求如果改成这样就容易实现多了Top

23 楼chuhx(xiaoyao)回复于 2006-11-11 17:57:45 得分 0

要触发textBox2啊  
  Top

24 楼sguar530()回复于 2006-11-11 18:49:22 得分 0

好复杂说的Top

25 楼eric2006()回复于 2006-11-11 23:05:55 得分 0

InitializeComponent这个里面,都没有看到事件注册.  
  在它里面加上this.textBox1.Click   +=   new   System.EventHandler(this.textBox1_Leave);  
  就行了  
   
  Top

26 楼partpass(咔咔)回复于 2006-11-11 23:25:01 得分 0

顶!Top

27 楼superxiaomm(小美)回复于 2006-11-12 09:31:09 得分 0

markTop

28 楼jcyluck(C# + SQL 2005 QQ群:26096739)回复于 2006-11-12 10:40:44 得分 0

试试下面的代码:  
   
  using   System;  
  using   System.Drawing;  
  using   System.Collections;  
  using   System.ComponentModel;  
  using   System.Windows.Forms;  
  using   System.Data;  
  using   System.Data.SqlClient;  
   
  namespace   member  
  {  
  ///   <summary>  
  ///   login   的摘要说明。  
  ///   </summary>  
  public   class   login   :   System.Windows.Forms.Form  
  {  
  private   System.Windows.Forms.TextBox   textBox1;  
  private   System.Windows.Forms.TextBox   textBox2;  
  private   System.Windows.Forms.TextBox   textBox3;  
  private   System.Windows.Forms.Button   button1;  
  private   System.Windows.Forms.Button   button2;  
  private   System.Windows.Forms.Label   label1;  
  private   System.Windows.Forms.Label   label2;  
  private   System.Windows.Forms.Label   label3;  
  ///   <summary>  
  ///   必需的设计器变量。  
  ///   </summary>  
  private   System.ComponentModel.Container   components   =   null;  
   
  public   login()  
  {  
  //  
  //   Windows   窗体设计器支持所必需的  
  //  
  InitializeComponent();  
   
  //  
  //   TODO:   在   InitializeComponent   调用后添加任何构造函数代码  
  //  
  }  
   
  ///   <summary>  
  ///   清理所有正在使用的资源。  
  ///   </summary>  
  protected   override   void   Dispose(   bool   disposing   )  
  {  
  if(   disposing   )  
  {  
  if(components   !=   null)  
  {  
  components.Dispose();  
  }  
  }  
  base.Dispose(   disposing   );  
  }  
   
  #region   Windows   Form   Designer   generated   code  
  ///   <summary>  
  ///   设计器支持所需的方法   -   不要使用代码编辑器修改  
  ///   此方法的内容。  
  ///   </summary>  
  private   void   InitializeComponent()  
  {  
  System.Resources.ResourceManager   resources   =   new   System.Resources.ResourceManager(typeof(login));  
  this.button1   =   new   System.Windows.Forms.Button();  
  this.button2   =   new   System.Windows.Forms.Button();  
  this.textBox1   =   new   System.Windows.Forms.TextBox();  
  this.textBox2   =   new   System.Windows.Forms.TextBox();  
  this.textBox3   =   new   System.Windows.Forms.TextBox();  
  this.label1   =   new   System.Windows.Forms.Label();  
  this.label2   =   new   System.Windows.Forms.Label();  
  this.label3   =   new   System.Windows.Forms.Label();  
  this.SuspendLayout();  
  //    
  //   button1  
  //    
  this.button1.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.button1.FlatStyle   =   System.Windows.Forms.FlatStyle.Flat;  
  this.button1.Font   =   new   System.Drawing.Font("宋体",   9F,   System.Drawing.FontStyle.Regular,   System.Drawing.GraphicsUnit.Point,   ((System.Byte)(134)));  
  this.button1.Location   =   new   System.Drawing.Point(280,   184);  
  this.button1.Name   =   "button1";  
  this.button1.Size   =   new   System.Drawing.Size(56,   24);  
  this.button1.TabIndex   =   3;  
  this.button1.Text   =   "确     定";  
  this.button1.Click   +=   new   System.EventHandler(this.button1_Click);  
  //    
  //   button2  
  //    
  this.button2.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.button2.FlatStyle   =   System.Windows.Forms.FlatStyle.Flat;  
  this.button2.Location   =   new   System.Drawing.Point(368,   184);  
  this.button2.Name   =   "button2";  
  this.button2.Size   =   new   System.Drawing.Size(56,   24);  
  this.button2.TabIndex   =   4;  
  this.button2.Text   =   "取     消";  
  this.button2.Click   +=   new   System.EventHandler(this.button2_Click);  
  //    
  //   textBox1  
  //    
  this.textBox1.BorderStyle   =   System.Windows.Forms.BorderStyle.FixedSingle;  
  this.textBox1.Location   =   new   System.Drawing.Point(272,   104);  
  this.textBox1.Name   =   "textBox1";  
  this.textBox1.Size   =   new   System.Drawing.Size(160,   21);  
  this.textBox1.TabIndex   =   0;  
  this.textBox1.Text   =   "";  
  this.textBox1.Leave   +=   new   System.EventHandler(this.textBox1_Leave);  
  //    
  //   textBox2  
  //    
  this.textBox2.BorderStyle   =   System.Windows.Forms.BorderStyle.FixedSingle;  
  this.textBox2.Enabled   =   false;  
  this.textBox2.Location   =   new   System.Drawing.Point(272,   128);  
  this.textBox2.Name   =   "textBox2";  
  this.textBox2.Size   =   new   System.Drawing.Size(160,   21);  
  this.textBox2.TabIndex   =   1;  
  this.textBox2.Text   =   "";  
  //    
  //   textBox3  
  //    
  this.textBox3.BorderStyle   =   System.Windows.Forms.BorderStyle.FixedSingle;  
  this.textBox3.Location   =   new   System.Drawing.Point(272,   152);  
  this.textBox3.Name   =   "textBox3";  
  this.textBox3.PasswordChar   =   '*';  
  this.textBox3.Size   =   new   System.Drawing.Size(160,   21);  
  this.textBox3.TabIndex   =   2;  
  this.textBox3.Text   =   "";  
  //    
  //   label1  
  //    
  this.label1.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.label1.Location   =   new   System.Drawing.Point(208,   109);  
  this.label1.Name   =   "label1";  
  this.label1.Size   =   new   System.Drawing.Size(72,   16);  
  this.label1.TabIndex   =   5;  
  this.label1.Text   =   "用户代码:";  
  //    
  //   label2  
  //    
  this.label2.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.label2.Location   =   new   System.Drawing.Point(208,   133);  
  this.label2.Name   =   "label2";  
  this.label2.Size   =   new   System.Drawing.Size(72,   16);  
  this.label2.TabIndex   =   6;  
  this.label2.Text   =   "用户姓名:";  
  //    
  //   label3  
  //    
  this.label3.BackColor   =   System.Drawing.SystemColors.ActiveCaptionText;  
  this.label3.Location   =   new   System.Drawing.Point(208,   157);  
  this.label3.Name   =   "label3";  
  this.label3.Size   =   new   System.Drawing.Size(72,   16);  
  this.label3.TabIndex   =   7;  
  this.label3.Text   =   "用户密码:";  
  //    
  //   login  
  //    
  this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);  
  this.BackgroundImage   =   ((System.Drawing.Bitmap)(resources.GetObject("$this.BackgroundImage")));  
  this.ClientSize   =   new   System.Drawing.Size(448,   229);  
  this.Controls.AddRange(new   System.Windows.Forms.Control[]   {  
      this.textBox3,  
      this.textBox2,  
      this.textBox1,  
      this.button2,  
      this.button1,  
      this.label1,  
      this.label2,  
      this.label3});  
  this.MaximizeBox   =   false;  
  this.MinimizeBox   =   false;  
  this.Name   =   "login";  
  this.Text   =   "会员登录";  
  this.ResumeLayout(false);  
   
  }  
  #endregion  
   
  private   void   button1_Click(object   sender,   System.EventArgs   e)  
  {  
   
  if   (this.CheckUser(this.textBox1.Text.Trim(),this.textBox2.Text.Trim(),this.textBox3.Text.Trim())==true)  
  {  
  winmain   winmain   =   new   winmain();  
  classa.Code   =textBox1.Text;  
  classa.User   =textBox2.Text;  
  winmain.Show();  
  this.Hide();  
  }  
  else  
  {  
  MessageBox.Show("您输入的帐号或密码有误,请重新登录!","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);  
  }  
   
  }  
   
  private   void   button2_Click(object   sender,   System.EventArgs   e)  
  {  
  Application.Exit   ();  
  }  
   
   
  private   void   textBox1_Enter(object   sender,   System.EventArgs   e)  
  {  
  Application.Exit   ();  
  }  
   
   
  private   bool   CheckUser(string   Code,string   UserName,string   Password)  
  {  
  bool   IsIn=false;  
  string   source="server=localhost;"+  
  "uid=sa;pwd=ntdba;"+  
  "database=zyhx";  
  string   select="select   *   from   admin   where   code='"   +   Code.Trim()   +   "'   and   passwd='"   +   Password.Trim()   +   "'";  
  try  
  {  
  SqlConnection   conn   =   new   SqlConnection(source);  
  conn.Open();  
  SqlCommand   cmd   =   new   SqlCommand(select,conn);  
  SqlDataReader   sql   =   cmd.ExecuteReader();  
  while   (sql.Read())  
  {  
  textBox2.Text   =   sql["user"].ToString   ();  
  IsIn=true;  
  }  
  }  
  catch(Exception   ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  
  //conn.Close();  
  return   IsIn;  
  }  
  [STAThread]  
   
  private   void   textBox1_Leave(object   sender,   System.EventArgs   e)  
   
  {  
  textBox2.Text   =   "aa";  
  }  
   
  static   void   Main()    
  {  
  login   newmain=new   login();  
  Application.Run(newmain);  
  }  
   
   
  }  
  }  
  Top

29 楼z2xlong(缦缨客)回复于 2006-11-13 09:36:30 得分 0

同意tipboy(想做技术边缘人)   的办法,使用自定义验证控件,在验证函数内实现回调即可Top

30 楼minwent()回复于 2006-11-13 14:33:48 得分 0

看msdn关于textbox控件的事件说明。Top

相关问题

关键词

得分解答快速导航

  • 帖主:ntmit
  • lizhizhe2000

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo