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); //可以保证子窗体一直显示在父窗体之上
}