RT
我在网上看到了很多窗体传值的方式,但是普通的类如何控制就不知道了。我在 form中new了一个对象,让后把form传进去,但是也不能修改Textbox上的值啊。

解决方案 »

  1.   

    没甚理解 楼主的意思 看下:
     class Class1
        {
            public static void changeTextValue(System.Windows.Forms.TextBox tb,string v) {
                tb.Text = v;
            }
        }
       private void button1_Click(object sender, EventArgs e)
            {            Class1.changeTextValue(textBox1, "test");//textBox1为from中textbox 的id
    }
      

  2.   


    我要在class1中修改 Form中的值,好像你这个是反的
      

  3.   

    button1就是 from 中的按钮啊
    textBox1为form 中的文本框楼主可否举个例子,具体的功能
      

  4.   

    一个普通类给txtBox赋值
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                A a = new A();
                txtBox1.Text = a.TextString;
            }
        }    class A
        {
            public string TextString
            {
                get { return "Yes"; }
            }
        }
    new了一个对象,让后把form传进去
     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                A a = new A();
                a.SetTxtBoxTest(this, "Yes");
            }        
        }    class A
        {
            public void SetTxtBoxTest(Form1 form1,string Text)
            {
                form1.txtBox1.Text = Text;
            }
        }
      

  5.   

    public class A{
    public void xxx(){
    //我希望能在这里修改到form控件中的Textbox
    }
    }
      

  6.   

    就是把窗体的实例传到A类的SetTxtBoxTest方法里面去,在方法内部对窗体的成员进行赋值,还有哪里不明白?
      

  7.   

    通过Set方法,窗体Form1把自己交给A类去操作,A喜欢怎么搞就怎么搞
      

  8.   

    public class A{
    public void xxx(){
    //我希望能在这里修改到form控件中的Textbox
    }
    }
    我给你写的例子 就是你要的啊
      

  9.   


    我是通过构造器的方式传入的。在form中,
    A a = new A(this);
    然后在A类中想修改Form中的Textbox。这样为什么不行啊?
      

  10.   

    可以啊,这样的话你就在A类的构造函数里面写了
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            A a = new A(this);
            }        
        }    class A
        {
            public A(Form1 from1)
            {
                from1.txtBox.Text = "Yes!";
            }
        }
      

  11.   


    我不想在构造器中对form 的Textbox赋值。我在class A 中这样写的。
    Class A{
    private Form f;
    public A (Form f){
    this.f = f;

    public void otherFunl(){
    f.Textbox.text = "xxx";//这样就不行。访问不到到Textbox
    f.setText();//直接调用我在form中写的方法也不行。
    }

      

  12.   


    公开了。
     private System.Windows.Forms.Button button5;
            public System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.Button button6;
      

  13.   

    form中默认控件都是private的 你把要textbox改成public
    主要是在form中定义一个public的方法 然后通过方法来更改textbox的值
      

  14.   

    把窗体中的TextBox公开就可以了,或者在窗体公开一个方法去修改这个TextBox的Text值,或者公开一个属性去获取或设置这个TextBox的值就行了。
    比如:在你的窗口中有一个TextBox的控件,名称为tbTest,那么可以通过以下方法设置:
    1、public TextBox TbTest{
    get{return this.tbTest;}
    }
    2、public string TbTestStr{
    get{return this.tbTest.Text;}
    set{this.tbTest.Text = value;}
    }
    3、public void SetTestText(string text){
    this.tbTest.Text = text;
    }
      

  15.   


    3、public void SetTestText(string text){
    this.tbTest.Text = text;
    }
    我就是这样的写的,但是还是访问不到。很奇怪
      

  16.   

    Class A{
    private Form f;
    public A (Form f){
    this.f = f;

    public void otherFunl(){
    f.Textbox.text = "xxx";//这样就不行。访问不到到Textbox
    f.setText();//直接调用我在form中写的方法也不行。
    }

    Form 改成 Form1   改成你写的窗体的那个类名称