熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C# TextBox事件實現實例詳解

2013-11-13 10:20:27  來源: .NET編程 

  C# TextBox事件是我們在開發中會碰到的具體的功能需求那麼如何使得想要的C# TextBox事件執行呢?那麼這裡就向你介紹具體的C# TextBox事件演示實例包括需求的實現希望對你有所幫助

  C# TextBox事件具體的需求

  ◆界面要求定義個TEXTBOX分別是姓名地址職業年齡輸出內容個BUTTON

  ◆滿足條件

  ()用戶名不能為空

  ()年齡必須是一個大於或等於的數字

  ()職業必須是程序員或為空

  ()地址不能為空

  C# TextBox事件實例實現

  using System;

  using SystemCollectionsGeneric;  using SystemComponentModel;

  using SystemData;

  using SystemDrawing;

  using SystemText;

  using SystemWindowsForms;

  namespace Chapter 

  {

  public partial class Form : Form 

  {

  public Form() 

  {

  InitializeComponent();

  thisbuttonEnabled = false;

  thistextBoxTag = false;

  thistextBoxTag = false;

  thistextBoxTag = false;

  thistextBoxTag = false;

  thistextBoxValidating+=   new SystemComponentModelCancelEventHandler(  thistextboxEmpty_Validating);

  thistextBoxValidating +=   new SystemComponentModelCancelEventHandler(  thistextboxEmpty_Validating);

  thistextBoxValidating+=   new SystemComponentModelCancelEventHandler(  thistextboxOccupation_Validating);

  thistextBoxValidating +=   new SystemComponentModelCancelEventHandler(  thistextboxEmpty_Validating);

  thistextBoxTextChanged +=   new SystemEventHandler(thistextbox_TextChanged);

  thistextBoxTextChanged +=   new SystemEventHandler(thistextbox_TextChanged);

  thistextBoxTextChanged +=   new SystemEventHandler(thistextbox_TextChanged);

  }

  //C# TextBox事件  private void textboxOccupation_Validating(  object sender   SystemComponentModelCancelEventArgs e)

  

  {

  TextBox tb=(TextBox)sender;

  if (tbTextCompareTo(Programmer) == ||tbTextLength==

  {

  tbTag = true;

  tbBackColor = SystemDrawingSystemColorsWindow;

  }

  else

  {

  tbTag = false;

  tbBackColor = ColorRed;

  }

  ValidateOk();

  }

  //C# TextBox事件  private void textboxEmpty_Validating(  object sender   SystemComponentModelCancelEventArgs e) 

  {

  TextBox tb = (TextBox)sender;

  if (tbTextLength ==

  {

  tbBackColor = ColorRed;

  tbTag = false;

  }

  else

  {

  tbBackColor = SystemDrawingSystemColorsWindow;

  tbTag = true;

  }

  ValidateOk();

  }

  private void textboxAge_KeyPress(  object sender KeyPressEventArgs e) 

  {

  if ((eKeyChar < || eKeyChar > ) &&   eKeyChar != )

  eHandled = true;

  }

  private void textbox_TextChanged(  object sender SystemEventArgs e) 

  {

  TextBox tb = (TextBox)sender;

  if (tbTextLength == && tb!=textBox

  {

  tbTag = false;  tbBackColor = ColorRed;

  }

  else if (tb == thistextBox &&   (tbTextLength != &&   tbTextCompareTo(Programmer) != )) 

  {

  tbTag = false;

  }

  //C# TextBox事件  else

  {

  tbTag = true;

  tbBackColor = SystemColorsWindow;

  }

  ValidateOk();

  }

  private void ValidateOk() 

  {

  thisbuttonEnabled =   ((bool)(thistextBoxTag) &&   (bool)(thistextBoxTag) &&   (bool)(thistextBoxTag) &&   (bool)(thistextBoxTag));

  }

  private void button_Click(  object sender EventArgs e) 

  {

  string output;

  //C# TextBox事件

  output = Name: + thistextBoxText + ;

  output += Address: + thistextBoxText + ;

  output += Occupation: + thistextBoxText + ;

  output += Age: + thistextBoxText + ;

  thistextBoxText = output;

  }

  }

  }

  C# TextBox事件實現的具體實例就向你介紹到這裡希望對你了解和學習C# TextBox事件有所幫助


From:http://tw.wingwit.com/Article/program/net/201311/13291.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.