Question

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.

Was it helpful?

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.

OTHER TIPS

public class Form2 : System.Windows.Forms.Form
{
    public System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();

    public void Main(string args[])
    {
        Form2 first = createform("1st instance", "some text");
        Form2 second = createform("2nd instance", "other text");
    }

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

    public void SetMessage(string message)
    {
        this.label1.Text = message;
    }
}

You could create a list and keep your forms in that, e.g.,

private List<Form2> forms = new List<Form2>();

public void createform(string who, string msg)
{    
    Form2 frm = new Form2();
    forms.Add(frm);
    // etc

Now you can access each form in the list:

foreach (Form2 form in forms)
{
    form.Text = "Hello world";
}

Use any collection to store and refer back your form instances

You need to keep a reference to it that you can use to "communicate" with them. Like private variables such as your strings, for example:

string priv_windows, who, msg;
List<Form> forms = new List<Form>();

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

public void ChangeChild()
{
    Form f = forms[0];
    f.Text = "New Form Title Text";
}

This is just a simple example that manipulates the first Form in the collection, if you had many you'd need a more sophisticated method of determining which retrieving which one you want to change.

I recommend using event aggregator to communicate with objects in your domain.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top