本帖最后由 FWSH 于 2012-02-23 23:05:42 编辑

解决方案 »

  1.   

    在一个窗体中放入2个UserControl,控制Show/Hide
      

  2.   

    可以通过加 公共变量 和 公共方法,例:
       static class Program 
        {  //公共变量
            public static Form1 fm1; //窗体1
           public static Form2 fm2; //窗体2       static void Main()
           { //省略程序启动代码...}
        }    窗体Form1中公共方法: //在这里改成你自己需要的功能
        public void ChangeButton(bool bEnabled)
        {
            this.button1.Enabled = bEnabled;
        }
        
        private void frmMain_Load(object sender, EventArgs e)
        {   //Form1初始
            fm1 = this;
        }
        private void button1_Click(object sender, EventArgs e)
        {   //Form1按钮事件,这个达到目的,改了Form2的button状态
            if (fm2 != null)
               fm2.ChangeButton(true);
        }
        
       窗体Form2中公共方法: ////在这里改成你自己需要的功能
        public void ChangeButton(bool bEnabled)
        {
            this.button2.Enabled = bEnabled;
        }
        
        private void frmMain_Load(object sender, EventArgs e)
        {   //Form2初始
            fm2 = this;
        }
        private void button2_Click(object sender, EventArgs e)
        {   //Form2按钮事件,这个达到目的,改了Form1的button状态
             if (fm1 != null)
               fm1.ChangeButton(true);
        }
      

  3.   

    放入2个UserControl,控制Show/Hide
      

  4.   

    上面的代码 公共变量 使用漏了静态类,都要改为Program.fm1 和 Program.fm2,如
    fm1 = this;
    改为:
    Program.fm1 = this;
    你运行就会提示了,我在几个工具经常切换忘了,^_^
      

  5.   

    两位不睡觉吗?
    改变按钮的状态是什么意思?是按钮对应的函数被执行了还是什么?
    这个首先要在一个窗口中获取另外一个窗口对象。
    如果只是要实现这个功能的话,建议你用mdi来做,就是把一个作为子窗口,另一个作为父窗口。
    在Form1的属性中窗口样式里面把ismdicontener设置为true。
    添加一个Button button1
    public partial class Form1 : Form
        {
            Form2 f2;
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Form2 f22 =(Form2) this.MdiChildren[0];
                f22.SetButton();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                f2 = new Form2();
                f2.MdiParent = this;            //f2.WindowState = FormWindowState.Maximized;            f2.Show();
            }
            public void SetButton()
            {
                button1.Text = "fuck";
            }
        }
    Form1中是这个样子的。
    然后新建一个窗体Form2,添加一个按钮
    public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Form1 f1 = (Form1)this.ParentForm;
                f1.SetButton();
            }
            public void SetButton()
            {
                button1.Text = "fuck you";
            }
        }
      

  6.   

    然后是不用mdi的情况
    还是先新建一个窗体Form2
    关键是要让Form2中知道Form1.在Form2中声明一个Form1类型的全局变量f1.
    修改Form2的构造函数,增加一个Form1类型的参数,这样当Form1中创建Form2时,Form2就能得到Form1的信息。
    还要在Form1中添加一个Form2类型的全局变量。
    Form1中的代码:
    public partial class Form1 : Form
        {
            Form2 f2;
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                f2.SetButton();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                f2 = new Form2(this);
                f2.Show();
            }
            public void SetButton()
            {
                button1.Text = "fuck";
            }
        }
    Form2中的代码
    public partial class Form2 : Form
        {
            Form1 f1;
            public Form2(Form1 f)
            {
                InitializeComponent();
                f1 = f;
            }        private void button1_Click(object sender, EventArgs e)
            {
                f1.SetButton();
            }
            public void SetButton()
            {
                button1.Text = "fuck you";
            }
        }
      

  7.   

     private void Form1_Load(object sender, EventArgs e)
    这个函数是在Form1的事件中添加的,不要忘了添加这个事件
      

  8.   


    运行时在static class Program 行出现修饰符 static对该项无效提示