Reputation: 10552
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
Upvotes: 2
Views: 2447
Reputation: 10452
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);
Upvotes: 3
Reputation: 2174
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:
Upvotes: 4
Reputation: 47600
There are few issues in your code.
Calling a method like a property.
BluMote.SettingsForm.send2display = "test"; // This is wrong
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");
Upvotes: 1
Reputation: 4340
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");
Upvotes: 3
Reputation: 16287
You might change the code below and see if it works:
public void send2Display(string whatWasSent)
{
this.rtbDisplay.Text = whatWasSent;
this.rtbDisplay.refresh();
}
Upvotes: 0