RadioButton与Menustrip的细节问题

1.在窗体拖入Menustrip控件

2.设置此From的MainMenuStrip的属性为Menustrip,IsMdiContainer为True

3.显示子窗体方法:

 

      private void ShowChildForm(string fromName, Form form)
        {
            if (this.MdiChildren.Length > 0)  //这里进行判断下,用来重置窗体控件
                this.ActiveMdiChild.Dispose();
            if (checkchildFrmExist(fromName)) { return; }//此处要判断下,是否已经打开
            form.MdiParent = this;
            form.Show();
            form.Dock = DockStyle.Fill;
        }
     
        #region 查询窗体Name,然后激活窗体 +checkchildFrmExist(string childFrmName)
        /// <summary>
        /// 查询窗体Name,然后激活窗体
        /// </summary>
        /// <param name="childFrmName"></param>
        /// <returns></returns>
        public bool checkchildFrmExist(string childFrmName)
        {
            foreach (Form childFrm in this.MdiChildren)
            {
                if (childFrm.Name.ToString().Equals(childFrmName))
                {
                    if (childFrm.WindowState == FormWindowState.Minimized)
                    {
                        childFrm.WindowState = FormWindowState.Normal;

                    }
                    childFrm.Activate();

                    return true;

                }
            }
            return false;
        } 
        #endregion
   
      

 4.使用方法,如下:

 private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowChildForm("密码修改", new PassWordForm());
        }

 5.RadioButton上场,例如界面有如下,注意:这里要给个Textbox,让他的Tabindx优先与Radiobutton,这样打开,按钮才不会选中。要控件聚焦到非RadioButton上

6.后台代码如下:

 

public partial class 城市数据 : Form
    {
        public 城市数据()
        {
            InitializeComponent();
            
           
            rb_Person.CheckedChanged += new EventHandler(rb_CAI_CheckedChanged);//加载 rb_Person事件
          
        }

        
        private void rb_CAI_CheckedChanged(object sender, EventArgs e)
        {
            bool isClick = ((RadioButton)sender).Checked;//判断是否有radiobutton选中
            if (!isClick)//没有选中,return
                return;

            string ControlName=   ((RadioButton)sender).Name.ToString();//radiobutton选中的Name
            ReflshControl reflsh = NoCheckedRadio;
            switch (ControlName)
            {
                case "rb_CAI":
                    new Ui.CAI(reflsh).ShowDialog();
                    break;
                case "rb_Person":
                    new Ui.Person(reflsh).ShowDialog();
                    break;
                default:
                    break;
            }






        }


        public delegate void ReflshControl();//刷新Radiobutton不选中状态
        public void NoCheckedRadio()
        {
            rb_CAI.Checked = rb_Person.Checked = false;//设置不选中
     
        }



    }

 7.CAI窗体页面代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Win_Main.Ui
{
    public partial class CAI : Form
    {
        城市数据.ReflshControl reflsh;
        public CAI(城市数据.ReflshControl _reflsh)
        {
            InitializeComponent();
            reflsh = _reflsh;//刷新父页面的控件状态
        }

        private void CAI_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (reflsh != null)
                reflsh();//执行刷新
        }
    }
}

 总结,要注意,Menustrip的设置,最关键的是,每次打开子窗体的时候,要判断下是否销毁。如果有,就销毁,否则的话,如何重绘窗体状态。

 源码下载

posted @ 2014-10-17 11:07  寻找灯塔ing  阅读(277)  评论(0)    收藏  举报