驗証TextBox輸入的文本

代碼如下:

public   partial   class  Textboxtext : Form
    
{
        
public Textboxtext()
        
{
            InitializeComponent();
            btnClose.CausesValidation 
= false;//當單擊按鈕時不考慮驗証狀態
            txtUsename.Tag = false;
            txtAddress.Tag 
= false;
            txtAge.Tag 
= false;
            txtOccupation.Tag 
= false;
            btnOK.Enabled 
= false;
            txtUsename.Validating 
+= new CancelEventHandler(txtBoxEmpty_Validating);
            txtAddress.Validating 
+= new CancelEventHandler(txtBoxEmpty_Validating);
            txtOccupation.Validating 
+= new CancelEventHandler(txtOccupation_Validating);
            txtAge.KeyPress 
+= new KeyPressEventHandler(txtAge_KeyPress);
            txtAge.Validating 
+= new CancelEventHandler(txtBoxEmpty_Validating);
            txtUsename.TextChanged 
+= new EventHandler(txt_TextChanged);
            txtAddress.TextChanged 
+= new EventHandler(txt_TextChanged);
            txtOccupation.TextChanged 
+= new EventHandler(txt_TextChanged);
            txtAge.TextChanged 
+= new EventHandler(txt_TextChanged);
            Combox.KeyDown 
+= new KeyEventHandler(Combox_KeyDown);

            LoadCombox();
        }


        
void Combox_KeyDown(object sender, KeyEventArgs e)
        
{
            
int index = 0;
            ComboBox cbo 
= (ComboBox)sender;
            
if (e.KeyCode == Keys.Enter)
            
{
                index 
= cbo.FindStringExact(cbo.Text);
                
if (index < 0)
                    cbo.Items.Add(cbo.Text);
                
//else cbo.SelectedIndex = index;
            }

            
if (e.KeyCode == Keys.Delete)
            
{
                index 
= cbo.FindStringExact(cbo.Text);
                
if (index > -1)
                    cbo.Items.Remove(cbo.Text);
            }

        }

        
void LoadCombox()
        
{
            
string FilePath = Application.StartupPath + @" xt.txt";
            
bool exist = File.Exists(FilePath);
            
if (exist == false)
            
{
                Directory.CreateDirectory(Application.StartupPath);
                FileStream fs 
= new FileStream(FilePath, FileMode.Create);
                fs.Close();
            }

            System.IO.StreamReader sr 
= new System.IO.StreamReader(FilePath);
            
try
            
{

                
string input;
                
do
                
{
                    input 
= sr.ReadLine();
                    
if (input != ""&&input!=null)
                        Combox.Items.Add(input);
                }

                
while (sr.Peek() != -1);
                sr.Close();
            }

            
catch (Exception)
            
{
                MessageBox.Show(
"沒有找到相關文件");
            }

            
//finally
            
//{ }
        }

        
void SaveCombox()
        
{
            
try
            
{
                System.IO.StreamWriter sw 
= new System.IO.StreamWriter(Application.StartupPath + @" xt.txt");
                
foreach (string item in Combox.Items)
                    sw.WriteLine(item);
                sw.Flush();
                sw.Close();
            }

            
catch (Exception err)
            
{
                MessageBox.Show(err.Message);
            }

        }


        
void txt_TextChanged(object sender, EventArgs e)
        

            TextBox tb 
= (TextBox)sender;
            
if (tb.Text.Length == 0 && tb != txtOccupation)
            
{
                tb.BackColor 
= Color.Purple;
                tb.Tag 
= false;
            }

            
else if (tb == txtOccupation && (txtOccupation.Text.Length != 0 && tb.Text.CompareTo("程序員"!= 0))
            
{
                tb.BackColor 
= Color.Purple;
                tb.Tag 
= false;
            }

            
else
            
{
                tb.BackColor 
= SystemColors.Window;
                tb.Tag 
= true;
            }

            ValidateAll();
        }


        
void txtOccupation_Validating(object sender, CancelEventArgs e)
        
{
            TextBox tb 
= (TextBox)sender;
            
if (tb.Text.CompareTo("程序員"== 0 || tb.Text.Length == 0)
            
{
                tb.BackColor 
= SystemColors.Window;
                tb.Tag 
= true;
            }

            
else
            
{
                tb.BackColor 
= Color.Purple;
                tb.Tag 
= false;
            }

            ValidateAll();
        }


        
void txtAge_KeyPress(object sender, KeyPressEventArgs e)
        
{
            
if ((e.KeyChar < 48 || e.KeyChar > 57)&&e.KeyChar!=8)//ASC值8表示退位鍵
                e.Handled = true;
        }


        
void txtBoxEmpty_Validating(object sender, CancelEventArgs e)
        
{
            TextBox tb 
= (TextBox)sender;
            
if (tb.Text.Length == 0)
            
{
                tb.BackColor 
= Color.Purple;
                tb.Tag 
= false;
                
//e.Cancel = true;
            }

            
else
            
{
                tb.BackColor 
= SystemColors.Window;
                tb.Tag 
= true;
            }

            ValidateAll();
        }

        
private void btnOK_Click(object sender, EventArgs e)
        
{
            
string outputtxt;
            outputtxt 
= "Usename:" + txtUsename.Text + Environment.NewLine;
            outputtxt 
+= "Address:" + txtAddress.Text + " ";
            outputtxt 
+= "Occupation:" + txtOccupation.Text + " ";
            outputtxt 
+= "Age:" + txtAge.Text + " ";
            outputtxt 
+= "Sex:" +(string)(RdoMale.Checked ? "Male" : "Female"+ " ";
            outputtxt 
+= "星座:" + Combox.Text + " ";
            txtOutput.Text 
+= outputtxt;
        }


        
private void btnHelp_Click(object sender, EventArgs e)
        
{
            
string outputtxt;
            outputtxt 
= "Usename: 用戶名(不能為空)" + Environment.NewLine;
            outputtxt 
+= "Address: 地址(不能空空) ";
            outputtxt 
+= "Occupation: 職業(必須是程序員或為空) ";
            outputtxt 
+= "Age: 年齡(大於或等於零的一個數字) ";
            outputtxt 
+= "Sex: 性別 ";
            outputtxt 
+= "星座: 所屬星座 ";
            txtOutput.Text 
+= outputtxt;
        }

        
void ValidateAll()
        
{
            btnOK.Enabled 
= (bool)txtUsename.Tag && (bool)txtAddress.Tag && (bool)txtAge.Tag && (bool)txtOccupation.Tag;
        }


        
private void btnClose_Click(object sender, EventArgs e)
        
{
            Application.Exit();
        }

    }

 運行效果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值