C#winform常用控件方法及出错BUG(持续更新)

Panel
1.自定义标题栏最小化最大化关闭

private void ZXH_Click(object sender, EventArgs e)
{
	this.WindowState = FormWindowState.Minimized;
}//最小化

private void BT_ZDH_Click(object sender, EventArgs e)
{
	if (this.WindowState == FormWindowState.Maximized)
		this.WindowState = FormWindowState.Normal;
	else
		this.WindowState = FormWindowState.Maximized;
}//最大化
        
private void GB_Click(object sender, EventArgs e)
{
	this.Close();
}//关闭    

2.自定义标题栏模拟窗体拖动

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern bool ReleaseCapture();
//上方添加至cs任意地方,下方为panel设置mousedown事件
private void panel_MouseDown(object sender, MouseEventArgs e)
{
	const int WM_NCLBUTTONDOWN = 0x00A1;
	const int HTCAPTION = 2;
	if (e.Button == MouseButtons.Left) 
	{
		ReleaseCapture();
		SendMessage(this.Handle, WM_NCLBUTTONDOWN, (IntPtr)HTCAPTION, IntPtr.Zero); 
	}
}

DataGridview
1.移动表格时表格及内容闪烁

protected override CreateParams CreateParams
{
	get
    {
    	CreateParams cp = base.CreateParams;
     	cp.ExStyle |= 0x02000000; //其中该字段与自定义最小化产生BUG
     	//cp.Style |= 0x20000;可用该字段代替
		return cp;
    }
}//放置代码任意位置即可

2.使用Task线程时导致卡顿或者滚动条黑色无法使用

data.Invoke(new Action(() =>
{
	data.Rows.Add();//添加数据
	data.Rows[0].DefaultCellStyle.BackColor = Color.Beige;//指定颜色
}));//添加数据时使用Invoke方法将更新UI的操作委托给UI线程执行

3.在子窗体中用GroupBox包裹DataGridview时右侧黑色

将GroupBox换为TabControl

CheckBoxComboBox
1.根据指定条件批量选中

for (int i = 0; i < comBoBox.Items.Count; i++)
{
	if (comBoBox.Items[i].ToString() == "条件")
		comBoBox.CheckBoxItems[i].Checked = true;
	else
		comBoBox.CheckBoxItems[i].Checked = false;
}

2.批量反选

for (int i = 0; i < comBoBox.Items.Count; i++)
{
	if (comBoBox.CheckBoxItems[i].Checked)
		comBoBox.CheckBoxItems[i].Checked = false;
	else
		comBoBox.CheckBoxItems[i].Checked = true;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值