0

I have seen a few links on attempts at this but I haven't found a solution. I am attempting to access my form textbox and update it with text from another class. I can update the text within my DataOrganizerForm class directly but when I pass text back to the DataOrganizerForm class then it doesn't update on the GUI. Here is what I have:

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"); 
    }        
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Tyler Wilson
  • 99
  • 3
  • 11

3 Answers3

0

Well, it won't update the textbox 'cause you're instantiating another object of the DataOrganizerForm class. What you could do is passing a reference of the form object to the SerialNumberSearcher, like this:

public class SerialNumberSearcher
{
    private readonly DataOrganizerForm _form;  

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

    public void searchSN()
    {
        _form.setNotificationText("Updated text from different class"); 
    }  
}
lucacelenza
  • 1,259
  • 1
  • 15
  • 28
  • Thanks for helping point out that I was instantiating another object. I ended up passing the Textbox by reference and accessing it that way. – Tyler Wilson Dec 22 '16 at 14:56
0

You need to understand at which instance you operate. When you use the new-eperator you create a new instance, like a new copy of that type.

DataOrganizerForm formAccess = new DataOrganizerForm();

The original Form is a different instance then the one you create in the searchSN method.

You need to pass that instance into the method to manipulate it:

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

When you want to call this method you need to use this to reference the current object :

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

this will access the current instance of the Form, thereby allowing you to manipulate the textbox that you are interested in.

When do you use the “this” keyword? might also be helpfull

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
0

Thanks for the help. This is what I was able to do to make my application work. I passed the Textbox object by reference to my other class and was able to display my information that way. Also, I had issues getting my text box to continuously update. I had to add

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();
    }        
}
Tyler Wilson
  • 99
  • 3
  • 11
  • I am glad that you have found a solution for your problem. Usually the best way to say thank on StackOverflow is to mark the answer that helped you as accepted. Thereby increasing the reputation of yourself and that of the answerer. :) – Mong Zhu Dec 23 '16 at 13:40