在从其他类形式不能改变textBox.text - C# - VS 2010 [英] Can't change textBox.text in form from other classes - C# - VS 2010

查看:174
本文介绍了在从其他类形式不能改变textBox.text - C# - VS 2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Form1窗体。在这里面,我有一个名为加密按钮。当我打的按钮,我援引不同的类中的方法。我想这个方法来改变textBox1.text的价值,但是当我在Form1类中使用此代码

I have a form called Form1. In it, I have a button called Encrypt. When I hit that button I invoke a method in a different class. I want that method to change the value of textBox1.text but nothing happens when I use this code

public string txtbox1
    {
        get
        {
            return textBox1.Text;
        }

        set
        {
            textBox1.Text = value;
        }
    }



在其他类中的方法



in a method in the other class

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



没有在第一个文本框的变化。这就像我根本不!

Nothing changes in the first text box. It's like I don't press anything at all!!!

任何帮助将是非常赞赏

推荐答案

在你的其他类,则需要在其上单击该按钮的形式引用(并具有现有的文本框),而不是一个的新的的形式。

In your other class, you need a reference to the Form on which the button was clicked (and that has your existing text-boxes), not a new form.

你实例化这个新的表单是不是你要找在您点击您的按钮在屏幕上的人。

This new form you're instantiating isn't the one that you're looking at on the screen where you clicked your button.

(我假设Form1类中存在的事件处理程序,它然后选择转发作为必要的信息给其他类的方法吗?如果不是......它应该!)

(I'm assuming your event handler exists within the Form1 class, and that it then "forwards" information out to other class's method as required? If not... it should!)

按钮参考将通过发送者可获得对象和事件参数传递到您的事件处理程序。你可以通过在这个关键字到其他类的方法传递到当前 Form1中引用实例。或者你可以通过发件人如果这是对你有用,或者只是通过您的其他方法传递一个明确提及具体的文本框。

The button reference will be obtainable through the sender object and the event args passed to your event handler. You can pass a reference to the current Form1 instance by passing the this keyword to your other class's method. Or you could pass the sender if that's useful to you, or just pass an explicit reference to the specific text box through to your other method.

例如,向窗体的引用传递到您的另一种方法:

eg, to pass a reference to the form to your other method:

// 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";  
}



例如,向明确的文本框中输入一个引用传递给你的另一种方法:<在窗体
私人无效的button1_Click(对象发件人,EventArgs五)
{$ / p>

eg, to pass a reference to explicit text box to your other method:

// 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";
}



例如,事件对象传递给你的另一种方法:

eg, to pass the event object to your other method:

// 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";
}



另外,贴在你的其他方法一个破发点,以确保该代码甚至被解雇。如果没有,回到你的事件处理程序,确保公司被解雇。如果没有,请检查您的活动线了。

Also, stick a break point on your "other method" to ensure this code is even being fired. If not, go back to your event-handler, ensure that's being fired. If not, check your event wire-up.

虽然在所有的情况下,你需要小心的保护级别上要更新控制......你需要把它公开,内部,或受保护的取决于你的形式与其他类之间的关系,如果你想从你的外 Form1中更新类。

Although in all cases you need to be careful of the protection level on the control you want to update... you'll need to make it public, internal, or protected depending on the relationship between your form and the other class, if you want to update it from outside your Form1 class.

一个更好的面向对象的方法是对 Form1中一个方法允许其他类告诉 Form1中来更新他们的控制(例如 updateTextBox(字符串newText))。因为它不是面向对象的最佳实践允许外部物体作用于直接在类的成员(因为这需要你的类的内部结构......这应该被封装,使您的实现可以改变而不破坏存在于界面的知识。您的类和外部世界)之间

A better OO approach would be to have a method on Form1 that allows other classes to tell Form1 to update the control for them (eg updateTextBox(string newText)). Because it's not OO best-practice allow external objects to act on the members of your classes directly (as this requires knowledge of the internal structure of your class... which should be encapsulated so that your implementation can change without breaking the interface that exists between your class and the outside world).

编辑:
实际上,在重新阅读你的问题,你就已经封装你的文本框使用的get / set属性。尼斯。所以,你应该通过提及您的表格,你的其他方法,然后通过更新属性窗体的文本。添加了这个方法上面的例子。

Actually, on re-reading your question, you do already encapsulate your text boxes using get/set properties. Nice. So you should pass the reference to your Form to your other method, and then update the form's text via the property. Have added this method to the examples above.

这篇关于在从其他类形式不能改变textBox.text - C# - VS 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆