我有两个windowform窗体f1和f2,现想要将f1的一个全局变量string str传到f2的 textbox1中去,在f1事件代码怎么写,在f2的代码又怎么写。因为我传的只是一个值,所以不想再写一个类那么复杂,有没有更简单的方式来实现。

解决方案 »

  1.   

    'f1调用f2代码
    dim f2 as new form2
    f2.textbox1.text=me.str
    f2.showdialog
      

  2.   


    Form1 中
    private string str="This is a param";
    private btnShow_Click(object sender,EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.Show();
    }
    Form2 中
    重写构造方法
    public Form2(string str)
    {
        InitializeComponent();
        this.textBox1.Text=str;
    }
      

  3.   


    楼上的不错,就是漏了在FROM1中声明FORM2的时候传入的STR了
    Form1 中
    private string str="This is a param";
    private btnShow_Click(object sender,EventArgs e)
    {
        Form2 form2 = new Form2(str);
        form2.Show();
    }Form2 中
    public Form2(string str)
    {
        InitializeComponent();
        this.textBox1.Text=str;
    }
      

  4.   

    f1的一个全局变量string str 是什么样的啊,public还是private?如果是public 就可以直接访问的。如果是private,你可以根据你的具体需求在其中一个Form中设置一个public的static变量。通过它来传递值。
      

  5.   

     public delegate void mydelegate(string info);//定义一个委托类型//Form1的代码
    public partial class Form1 : Form
        {
             .  ..  ....//省略系统代码
            public static event mydelegate myevent;//定义事件
           
            //当按钮被点,就传递数据
            private void button2_Click(object sender, EventArgs e)
            {
                string s = "aa";//要传递的数据,这样的数据不需要全局的变量
                myevent(s);//触发事件
            }
            //创建Form2窗体,其实可以在任何地方创建都无所谓
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2();
                frm.Show();
            }
    }//Form2中的代码
           private void Form2_Load(object sender, EventArgs e)
            {
                Form1.myevent += new mydelegate(Form1_myevent);//实例化事件对象
            }        void Form1_myevent(string info)//定义事件调用的方法
            {
                this.textBox1.Text = info;
            }
      

  6.   

    这样的好处是不需要将Form1和Form2关联起来
    比如:
    Form1的button1_Click创建的是Form3
    Form3的button1_Click创建的是Form2
    当我们点Form1的button1时候仍然能将数据传递到Form2用其他方法的灵活性不强
      

  7.   

    为自己的前一个Form增加一个属性:private string changeMessage="";
    ///<summary>
    ///交换信息的属性
    ///</summary>
    public string ChangeMessage
    {
      get{return this.changeMessage;}  //只保留一个读的属性
    }
      

  8.   

    Form1 中
    private string str="This is a param";
    private btnShow_Click(object sender,EventArgs e)
    {
        Form2 form2 = new Form2(str);
        form2.Show();
    }Form2 中
    public Form2(string str)
    {
        InitializeComponent();
        this.textBox1.Text=str;
    }
      

  9.   

    Form1 中 
    public static string str="This is a param"; 
    private btnShow_Click(object sender,EventArgs e) 

        Form2 form2 = new Form2(str); 
        form2.Show(); 
    } Form2 中 this.textBox1.Text=Form1.str; 
      

  10.   

    form1
    public string aa = "ssssssxs";
    Form2 form = new Form2(aa);form2
    InitializeComponent(); 
    this.textBox.Text = aa;
      

  11.   

            FORM1中
            public string str = " this is form1";
            private void button1_Click(object sender, EventArgs e)
            {
                //string str = "-1-2-3-4-5";
                //foreach (string s in str.Split('-'))
                //{
                //    listBox1.Items.Add(s);
                //}
                ////textBox1.Text = textBox1.Text + "-";
                //bind("tablea");
                Form2 form2 = new Form2(str);
                form2.Show();
            }
    FORM2中
    public Form2(string str)
            {
                InitializeComponent();
                textBox1.Text = str;
            }
      

  12.   

    f1中定义一个
    class Global
           {
            
                public static string n;
           }
            赋值:
    Global.n="zifuchuan";
    f2中   textBox1.Text = Global.n.ToString();
      

  13.   

    FORM1中
    public string str = " this is form1";
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2(str);
        form2.Show();
    }FORM2中
    public Form2(string str)
    {
        InitializeComponent();
        textBox1.Text = str;
    }
      

  14.   

    用构造函数呀,3楼的就很正确的。当Show窗体的时候在构造函数里面把那个变量传过去。你试一下。
      

  15.   

    不知道为什么都喜欢构造函数传值,如果两个窗体已经存在
    数据是在Form用户操作后再传递到Form2,难道要重新New一
    个新的 Form2窗体出来么!!!
    在Windows应用程序中,消息处理机制才是基础,一个窗体发送
    消息,一个窗体接收消息处理数据才是王道,请在代码设计过程
    中注意代码的灵活性,维护性和开展性的问题,不能是把代码写
    成死的!!!