Winform主窗体设计

有些话、适合烂在心里 提交于 2020-04-02 09:38:48

主窗体顶部为菜单按钮,子窗体内嵌入Panel显示

界面如下:

第二步,主窗体离不开的几个方法

    1,点击菜单功能,加载子窗体

      private void btnOpenForm_Click(object sender, EventArgs e)
        {
            try
            {
                string frmName = ((ButtonX)sender).Tag.ToString();
                CloseForm();
                Form objForm =
                  (Form)Assembly.LoadFrom("CardManager.EXE").CreateInstance("CardManager." + frmName);
                this.OpenForm(objForm);
            }
            catch (Exception ex)
            {

                MessageBox.Show("错误提示:" + ex.Message,"提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

 

     2,打开窗体方法

        private void OpenForm(Form objForm)
        {
            objForm.TopLevel = false;
            objForm.Parent = this.pnlFill;
            objForm.FormBorderStyle = FormBorderStyle.None;
            objForm.StartPosition = FormStartPosition.CenterParent;
            objForm.WindowState = FormWindowState.Maximized;
            objForm.Show();
        }

 

    3,关闭窗体方法

        private void CloseForm()
        {
            foreach (Control item in this.pnlFill.Controls)
            {
                if (item is Form)
                {
                    Form objFrm = item as Form;
                    objFrm.Close();
                    this.pnlFill.Controls.Remove(item);
                }
            }
        }

     4,关闭系统事件的处理 ,用到窗体的FormClosing事件

      private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("真的要关闭系统吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if(result==DialogResult.Cancel)
            {
                e.Cancel = true;
            }
        }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!