比如说我主窗体里有个label的text值为5,我又建了个对话框,要在对话框里显示这个值,怎么弄?

解决方案 »

  1.   

    你建一个类吧,这是我的例子
    using System;
    using System.Collections.Generic;
    using System.Text;namespace xiaopingguo
    {
        class change
        {
            private static string logevent;
            public static string getevent
            {
                get
                {
                    return logevent;
                }
            }
            public static string setevent
            {
                set
                {
                    logevent = value;
                }
            }
            当主窗体的label1值变化时添加语句:change.setevent=label1.Text;
            对话框调用时添加语句:textBox1.Text=change.getevent;
      

  2.   

    我这个是在不同窗体间调用的,如果是同一个窗体那就直接这样写:
    textBox1.Text=lable1.Text
      

  3.   

    在创建新窗体时,将label的text传到新窗口中去
    Form2 form=new Form2(label.Text)
      

  4.   

    Form1.cs:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.Text = DateTime.Now.ToString(); //设置textBox1值
                (new Form2(textBox1.Text)).Show();//调用非默认构造函数
            }
        }
    }
    Form2.cs:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
                : this("")
            { }        /// <summary>
            /// 非默认构造函数
            /// </summary>
            /// <param name="text"></param>
            public Form2(string text)
            {
                InitializeComponent();
                label1.Text = text;
            }        private void Form2_Load(object sender, EventArgs e)
            {        }
        }
    }
      

  5.   

    1.使用构造函数Form1.cs:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.Text = DateTime.Now.ToString(); //设置textBox1值
                (new Form2(textBox1.Text)).Show();//调用非默认构造函数
            }
        }
    }
    Form2.cs:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
                : this("")
            { }        /// <summary>
            /// 非默认构造函数
            /// </summary>
            /// <param name="text"></param>
            public Form2(string text)
            {
                InitializeComponent();
                label1.Text = text;
            }        private void Form2_Load(object sender, EventArgs e)
            {        }
        }
    }2.使用Show(IWin32Window owner)构造函数的缺点是传递是一次性的,你不能动态去改变,而下边的方法可以:Form1.cs:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.Text = DateTime.Now.ToString();//设置textBox1值
                (new Form2()).Show(this);//显示Form2
            }        public string BoxText
            {
                get { return textBox1.Text; }
                set { textBox1.Text = value; }
            }
        }
    }
    Form2.cs:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void Form2_Load(object sender, EventArgs e)
            {        }        private void button1_Click(object sender, EventArgs e)
            {
                //不能放在Form2_Load中执行,否则出现未将对象引用到对象实例
                Form frm = this.Owner;
                if (frm is Form1)
                    label1.Text = ((Form1)frm).BoxText;
            }
        }
    }
    这种方法的缺点是不能在Form2一显示的时候获取并设置值,但两个方法可以放在一起使用-_-
      

  6.   


    Form2 frm=new Form2;
    frm.ShowDialog(mainFrm);
    this.Textbox1.text= mainFrm.label1.text;//这是一种
      

  7.   

    方法1:新创建对话框提供一个接口(如public 属性或是public 方法),在该接口代码里完成新对话框要进行的操作,方法2:运用单例模式用作全局数据区