Search code examples
c#windowswinformsmultiple-forms

Accessing new form instances c#


I am writing sort of instant messanger application in C#, that works on sockets. I managed all socket problems and it works like an IRC now. However, i want to implement private message system. We have 2 forms - one is main, second one is a prototype of like private msg. window should look like. And here is my problem:

string priv_windows, who, msg;

...

public void createform(string who, string msg)
{
Form2 frm = new Form2();
frm.Text = who;
frm.label1.Text = msg;
priv_windows += who += " ";
frm.Show();
}

...

createform("1st instance", "some text");
createform("2nd instance", "other text");

This works just fine for creating new instances of form, however i can't find a way to communicate with these child forms, after creating them. I mean, how can i for example change text on first form instance ? Is there an easy way, to set an index or something to a specific form instance ? Please, describe shortly how to use it later.


Solution

  • If I interpret the parameter who of void CreateForm(String, String) right, you want to have one private window per active conversation with a certain person.

    If this is right i would use the class Dictionary (see MSDN for details) instead of List to hold a reference to the the conversation window.

    Another good thing, would be to implement a a form controller class, which has actually static members to manage all used forms. This would allow you to access windows form anywhere in your code. If you do not want that every part of your code has access to it you can control the accessibility by using namespace.