winform使用相关

1.回车键触发按钮点击事件——回车登录

设置窗体的AccessButton属性

2.密码框样式设置

设置PasswordChar为想要的密码显示的样式,如*

 3.设置窗口居中

设置StartPositon为想要的显示样式,如CenterScreen,屏幕居中

  4.获取当前程序运行的目录

System.IO.Directory.GetCurrentDirectory()

5.textbox只允许输入数字,同时验证格式,小数点后两位

设置textbox的keypress时间
private void money_KeyPress(object sender, KeyPressEventArgs e)
{
//48代表0,57代表9,8代表空格,46代表小数点
if ((e.KeyChar < || e.KeyChar > ) && (e.KeyChar != ) && (e.KeyChar != ))
{
e.Handled = true;
}
else
{
//判断输入的是否是删除键
if(e.KeyChar==)
{
e.Handled = false;
return;
}
//检查允许输入的值
//判断已经输入了小数点,同时当前输入的是点
if (money.Text.IndexOf((char)) != - && e.KeyChar==)
{
e.Handled = true;
return;
}
//已经输入了小数点,当前输入的是小数点后两位
//获取.的索引
if (money.Text.IndexOf((char)) != - )
{
try
{
int index = money.Text.IndexOf('.');
string str = money.Text.Substring(index);
//输入位置在小数点之后
if(money.SelectionStart>index)
{
if (str.Length > )
{
e.Handled = true;
return;
}
}
}
catch (Exception)
{
//
} }
e.Handled = false;
return;
}
}

 6.窗口口最小化背景图片消失

简单粗暴的办法
使用picturebox,然后设置dock属性以及SizeMode设置为StretchImage

 7.启动界面闪烁问题

直接设置背景图片的方式会由于窗口绘制的原因产生闪烁,
可通过picturebox设置fill填充的方式来投机取巧

 8.控件跟随窗口同时放大

设置anchor属性来控制绑定的边缘
设置dock属性来控制停靠

 9.选择多个文件

设置MultiSelect属性
代码中使用productFile.FileNames;获取所有的文件名

10.使用系统托盘最小化

使用NotifyIcon控件,需要注意必须设置最小化的图标
然后根据一定条件将窗体hide
通过设置NotifyIcon的点击事件重新show窗体即可

 11.不显示窗口标题

设置ControlBox为False,然后删除窗口的TEXT属性
设置FormBorderStyle为NONE

 12.dataGridView判断checkBox是否选中

(bool)dataGridView1.Rows[e.RowIndex].Cells[].EditedFormattedValue;

 13.获取listbox点击位置所在的行索引

int index = this.listBox1.IndexFromPoint(e.Location);

14.保存对话框选择保存目录

使用FolderBrowserDialog控件

 15.设置回车激活某个按钮

设置窗体属性的AcceptButton属性为需要的按钮即可

 16.获取当前工作区宽度和高度(工作区不包含状态栏

int ScreenWidth = Screen.PrimaryScreen.WorkingArea.Width;
int ScreenHeight = Screen.PrimaryScreen.WorkingArea.Height;

 17,datagridview表头背景色

dataGridView1.EnableHeadersVisualStyles = false;
for (int i = ; i < dataGridView1.Columns.Count; i++)
{
dataGridView1.Columns[i].HeaderCell.Style.BackColor =Color.DodgerBlue;
}

 18.去掉datagridview最左侧列

RowHeadVisible属性设置为false

 19.datagridview排序

// 按自编号排序
DataGridViewColumn col = dataGridView1.Columns[];
// 按降序(即始终每次新添加的数据排最前)
ListSortDirection direction = ListSortDirection.Descending;
dataGridView1.Sort(col, direction);
上一篇:关于Android文件Apk下载的那点事


下一篇:CocoaPods版本升级