c# - 无法从其他类更改表单中的 textBox.text - C# - VS 2010

标签 c# visual-studio-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";

第一个文本框中没有任何变化。就像我根本没有按任何东西一样!!!

任何帮助将不胜感激

最佳答案

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

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

(我假设您的事件处理程序存在于 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";
}

此外,在您的“其他方法”上设置一个断点,以确保该代码甚至被触发。如果没有,请返回您的事件处理程序,确保它已被触发。如果没有,请检查您的事件接线。


尽管在所有情况下您都需要注意要更新的控件的保护级别...您需要将其设置为公开、内部或 protected ,具体取决于您的表单与其他类之间的关系,如果您想从 Form1 类之外更新它。

更好的 OO 方法是在 Form1 上有一个方法,允许其他类告诉 Form1 为它们更新控件(例如 updateTextBox(string新文本))。因为这不是面向对象的最佳实践,所以允许外部对象直接作用于类的成员(因为这需要了解类的内部结构……应该封装它,以便您的实现可以在不破坏现有接口(interface)的情况下进行更改在你的类(class)和外界之间)。

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

关于c# - 无法从其他类更改表单中的 textBox.text - C# - VS 2010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12879181/

相关文章:

c# - 表达式的值不变

visual-studio-2010 - 无法从特权运行的Visual Studio 2010拖放到已调试的应用程序运行

html - CSS 3.0 旋转(90deg) 不是 'transform' 属性的有效值

c# - 使用 Babel 将 C# 中的 JSX 转换为 JS

c# - 将 Color32 数组转换为字节数组以通过网络发送

c# - 在 1 000 000 的数组中查找整数和

visual-studio-2010 - 我如何(以及何时)将 TFS 与私有(private) DLL 一起使用,这些 DLL 也可以由 NuGet/NuPack 提供服务?

c++ - 视觉 C++ 2010 : Why "signed/unsigned mismatch" disappears if i add "const" to one comparand?

c++ - 未声明的标识符自定义类

c# - 将窗口关闭事件绑定(bind)到 ICommand