2

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

0

5 Answers 5

4

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.
Sign up to request clarification or add additional context in comments.

4 Comments

Ok it seems to be passing it to the function but its not displaying it in the textbox?
where are you calling the method from? I've edited with a new suggestion
It's not being called inside SettingsForm. Its being called from another .cs file.
then you need a reference to the instance of the SettingsForm that is actually being displayed. Can you post a bit more detail from the calling code? (What class is calling, what is its relation to SettingsForm)
3

You have to have an instance of a form object to do that, like this:

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

5 Comments

Using your code i get the error: Error 3 Cannot assign to 'send2Display' because it is a 'method group'
Sorry, C# is case sensitive, you have to capitalize the D in send2Display, updated my answer.
I also updated it again, saus's answer is correct too, you defined it as method and not a property.
Ok it seems to be passing it to the function but its not displaying it in the textbox?
You need to have the instance of the form that is displayed. If you just run the code above like its typed, it creates a new instance of the form. Somewhere in your code you must be doing a form.Show(), I'll update my example.
3
BluMote.SettingsForm.send2display = "test";

Should be:

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

But this creates a new instance, probably not what you want. You want to change the text on the currently displayed form, so you need to pass the instance the method needs to act on into the OpenPort method:

namespace PCComm
{
    class CommunicationManager
    {
    #region OpenPort
    public bool OpenPort(BluMote.SettingsForm form)
    {
        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
}
}

Then, somewhere in Form1 (like the load event), you'll want to instantiate the class dependent on it.

CommunicationManager comm = new CommunicationManager();
comm.OpenPort(this);

5 Comments

That would only work on a static method (which wouldn't work since he needs to access an instance variable (the rtbDisplay) on the form). He needs an instance of the SettingsForm class.
Ok it seems to be passing it to the function but its not displaying it in the textbox?
Saw it, don't understand it :o)
@tom: That doesn't seem to work either. Error 24 'System.Windows.Forms.Form' does not contain a definition for 'send2Display' when using Form.send2Display("test");. I even tried PCComm.frmMain.send2Display("test"); and i got the error Error 24 An object reference is required for the non-static field, method, or property 'PCComm.frmMain.send2Display(string)'
Can you paste the code from your SettingsForm where you instantiate the CommunicationManager class and pass in your instance?
1

There are few issues in your code.

  1. Calling a method like a property.

    BluMote.SettingsForm.send2display = "test"; // This is wrong

  2. Trying to access SettingsForm class members from another class like accessing static members.

First you have to parse SettingsForm instance to the 'Other Class'.

//In Other Class

private SettingsForm settingsForm;

// Get the instance as a parameter in Constructor (this is one of options)
public OtherClass(SettingsForm instanceOfSettingsForm)
{
    settingsForm = instanceOfSettingsForm;
}

//Now you can call send2Display method from OtherClass

settingsForm.send2Display("Test");

2 Comments

Method must have a return type?
Yes methods must have a return type. But constructors don't.
0

You might change the code below and see if it works:

public void send2Display(string whatWasSent)
{
     this.rtbDisplay.Text = whatWasSent;
     this.rtbDisplay.refresh();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.