从我的Form类C#之外的另一个类更新文本框

时间:2016-12-21 17:28:18

标签: c# forms class textbox updates

我已经看到了一些有关此尝试的链接,但我还没有找到解决方案。我试图访问我的表单文本框并使用另一个类的文本更新它。我可以直接在DataOrganizerForm类中更新文本,但是当我将文本传递回DataOrganizerForm类时,它不会在GUI上更新。这就是我所拥有的:

public partial class DataOrganizerForm : Form
{        
    //Default constructor
    public DataOrganizerForm()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    //Handle a Start/Stop button click
    private void start_stop_button_Click(object sender, EventArgs e)
    {
        SerialNumberSearcher snsearch = new SerialNumberSearcher();
        snsearch.searchSN();
    }

    //Allow simple access to update to notification textbox
    public void setNotificationText(string text)
    {
        notification_textbox.Text = text;            
    }
}

public class SerialNumberSearcher
{
    public void searchSN()
    {
        DataOrganizerForm formAccess = new DataOrganizerForm();
        formAccess.setNotificationText("Updated text from different class"); 
    }        
}

3 个答案:

答案 0 :(得分:0)

好吧,它不会更新文本框,因为你要实例化DataOrganizerForm类的另一个对象。你可以做的是将表单对象的引用传递给SerialNumberSearcher,如下所示:

public class SerialNumberSearcher
{
    private readonly DataOrganizerForm _form;  

    public SerialNumberSearcher(DataOrganizerForm form)
    {
        _form = form;
    }

    public void searchSN()
    {
        _form.setNotificationText("Updated text from different class"); 
    }  
}

答案 1 :(得分:0)

您需要了解您操作的实例。当您使用new-eperator时,您可以创建实例,就像该类型的新副本一样。

DataOrganizerForm formAccess = new DataOrganizerForm();

原始Form是您在searchSN方法中创建的实例。

您需要将该实例传递给方法来操作它:

public void searchSN(DataOrganizerForm formAccess )
{
    formAccess.setNotificationText("Updated text from different class"); 
}  

如果要调用此方法,则需要使用this来引用当前对象:

private void start_stop_button_Click(object sender, EventArgs e)
{
    SerialNumberSearcher snsearch = new SerialNumberSearcher();
    snsearch.searchSN(this);
}

this将访问Form的当前实例,从而允许您操作您感兴趣的文本框。

When do you use the “this” keyword?也可能有帮助

答案 2 :(得分:0)

感谢您的帮助。这是我能够使我的应用程序工作的方法。我通过引用传递Textbox对象到我的其他类,并能够以这种方式显示我的信息。此外,我有问题让我的文本框不断更新。我不得不添加

public partial class DataOrganizerForm : Form
{        
    //Default constructor
    public DataOrganizerForm()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    //Handle a Start/Stop button click
    private void start_stop_button_Click(object sender, EventArgs e)
    {
        SerialNumberSearcher snsearch = new SerialNumberSearcher();
        snsearch.searchSN(notification_textbox);
    }

    //Allow simple access to update to notification textbox
    public void setNotificationText(string text)
    {
        notification_textbox.Text = text;      
        notification_textbox.Update();      
    }
}

public class SerialNumberSearcher
{
    public void searchSN(Textbox notifyTextbox)
    {
        notifyTextbox.setNotificationText = "Updated text from different class"; 
        notifyTextbox.Update();
    }        
}