最近在学习C#Winform程序设计与开发,本文主要记录一些所遇到的问题,
特此声明:以下问题并非本人所解决,皆是网上大佬解决,小弟四处查找所获
,但文章来源已经难以找到,如有哪位大佬的思路在下面出现,请告知小弟,小弟备注一下。
在设计过程中遇到一些问题,通过查找资料总结如下:
问题一:初始化时最大化窗体
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
}
else
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
this.WindowState = FormWindowState.Maximized;
}
问题二:使用panel控件实现页面切换
panel2.Controls.Clear(); //移除所有控件
form.Dock = DockStyle.Fill; //设置样式是否填充整个panel
panel2.Controls.Add(form); //添加窗体
form.Show(); //窗体运行
问题三:窗体单例模式
public static ManageForm singleManage=null;
private void tbtmanage_Click(object sender, EventArgs e)
{
if (singleManage == null)
{
singleManage = new ManageForm();
}
else
{
return singleManage;
}
}
问题四:在控件具有焦点并且按下某个键并释放后某个控件获得焦点
示例如下:
private void txtpwd_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
btLogin.Focus();
}
}
问题四:禁止通过拖动,双击标题栏改变窗体大小。
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x00A1 && m.WParam.ToInt32() == 2)
return;
if (m.Msg == 0xA3)
return;
base.WndProc(ref m);
}
问题五:返回上一层查找文件
pictureBox1.Image = Image.FromFile(@"…\Image\Penguins.jpg");
备注:一个…\代表返回上一层,两个则代表返回上上一层
问题六:行接收焦点
///
/// 行接受输入焦点成为当前选中行时的事件
///
///
///
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
//获取当前选中的行对象
DataGridViewRow currentRow = this.dgv1.Rows[e.RowIndex];
//获取当前行中绑定的TblClass数据对象
Person model = currentRow.DataBoundItem as Person;
if (model != null)
{
txtname.Text = model.Name;
txtgender.Text = model.Gender;
txtage.Text = model.Age.ToString();
txtphone.Text = model.PhoneNum;
}
}
学习笔记第一篇。
后续会继续更新~~~