views:

48

answers:

4

I am having a problem that is strange to me but hopefully is not so strange to someone else. : ) Some background: I am working on a simple IM client that allows the user to broadcast messages to multiple recipients. The goal is to create a chat form for each of the recipients containing the text of the broadcast message, then show that form only if the recipient responds to the broadcast-er. However, when the application receives a response then attempts to locate the form for that particular chat session (using Application.OpenForms) it cannot find it UNLESS I .Show at the time it is created. I would like to avoid having to show this form when it is created because this means that the user will see a flash on the screen. The form doesn't seem to really be created until I show it, but it would seem there has to be a way to do this without showing first. Can anyone assist?

I can provide code snippets if needed, I didn't in this post because this feels more like a conceptual misunderstanding on my part than a bug in the code. Thanks in advance!

A: 

windows form has methods like Hide(),Show() and Activate(). use these method for your problem.

Krishna
A: 

Why not store a reference to the form with the chat session and use that to call .Show() when you need to display the form:

session.form.Show();

You can then create the form without showing it and you don't have the overhead of calling Application.OpenForms each time you want to reference it.

I know this is stating the obvious but OpenForms won't find a form that hasn't been shown because it's not open.

ChrisF
A: 

As the form handle does not get created until the form is shown you can assign it manually like so:

mf = new MainForm();

        /* Need to assign a handle to MainForm instance manually
         as handle does not get created until form is shown */
        IntPtr handle = mf.Handle; 
Longball27
+1  A: 

Instead of using the form as a base class, do it the other way, create a class that can reference a form. That way, you'll keep the class informed of the content, and reflect it on the form (if it's initialized), not the other way around. You shouldn't rely on Forms as a basis of your objects. Using Application.OpenForms should be unnecessary.

public class Contact
{
    string displayname = String.Empty;
    List<Message> history = new List<Message>();
    MessageForm theform = new MessageForm(this);

    public void OnEvent(Message msg)
    {
        if(msg.Sender != me && !theform.Visible)
            theform.Show();

    }

    public void Tell(string message)
    {
    }

}

etc

Keep your contacts in some sort of list, and things should be relatively simple. (Be aware that windows forms aren't thread-safe, and will throw an exception if you try to alter any properties of any of the controls from a different thread than main)

Jarle Moe
Thanks to everyone for your comments. @Jarle Moe, this seems like the suggestion that will be easiest to integrate into the existing code, I will go ahead with it. Since my questions have been answered (I was doing it wrong) I believe this one can be closed.
awilson53