1

The main form of my program has a button that opens a second form called PortOpener. PortOpener is setup to open serial, GPIB, ethernet, and USB ports to talk to external equipment. You select all the data required to open the port in the PortOpener and click the open port button. This passed all the port information to a new form called serialWindow and then serialWindow opens the port.

Here is where the serialWindow is created and how it passes the data to the new form.

    private void OpenPortButton_Click(object sender, EventArgs e)
    {
        SerialWindow _serialWindow = new SerialWindow();
        _serialWindow.Show();
        _serialWindow.OpenCom(cboPort.SelectedItem.ToString(), cboParity.SelectedItem.ToString(), cboStop.SelectedItem.ToString(), cboData.SelectedItem.ToString(), cboBaud.SelectedItem.ToString(), NamePortTextBox.Text);
    }

Here is the new serialWindow connecting to the port.

    public void OpenCom(string port, string parity, string stop, string data, string baud, string PortName)
    {
        comm.PortName = port;
        comm.Parity = parity;
        comm.StopBits = stop;
        comm.DataBits = data;
        comm.BaudRate = baud;
        comm.DisplayWindow = rxRichTextBox;
        comm.OpenPort();
    }

The port opens and I am able to transmit and receive data over the port. I can open multiple instances of the form by selecting different COM ports. I am trying to make it so the mainForm can pass data to the serialWindow to be transmitted.

I know how to pass data from the serialWindow to the mainForm using this method.
How to access a form control for another form?

I am unsure how to differentiate between the different instances of serialWindows. If the mainForm wants to transmit data over COM 3 and 6 COM ports are opened, how would you address each one? Should I try to send it to all of them and then have the serialWindows check if its opened COM port matches the one I want to transmit on?

portOpenerForm

serialWindowForm

Thanks for the help

Community
  • 1
  • 1
Laggmaster
  • 23
  • 4
  • Even if thinking only in forms might not be the best way, i would create a Dictionary in the mainForms class, which is keeping track of all opened ports and the corresponding serialWindow instances. Using keys like COM3 or USB1 will let you differentiate between the connections. Add the reference when connected and remove them on disconnecting. – Yosh Sep 02 '16 at 20:07
  • There are numerous approaches you can take. See the marked duplicate for several different options. If you are still unable to resolve this, post a new question in which you've included a good [mcve] that shows clearly what you've tried, along with a precise and detailed explanation of what the code does now, what you want it to do instead, and what _specific_ issue you are having accomplishing that goal. – Peter Duniho Sep 02 '16 at 20:54
  • 1
    Peter Duniho, not sure if you're trolling or not. If you read the whole question, you would see that I know how to pass data between 2 forms and provided an example link that you're claiming my question is a duplication of. What I wanted help with was talking to the correct form when multiple versions of that form was opened. Steve helped solve the answer with Application.OpenForm.OfType. – Laggmaster Sep 03 '16 at 01:02

1 Answers1

0

This is relatively simple knowing that all open forms are stored in the Application.OpenForms collection.

Add to your SerialWindow form a public property called (for example) PortName of type string

In SerialWindow code

public string PortName { get; set; }

now inside the method OpenCom set this property

public void OpenCom(string port, string parity, string stop, string data, string baud, string PortName)
{
    this.PortName = port;
    comm.PortName = port;
    ....
    comm.OpenPort();
}

At this point your mainForm is able to identify the specific SerialWindow looking at this public property

In mainForm you loop over the Application.OpenForms collection where all the form instances opened by your application are kept and find only the forms of type SerialWindow

foreach(SerialWindow frm in Application.OpenForms.OfType<SerialWindow>())
{ 
    // If this is a SerialWindow, then it has the property PortName
    if(frm.PortName == "COM4")
       ... pass your data to the frm instance
}

This of course could be adapted to any property that you want to check from you mainForm

Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    Application.OpenForms.OfType got the job done. Thanks for helping show how to differentiate between multiple forms of the same object. – Laggmaster Sep 03 '16 at 00:58
  • It is an act of courtesy explaining a downvote and an useful advice to future readers to avoid the bad answer – Steve Sep 05 '16 at 17:33
  • What down vote? I accepted you reply as the answer and up voted you for posting it. I do not have the reputation required for the up vote to show. – Laggmaster Sep 06 '16 at 16:40