1.窗体属性窗体居中
2.窗体属性窗体居中
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
3.窗体去掉最小化、最大化
MinimizeBox = false;
MaximizeBox = false;
4.设置窗体不可拉伸
设置窗体的FormBorderStyle属性,将默认的”Sizable”改为” FixedSingle”或” Fixed3D”即可。
5.C#实现Form窗口最大化(最小化)
this.WindowState = FormWindowState.Maximized;
this.WindowState = FormWindowState.Normal;
this.WindowState = FormWindowState.Minimized;
6.Panel控件增加滚动条
AutoScroll=True AutoScrollMiniSize 450,450 //设置逻辑区域尺寸,如果它大于控件尺寸就会出现滚动条。
7.WINFORM 中改变窗体大小
this.Size = new System.Drawing.Size(100, 100);
8.WINFORM 中动态添加按钮
private void button1_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Width = 30;
btn.Height = 30;
btn.Text = "123";
panel1.Controls.Add(btn);
}
9.c#鼠标在控件上面,然后显示文字
先添加toolTip控件到界面
然后每个控件的属性会多一项 ToolTip
第一种:直接给里面加文字
第二种:
private void pictureBox_topmost_MouseHover(object sender, EventArgs e)
{
if (this.TopMost == true)
{
toolTip_jmt.SetToolTip(pictureBox_topmost, "取消置顶");
}
else
{
toolTip_jmt.SetToolTip(pictureBox_topmost, "置顶");
}
}
10.winform TextBox设置透明
1,方法1:设置背景色与父容器的背景色一致; 2,方法2:自定义控件;
10.C# 鼠标点击移动窗体代码,可以实现无边框窗体的拖动
private static bool IsDrag = false;
private int enterX;
private int enterY;
private void setForm_MouseDown(object sender, MouseEventArgs e)
{
IsDrag = true;
enterX = e.Location.X;
enterY = e.Location.Y;
}
private void setForm_MouseUp(object sender, MouseEventArgs e)
{
IsDrag = false;
enterX = 0;
enterY = 0;
}
private void setForm_MouseMove(object sender, MouseEventArgs e)
{
if (IsDrag)
{
Left += e.Location.X - enterX;
Top += e.Location.Y - enterY;
}
}
11.Windform C# pictureBox 更换背景图片
private void pbRun_Click(object sender, EventArgs e)
{
string pbName = pbRun.Tag.ToString();
if (pbRun.Tag.ToString() == "0")
{
string localFilePath = @"../../Source/启动.png";
Image Img = Image.FromFile(localFilePath);
Image bmp = new Bitmap(Img);
Img.Dispose();
pbRun.Image = bmp;
pbRun.SizeMode = PictureBoxSizeMode.StretchImage;
pbRun.Tag = "1";
}
else
{
string localFilePath = @"../../Source/suspend.png";
Image Img = Image.FromFile(localFilePath);
Image bmp = new Bitmap(Img);
Img.Dispose();
pbRun.Image = bmp;
pbRun.SizeMode = PictureBoxSizeMode.StretchImage;
pbRun.Tag = "0";
}
}
12.c# datagridview列宽自适应设置
13.winform中改变DataGridview的列标题字体颜色
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.ForeColor = Color.Red;
foreach (DataGridViewColumn col in this.dataGridView1.Columns)
{
col.HeaderCell.Style = style;
}
this.dataGridView1.EnableHeadersVisualStyles = false;
this.dataGridView1.RowHeadersVisible = false;
14.C#窗体中的DataGridView取消选中条
private void DataGridViewDetail_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
e.Row.Selected = false;
}
15.C# WINFORM 鼠标右键弹出菜单
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(e.X + this.Left, e.Y + this.Top);
}
}
|