c# 两个窗口。
form2经过一定操作后需要修改 form1的值。
//form1.cs.....................public System.Windows.Forms.ListBox listBox1;  
// 为了可以直接访问,我把它改成public了。public void setListBox(String s)
{  listBox1.Items.Add(s);
}//form2.cs.....................Form1 fm1 = new form1();
fm1.listBox1.Items.add("test text");   // way 1
fm1.setListBox("test text");           // way 2===================================================
两种方式都没有赋值成功。打断点看,way2的函数的参数都是传递成功了的。
现在看来单单只能在form1.cs中写 .items.add 才能成功。请问这是为什么?
怎样解决呢?谢谢各位!

解决方案 »

  1.   

    Form1 fm1 = new form1();
    问题在这儿这时的fm1其实是一个新的窗体,
      

  2.   

    //form2.cs.....................Form1 fm1 = GetOldForm1;fm1.listBox1.Items.add("test text");   // way 1
    fm1.setListBox("test text");           // way 2
      

  3.   

    楼主应该是想在Form2中新建窗体Form1,然后根据Form2的选项修改Form1中的listBox1吧?
      

  4.   

    http://bbs.cxbbs.com.ru/simple/index.php?t3006.html
      

  5.   

    参看
    http://blog.csdn.net/knight94/archive/2006/03/18/628285.aspx
      

  6.   

    谢谢各位。to wxdl1981:谢谢,明白了。
    to misvcom: form1是主窗口。是根据form2的选项改变form1的listbox
    to Knight94:两个星星就是老大哈~ 早都想到了。不过,偶太菜了。委托指的是什么阿
      

  7.   

    to 委托指的是什么阿Sample code as follows:
    ------Form2--------------
    public delegate void AddNewItemEvent( string NewValue );
    public AddNewItemEvent myAddNewItemHandler;
    private Form1 pForm1;
    public Form2( Form1 pParent )
    {
        pForm1 = pParent;
    }
    ---Add new value in form1 listbox using delegate method
    pForm1.Invoke( myAddNewItemHandler, new object{"New Item"} );
    ---------Form 1---------
    private void AddNewItem( string NewValue )
    {
        yourLstBox.Items.Add( NewValue );
    }----Open form2--------
    Form2 myForm2 = new Form2( this );
    myForm2.myAddNewItemHandler = new Form2.AddNewItemEvent( this.AddNewItem );
    myForm2.ShowDialog();
      

  8.   

    抱歉抱歉~~
    我还有个问题:pForm1.Invoke( myAddNewItemHandler, new object{"New Item"} );这里的第二个参数只能是 object;然后这里的 object 是为 
    private void AddNewItem( string NewValue ) 传递参数 NewValue 的吗?
    这个地方怎样实现呢。我想传递一个string。但是string不能被转成object,这里怎么办呢。
      

  9.   

    to
    pForm1.Invoke( myAddNewItemHandler, new object{"New Item"} );这里的第二个参数只能是 object;然后这里的 object 是为
    private void AddNewItem( string NewValue ) 传递参数 NewValue 的吗?是的,to 我想传递一个string。但是string不能被转成object,这里怎么办呢怎么可能不会转换呢,如下即可
    pForm1.Invoke( myAddNewItemHandler, new object{ yourString } );
      

  10.   

    winform的
    form1:
    public myButtonClass.MyButton myButton1;
    Bumon_depart bumon_depart=new Bumon_depart();
    bumon_depart.parent=this;
    this.myButton1.Enabled=false;form2:
    public  Form1 parent;
    this.parent.myButton1.Enabled=true;
    this.Dispose();
    this.parent.Show();参考这个
      

  11.   

    to Knight94:我没有用invoke 直接用了 myAddNewItemHandler(str);
    这样也行阿 为什么要用到invoke呢。还有pForm1.Invoke( myAddNewItemHandler, new object{ yourString } );
    这里的yourString,无论是string对象还是"string"这种临时对象,都说我的打括号是无效表达式,比如 pForm1.Invoke( myAddNewItemHandler, new object{ "ttt" } ); 另外看到说委托相当于c++的函数指针,完全是这样吗/谢谢~!
      

  12.   

    另外另外,通过form2的构造函数,传进了form1的引用,这个引用可以用来直接操作form1的对象吗private Form1 pForm1;
    public Form2( Form1 pParent )
    {
        pForm1 = pParent;
    }如果可以,委托又有什么好处呢。
      

  13.   

    其实,我并没有要求你用委托,在我的Blog中也提到了,操作另外窗口的控件,只需要提供方法即可。to 另外看到说委托相当于c++的函数指针,完全是这样吗/
    是这样的
      

  14.   

    这样的问题,一般都在构造函数里来实现:
    //form2.cs.....................Form1 fm1 = new form1("test text");//form1.cs
    public Form1(string str)
    {
      InitializeComponent();
     this.listBox1.Items.add(str); 
     
    }