我有三个窗体,form1,form2,from3。form1中有两个按钮:button1,button2。form2中有1个按钮:button3。form3中有一个label。我想当form1运行的时候,单击button1,就可以弹出非模态窗口form3,单击button2,就可以弹出模态窗口form2,当我在form2中单击button3的时候,在关掉button2的同时,改变form3中label的值,不知道该如何去做,请高手帮忙,问题解决就结贴。

解决方案 »

  1.   

    有两种方式
    1、把form3中的label设为全局变量
    2、定义委托
    方法1简单,适合初学者,方法2是比较正规的方式
      

  2.   


    Form2不控制form3吧,你有这需求?
      

  3.   

    有需要的话,可以传参到form画面,然后新开一个窗体,显示值,讲原来的关掉
      

  4.   

    是窗体间的跳转 想跳哪就new 哪
    学习
      

  5.   

    http://blog.csdn.net/xiaogang3438792/archive/2009/05/12/4174602.aspx
    这里有四种传值方法,讲的很详细 去看看吧
      

  6.   

    你这里的“当我在form2中单击button3的时候,在关掉button2的同时”我的理解可能应该是“当我在form2中单击button3的时候,在关掉form2的同时”,对吗?如果是这样,我的代码如下:
    public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }        //为你的私有标签做一个属性,以保证在外部可以通过属性对私有标签进行修改
            public Label MyLabel
            {
                get
                {
                    return this.lblInForm3;
                }
                set
                {
                    this.lblInForm3 = value;
                }
            }
        } public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void btnCloseAndEditLabel_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        Form2 f2 = null;
            Form3 f3 = null;        private void btnOpenForm2_Click(object sender, EventArgs e)
            {
                f2 = new Form2();
                f2.Show();
                f2.FormClosing += new FormClosingEventHandler(f2_FormClosing);
            }        void f2_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (f3 != null)
                {
                    f3.MyLabel.Text = "修改后的文本";
                    f3.Select();//选中
                }
            }        private void btnOpenForm3_Click(object sender, EventArgs e)
            {
                f3 = new Form3();
                f3.Show();
            }
        }
      

  7.   

    1.在form3中定义公共属性就行了2.在Program中定义public static form3
    总体代码参考:
    Program.csusing System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        static class Program
        {
            public static Form3 myForm3 = null;
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
    2. From1
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                if (Program.myForm3 == null)
                {
                    Program.myForm3 = new Form3();
                    Program.myForm3.Show();
                }
                else
                {
                    Program.myForm3.Show();
                    Program.myForm3.label1.Text = "form1打开时的值";
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                
            Form2 frm2 =  new Form2();
            frm2.Show();
            }
        }
    }2. from2.cs
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                if (Program.myForm3 == null)
                {
                    Program.myForm3 = new Form3();
                    Program.myForm3.Show();
                    Program.myForm3.label1.Text = "form2修改后的值";
                }
                else
                {                Program.myForm3.Show();
                    Program.myForm3.label1.Text = "form2修改后的值";
                }
            }
        }
    }3.From3.cs
    namespace WindowsFormsApplication1
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }        private void Form3_Load(object sender, EventArgs e)
            {        }
        }
    }
    namespace WindowsFormsApplication1
    {
        partial class Form3
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.label1 = new System.Windows.Forms.Label();
                this.SuspendLayout();
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(83, 73);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(65, 12);
                this.label1.TabIndex = 0;
                this.label1.Text = "from1 打开";
                // 
                // Form3
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(507, 265);
                this.Controls.Add(this.label1);
                this.Name = "Form3";
                this.Text = "Form3";
                this.Load += new System.EventHandler(this.Form3_Load);
                this.ResumeLayout(false);
                this.PerformLayout();        }        #endregion        public System.Windows.Forms.Label label1;
        }
    }
      

  8.   

        我按照你的方法试了,但是 void f2_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (f3 != null)
                {
                    f3.MyLabel.Text = "修改后的文本";
                    f3.Select();//选中
                }
            }
    这个方法 没有进去,调试过。所以还不行。
      

  9.   

    f2.FormClosing += new FormClosingEventHandler(f2_FormClosing);
    这条代码你写了没有,我执行时一点问题都没有
      

  10.   

    Using a delegate to pass data between two forms
      

  11.   

         我试了下你的方法可以了,但是当我想把 
                  f2 = new Form2();
                f2.Show();
                f2.FormClosing += new FormClosingEventHandler(f2_FormClosing);
    f2.Show()改成f2.ShowDialog()就不行了,我调试的时候到f2.ShowDialog()这里后就弹出F2窗体,当关闭的时候下面的f2.FormClosing += new FormClosingEventHandler(f2_FormClosing);执行了,但是这个f2_FormClosing方法没有进去,是不是这个方法只能用show()方法啊。
      

  12.   

    这个好解决f2.FormClosing += new FormClosingEventHandler(f2_FormClosing); 
    f2.ShowDialog();             交换一下顺序就可以了