button1就是 from 中的按钮啊 textBox1为form 中的文本框楼主可否举个例子,具体的功能
一个普通类给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; } }
public class A{ public void xxx(){ //我希望能在这里修改到form控件中的Textbox } }
public class A{ public void xxx(){ //我希望能在这里修改到form控件中的Textbox } } 我给你写的例子 就是你要的啊
我是通过构造器的方式传入的。在form中, A a = new A(this); 然后在A类中想修改Form中的Textbox。这样为什么不行啊?
可以啊,这样的话你就在A类的构造函数里面写了 public partial class Form1 : Form { public Form1() { InitializeComponent(); A a = new A(this); } } class A { public A(Form1 from1) { from1.txtBox.Text = "Yes!"; } }
我不想在构造器中对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中写的方法也不行。 } }
公开了。 private System.Windows.Forms.Button button5; public System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button6;
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 改成你写的窗体的那个类名称
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
}
我要在class1中修改 Form中的值,好像你这个是反的
textBox1为form 中的文本框楼主可否举个例子,具体的功能
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;
}
}
public void xxx(){
//我希望能在这里修改到form控件中的Textbox
}
}
public void xxx(){
//我希望能在这里修改到form控件中的Textbox
}
}
我给你写的例子 就是你要的啊
我是通过构造器的方式传入的。在form中,
A a = new A(this);
然后在A类中想修改Form中的Textbox。这样为什么不行啊?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); A a = new A(this);
}
} class A
{
public A(Form1 from1)
{
from1.txtBox.Text = "Yes!";
}
}
我不想在构造器中对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中写的方法也不行。
}
}
公开了。
private System.Windows.Forms.Button button5;
public System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button6;
主要是在form中定义一个public的方法 然后通过方法来更改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;
}
3、public void SetTestText(string text){
this.tbTest.Text = text;
}
我就是这样的写的,但是还是访问不到。很奇怪
private Form f;
public A (Form f){
this.f = f;
}
public void otherFunl(){
f.Textbox.text = "xxx";//这样就不行。访问不到到Textbox
f.setText();//直接调用我在form中写的方法也不行。
}
}
Form 改成 Form1 改成你写的窗体的那个类名称