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
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With