WinForm控件之【TextBox】


基本介紹

文本控件,提供多行文本編輯和密碼字符掩碼功能。

常設置屬性

ForeColor:此組件的前景色,用於顯示文本;

BorderStyle:指示編輯控件是否應帶有邊框或邊框類型;

Lines:多行編輯中的文本行,作為字符串值的數組;

MaxLength:指定可以在編輯控件中輸入的最大字符數;

PasswordChar:指示將為單行編輯控件的密碼輸入顯示的字符;

Multiline:控制編輯控件的文本是否能夠跨越多行;

ScrollBars:定義控件滾動條的行為;

WordWrap:指示多行編輯控件是否自動換行;

Enabled:指示是否啟用該控件,true為啟用狀態用戶可編輯,false為禁用狀態用戶不可編輯;

Name:指示代碼中用來標識該對象的名稱;

Text:獲取或設置多格式文本框中的文本;

 

事例舉例

 

相關代碼

 

        //控件提示信息變量
        string strUser = "長度不低於四個字符", strPwd = "長度不低於六個字符,由字母和數字組成";

        #region 用戶密碼控件設置提示信息相關事件
        private void txt_user_Enter(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (tb.Text.Equals(strUser))
            {
                tb.Text = string.Empty;
                tb.ForeColor = System.Drawing.SystemColors.WindowText;
            }
        }

        private void txt_user_Leave(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (string.IsNullOrWhiteSpace(tb.Text))
            {
                tb.Text = strUser;
                tb.ForeColor = System.Drawing.SystemColors.ScrollBar;
            }
        }

        private void txt_pwd_Enter(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (tb.Text.Equals(strPwd))
            {
                tb.Text = string.Empty;
                tb.ForeColor = System.Drawing.SystemColors.WindowText;
                tb.PasswordChar = '*';
            }
        }

        private void txt_pwd_Leave(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (string.IsNullOrWhiteSpace(tb.Text))
            {
                tb.Text = strPwd;
                tb.ForeColor = System.Drawing.SystemColors.ScrollBar;
                tb.PasswordChar = '\0';
            }
        }
        #endregion

        //登錄
        private void btn_login_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txt_user.Text) || txt_user.Text.Equals(strUser))
            {
                MessageBox.Show("用戶名不能為空!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                if (txt_user.Text.Length < 4)
                {
                    MessageBox.Show("用戶名長度不能低於四個字符!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            if (string.IsNullOrWhiteSpace(txt_pwd.Text) || txt_pwd.Text.Equals(strPwd))
            {
                MessageBox.Show("密碼不能為空!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                if (txt_pwd.Text.Length < 6)
                {
                    MessageBox.Show("用戶名長度不能低於六個字符!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                string strRegex = "[0-9]";
                if (!System.Text.RegularExpressions.Regex.IsMatch(txt_pwd.Text, strRegex))
                {
                    MessageBox.Show("密碼必須存在數字,請確認!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                strRegex = "[a-zA-Z]";
                if (!System.Text.RegularExpressions.Regex.IsMatch(txt_pwd.Text, strRegex))
                {
                    MessageBox.Show("密碼必須存在字母,請確認!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            MessageBox.Show("登錄成功!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //退出
        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM