class A
{
   public string returnStr(string str)
   {
       this.textBox1.Text = str;
       return this.textBox2.Text;
   }
   private void  Button_Click(Object o,EventArgs e)
   {
       this.textBox2.Text = "ddddddddddddd";
   }
}
------------------------------------------------------------------
class B
{
   A st=new A();
  //在此处触发class A 中的Button事件
   string newStr=st.returnStr("ssssssssssss");
}  想在类B中调用类A的方法returnStr,返回this.textBox2.Text的值,在类B中如何触发类A中Button_Click执行

解决方案 »

  1.   

    在A类的returnStr方法中调用即可:   public string returnStr(string str)
       {
           this.textBox1.Text = str;
           return this.textBox2.Text;       //调用Button_Click
           Button.PerformClick();
       }这个方法有效的前提是Button是一个按钮控件。
      

  2.   

    首先你要把class A 设为public 其次private void  Button_Click(Object o,EventArgs e)
    也要改为public void  Button_Click(Object o,EventArgs e),然后 A st=new A();
      //在此处触发class A 中的Button事件
       string newStr=st.Button_Click(this,e)就可以了
      

  3.   


    调用要提前,我刚才顺序错了,因为你先return了,这样肯定可以:   public string returnStr(string str)
       {
           this.textBox1.Text = str;       //调用Button_Click
           Button.PerformClick();       return this.textBox2.Text;   }
      

  4.   

            //要触发的按钮事件
            private void button2_Click(object sender, EventArgs e)
            {
                MessageBox.Show("button2 clicked~ ");
            }
            delegate void btnclick(object sender, EventArgs e);
            //通过委托调用button2_Click事件
            private void button1_Click(object sender, EventArgs e)
            {
                this.Invoke(new btnclick(button2_Click),new object[]{sender, e});
            }
      

  5.   

    class A
    {
       public string returnStr(string str)
       {
           this.textBox1.Text = str;
           return this.textBox2.Text;
       }
       private void  Button_Click(Object o,EventArgs e)
       {
           this.textBox2.Text = "ddddddddddddd";
       }
    }
    ------------------------------------------------------------------
    class B
    {
       A st=new A();
      //在此处触发class A 中的Button事件
       string newStr=st.returnStr("ssssssssssss");
    }在类B中如何触发类A中Button_Click执行
    ---------------------
    如下:
    class A
    {
       public string returnStr(string str)
       {
           this.textBox1.Text = str;
           return this.textBox2.Text;
       }
       private void  Button_Click(Object o,EventArgs e)
       {
           this.textBox2.Text = "ddddddddddddd";
       }
       public void ClickButton()
       {
          Button_Click(this,new EventArgs()); //this最好是改为你的button
       }
    }
    ------------------------------------------------------------------
    class B
    {
       A st=new A();
      //在此处触发class A 中的Button事件
       A.ClickButton();
       string newStr=st.returnStr("ssssssssssss");
    }
      

  6.   

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                
            }
            private void button2_Click(object sender, EventArgs e)
            {
                a a = new a();
                a.t();
            }
        }
        class a
        {
            public void t()
            {
                b b1 = new b();
                b1.btn.PerformClick();
                //在此处触发class A 中的Button事件
                //string newStr = st.returnStr("ssssssssssss");        }
        }
        class b
        {
            public Button btn;
            public b()
            {
                btn = new Button();
                btn.Click+=new EventHandler(btn_Click);
            }
            void btn_Click(object sender, EventArgs e)
            {
                MessageBox.Show("ok");
            }
        }