Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# referencing textbox from another class

new at C# so be nice...

I am trying to send some text to a form textbox using the following code:

SettingsForm.cs

 namespace BluMote
 {
      public partial class SettingsForm : Form
      {
           public void send2Display(string whatWasSent)
           {
               this.rtbDisplay.Text = whatWasSent;
           }

           private void cmdOpen_Click(object sender, EventArgs e)
           {
               commToy.Parity = "None";
               commToy.StopBits = "One";
               commToy.DataBits = "8";
               commToy.BaudRate = "115000";
               commToy.PortName = "COM4";
               commToy.OpenPort();
           }
      .........
      }
 }

And i am (trying) calling it from another class like so:

namespace PCComm
{
    class CommunicationManager
    {
    #region OpenPort
    public bool OpenPort()
    {
        try
        {
            if (comPort.IsOpen == true) comPort.Close();
            comPort.BaudRate = int.Parse(_baudRate);
            comPort.DataBits = int.Parse(_dataBits);
            comPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), _stopBits);
            comPort.Parity = (Parity)Enum.Parse(typeof(Parity), _parity);
            comPort.PortName = _portName;
            comPort.Open();
            PCComm.frmMain form = new PCComm.frmMain();
            form.send2Display("test");
            return true;
        }
        catch (Exception ex)
        {
            DisplayData(MessageType.Error, ex.Message);
            return false;
        }
    }
    #endregion
}
}

And "test" does not display in the textbox field

But as you can see, its not working... What am i missing?

David

like image 600
StealthRT Avatar asked May 25 '26 04:05

StealthRT


1 Answers

send2Display is a method, you need to call it with a parameter, not assign to it.

BluMote.SettingsForm form = new BluMote.SettingsForm();
form.send2Display("test");

EDIT:

If you are calling the method from inside the SettingsForm class, then you don't need to create a new instance. Try:

this.send2Display("test");

EDIT Based on updated question:

The problem is that the form that you are creating in OpenPort() is not the one that is displayed on screen, so any updates to the textbox won't show on screen. Here are a few quick and dirty ways to remedy this:

  • Pass a reference to the textbox into your method. I don't recommend this approach, because you will end up with view dependencies in your model.
  • Return a string from OpenPort() and pass the return value to sendToDisplay.
  • Define a property LastMessage of type string in CommunicationManager and assign to it in OpenPort(). Then read from it in SettingsForm and pass its value to sendToDisplay.
like image 125
saus Avatar answered May 28 '26 08:05

saus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!