WinForm控件之【TextBox】

桃扇骨 2021-11-02 06:42 639阅读 0赞

基本介绍

文本控件,提供多行文本编辑和密码字符掩码功能。

常设置属性

ForeColor:此组件的前景色,用于显示文本;

BorderStyle:指示编辑控件是否应带有边框或边框类型;

Lines:多行编辑中的文本行,作为字符串值的数组;

MaxLength:指定可以在编辑控件中输入的最大字符数;

PasswordChar:指示将为单行编辑控件的密码输入显示的字符;

Multiline:控制编辑控件的文本是否能够跨越多行;

ScrollBars:定义控件滚动条的行为;

WordWrap:指示多行编辑控件是否自动换行;

Enabled:指示是否启用该控件,true为启用状态用户可编辑,false为禁用状态用户不可编辑;

Name:指示代码中用来标识该对象的名称;

Text:获取或设置多格式文本框中的文本;

事例举例

660447-20190719184007071-806360765.png

660447-20190719185754138-26821800.png660447-20190719185527297-1706526667.png

660447-20190719185554787-1851072268.png660447-20190719185629238-253739049.png

相关代码

  1. //控件提示信息变量
  2. string strUser = "长度不低于四个字符", strPwd = "长度不低于六个字符,由字母和数字组成";
  3. #region 用户密码控件设置提示信息相关事件
  4. private void txt_user_Enter(object sender, EventArgs e)
  5. {
  6. TextBox tb = (TextBox)sender;
  7. if (tb.Text.Equals(strUser))
  8. {
  9. tb.Text = string.Empty;
  10. tb.ForeColor = System.Drawing.SystemColors.WindowText;
  11. }
  12. }
  13. private void txt_user_Leave(object sender, EventArgs e)
  14. {
  15. TextBox tb = (TextBox)sender;
  16. if (string.IsNullOrWhiteSpace(tb.Text))
  17. {
  18. tb.Text = strUser;
  19. tb.ForeColor = System.Drawing.SystemColors.ScrollBar;
  20. }
  21. }
  22. private void txt_pwd_Enter(object sender, EventArgs e)
  23. {
  24. TextBox tb = (TextBox)sender;
  25. if (tb.Text.Equals(strPwd))
  26. {
  27. tb.Text = string.Empty;
  28. tb.ForeColor = System.Drawing.SystemColors.WindowText;
  29. tb.PasswordChar = '*';
  30. }
  31. }
  32. private void txt_pwd_Leave(object sender, EventArgs e)
  33. {
  34. TextBox tb = (TextBox)sender;
  35. if (string.IsNullOrWhiteSpace(tb.Text))
  36. {
  37. tb.Text = strPwd;
  38. tb.ForeColor = System.Drawing.SystemColors.ScrollBar;
  39. tb.PasswordChar = '\0';
  40. }
  41. }
  42. #endregion
  43. //登录
  44. private void btn_login_Click(object sender, EventArgs e)
  45. {
  46. if (string.IsNullOrWhiteSpace(txt_user.Text) || txt_user.Text.Equals(strUser))
  47. {
  48. MessageBox.Show("用户名不能为空!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  49. return;
  50. }
  51. else
  52. {
  53. if (txt_user.Text.Length < 4)
  54. {
  55. MessageBox.Show("用户名长度不能低于四个字符!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  56. return;
  57. }
  58. }
  59. if (string.IsNullOrWhiteSpace(txt_pwd.Text) || txt_pwd.Text.Equals(strPwd))
  60. {
  61. MessageBox.Show("密码不能为空!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  62. return;
  63. }
  64. else
  65. {
  66. if (txt_pwd.Text.Length < 6)
  67. {
  68. MessageBox.Show("用户名长度不能低于六个字符!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  69. return;
  70. }
  71. string strRegex = "[0-9]";
  72. if (!System.Text.RegularExpressions.Regex.IsMatch(txt_pwd.Text, strRegex))
  73. {
  74. MessageBox.Show("密码必须存在数字,请确认!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  75. return;
  76. }
  77. strRegex = "[a-zA-Z]";
  78. if (!System.Text.RegularExpressions.Regex.IsMatch(txt_pwd.Text, strRegex))
  79. {
  80. MessageBox.Show("密码必须存在字母,请确认!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  81. return;
  82. }
  83. }
  84. MessageBox.Show("登录成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  85. }
  86. //退出
  87. private void btn_exit_Click(object sender, EventArgs e)
  88. {
  89. this.Close();
  90. }

转载于:https://www.cnblogs.com/ljhandsomeblog/p/11215249.html

发表评论

表情:
评论列表 (有 0 条评论,639人围观)

还没有评论,来说两句吧...

相关阅读

    相关 WinFormTextBox

    基本介绍 文本控件,提供多行文本编辑和密码字符掩码功能。 常设置属性 ForeColor:此组件的前景色,用于显示文本; BorderStyle:指示编辑控件是