Reputation: 59
I have a C# application that opens up two GUIs at startup. One of the GUI opens three virtual COM Ports. I want the other GUI that is a part of the same application to show the data that is being sent on those virtual COM Ports. Is there anyway to share this COM Port declaration that I do in the Main/First GUI that I am opening up.
Upvotes: 0
Views: 721
Reputation: 461
Three ways:
1) Copy the instance of the connection from Window1 to Window2.
window2.Connection = this.Connection;
2) Make it the connection a public static member
class Window1 : Window
{
public static SerialConnectionClass Connection { get; set; } = new SerialConnectionClass();
}
You can access the connection like that:
class Window2 :Window
{
...
private void func()
{
Window1.Connection.Send("");
}
}
3) Make a static ConnectionManager
static class ConnectionManager
{
public static SerialConnectionClass Connection { get; set; } = new SerialConnectionClass();
public static EventHandler MessageReceived;
public static void Send(string text)
{
Connection.Send(text);
}
...
}
Use it in Window1 and Window2 like this:
class Window2 :Window
{
...
private void func()
{
ConnectionManager.init("COM1");
ConnectionManager.MessageReceived += this.MessageReceived;
ConnectionManager.Send("test123");
}
}
Upvotes: 1