【重构】机房收费系统的优化

1、当用户名和密码没有输入时,进行提醒

  Boolean textboxHasText = false;//判断输入框是否有文本
//当焦点在textbox控件中时
private void txtUserID_Enter(object sender, EventArgs e)
{
       if (textboxHasText==false)
       {
            txtUserID.Text = "";

       }
       txtUserID.ForeColor = Color.Black;      //输入文本的颜色
}
//当焦点不在textbox控件中时

private void txtUserID_Leave(object sender, EventArgs e)
{
        if (txtUserID.Text == "")
        {
             txtUserID.Text = "请输入用户名";     //提示文本
             txtUserID.ForeColor = Color.LightGray;   //提示文本的颜色
             textboxHasText = false;
       }
       else
             textboxHasText = true;
}

2、设置窗体大小不能变化
设置窗体的FormBorderStyle属性为FixedDialog
3、添加背景图片
设置窗体的BackgroundImage属性
4、代码统一修改一个窗体所有控件的字体
使用循环语句

foreach (Control control in this.Controls)
{
        if (control is Label)
        {
            control.Font = new Font("楷体", 18,FontStyle.Bold);
        }

}

5、实现多次点击子窗体,只出现一个,并且子窗体不能离开父窗体
使用单例模式
子窗体

using System.Runtime.InteropServices;
[DllImport("user32")]
public static extern int SetParent(int Child, int Parent);   //子窗体只在父窗体中,不能移出
//使用单例模式
private static frmModifyPassword fmpwd;
private static readonly object syncRoot = new object();
public static frmModifyPassword GetInstance()
{
      if (fmpwd == null || fmpwd.IsDisposed)
     {
           lock (syncRoot)
           {
                if (fmpwd == null || fmpwd.IsDisposed)
                {
                    fmpwd = new frmModifyPassword();
                }
           }
      }
      SetParent((int)fmpwd.Handle, (int)ActiveForm.Handle);
      return fmpwd;
}

父窗体

//调用frmModifyPassword的GetInstance()方法,即使用单例模式
frmModifyPassword frmmodifypassword = frmModifyPassword.GetInstance(); 
if (frmmodifypassword.Visible == false)
{
        frmmodifypassword.Show(this);   //可以保证子窗体一直显示在父窗体之上
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 24
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值