无法从其他类中更改textBox.text – C# – VS 2010

我有一个名为Form1的表单。 在其中,我有一个名为Encrypt的按钮。 当我按下那个按钮时,我在另一个类中调用一个方法。 我希望该方法更改textBox1.text的值,但是当我使用此代码时没有任何反应

在Form1类中

public string txtbox1 { get { return textBox1.Text; } set { textBox1.Text = value; } } 

在另一个类的方法中

 Form1 textboxes = new Form1();//creating object of the Form1 class textboxes.txtbox1= "whatever"; 

第一个文本框中没有任何变化。 这就好像我什么都不按!

任何帮助将非常感激

在您的其他课程中,您需要引用单击按钮的表单(并且具有现有的文本框),而不是表单。

您实例化的这个新表单不是您在单击按钮的屏幕上查看的表单。

(我假设您的事件处理程序存在于Form1类中,然后根据需要将信息“转发”到其他类的方法?如果不是……它应该!)

可以通过sender对象获取按钮引用,并将event args传递给事件处理程序。 您可以通过将this关键字传递给其他类的方法来传递对当前Form1实例的引用。 或者您可以传递sender如果这对您有用,或者只是将对特定文本框的显式引用传递给您的其他方法。

例如,将表单的引用传递给您的其他方法:

 // Event handler on your form private void button1_Click(object sender, EventArgs e) { ButtonWasPressedOnForm(this); } // Other method in your other class public void ButtonWasPressedOnForm(Form formWhereButtonPressed) { // To act on the text box directly: TextBox textBoxToUpdate = (TextBox)formWhereButtonPressed.Controls.Find("textBox1"); textBoxToUpdate.Text = "whatever"; // Or, using the Form1.txtBox1 property. formWhereButtonPressed.txtBox1 = "whatever"; } 

例如,将对显式文本框的引用传递给您的其他方法:

 // Event handler on your form private void button1_Click(object sender, EventArgs e) { ButtonWasPressedOnForm(textBox1); } // Other method in your other class public void ButtonWasPressedOnForm(TextBox textBoxToUpdate) { textBoxToUpdate.Text = "whatever"; } 

例如,将事件对象传递给您的其他方法:

 // Event handler on your form private void button1_Click(object sender, EventArgs e) { Button clickedButton = (Button)sender; ButtonWasPressedOnForm(clickedButton); } // Other method in your other class public void ButtonWasPressedOnForm(Button clickedButton) { Form theParentForm = clickedButton.FindForm(); // To act on the text box directly: TextBox textBoxToUpdate = (TextBox)theParentForm.Controls.Find("textBox1"); textBoxToUpdate.Text = "whatever"; // Or, To act on the text box, via the form's property: theParentForm.txtBox1 = "whatever"; } 

另外,在“其他方法”上添加一个断点,以确保此代码被触发。 如果没有,请返回事件处理程序,确保被触发。 如果没有,请检查您的活动连线。


虽然在所有情况下你都需要注意你想要更新的控件的保护级别……你需要根据表单与其他类之间的关系将其设置为公共,内部或受保护,如果你想要从Form1类外部更新它。

更好的OO方法是在Form1上有一个方法,允许其他类告诉Form1更新它们的控件(例如updateTextBox(string newText) )。 因为它不是OO最佳实践允许外部对象直接作用于类的成员(因为这需要知道类的内部结构…应该封装,以便您的实现可以更改而不会破坏存在的接口你的class级和外界之间)。

编辑:实际上,在重新阅读您的问题时,您已经使用get / set属性封装了文本框。 尼斯。 因此,您应该将对Form的引用传递给其他方法,然后通过属性更新表单的文本。 已将此方法添加到上面的示例中。

我很容易做到这一点。 首先在类中创建一个TextBox

 class class1 { public static TextBox txt1=new TextBox(); public static void Hello() { txt1.Text="Hello"; } } 

form1上有一个按钮(button1)和一个TextBox(textBox1)。 好的,现在复制你想要的TextBox的地址必须改变我测试它,它工作正常

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { class1.txt1=textBox1; class1.Hello(); } } 

另外,Form1类的保护级别是什么? 这就是我经常遇到问题的地方……我忘记了如果这个类不公开,那么它就不能被它外面的类访问。 “受保护”也可以使用,但这取决于两个类之间的关系。

我发现在这样的情况下,将信息从1个表单传递到另一个表单是将信息保存在类文件中而不是其自身的forms

示例表1:

 public partial class customerHistory : Form { private string passtexboxvaluetoclass = ""; // load class dependant.cs dependents cls_dependents = new dependents(); public customerHistory() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { dependents.passtexboxvaluetoclass = textbox1.text; // load next form } 

然后我要做的就是在第二个表单上调用form_load

 label1.text = dependents.passtexboxvaluetoclass; 

通过这种方式,您可以回调大量信息,因为它们都将存储在类中,而不是物理forms本身

您也可以通过在类中创建事件来执行此操作,并且您的表单订阅该事件并将数据传递到UI作为EventArgs