Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys.
I'm new to .net and windows forms. I have created a window that creates a main form and then It should create a modal dialog over it displaying some information and input some data. The dialog should be created without any physical event, just after the main form is displayed. I tried to create dialog in Shown event of main form, but this is not the right way because it blocks the main forms Shown event. In win32 api, I should simply create the dialog in a separate thread or post a user message to the window procedure of main form. How can I do this in windows forms? I don't want to use unsafe code.
Thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 18-Aug-15 17:03pm    
Why, why "blocked"? Why another thread? Do you understand that even-oriented UI is designed to work with all forms (windows) and controls (UI elements) at the same time, even in one thread? Do you understand why?
—SA
mr.abzadeh 19-Aug-15 1:54am    
blocked because the added Shown event doesn't return until form2 is closed. I wanna use another thread. I spoke of thread because of my background of Win32 API programming. and as I mentioned, I'm new to .NET and windows forms. My question is this: At which point should I call form2.ShowDialog()? without causing the main form events suspend?
Ralf Meier 19-Aug-15 0:20am    
Do you want to show it every time the Main-Form is displayed ?
Or do you want to create a "Splash-Window" which is shown once when the application is startet ?
mr.abzadeh 19-Aug-15 1:57am    
I want to show the dialog only at startup. The dialog is modal, needs user attention. no splash window.
Ralf Meier 19-Aug-15 6:51am    
I can't see the Difference in the Moment - but it's OK ...
I see different possibilities for a Solution. One could be the posted Soultions.
Another could be to start you MessageForm first (Project-Settings), set it Topmost and then start your MainForm (from the OnShown-method of the MessageForm). Only a (Button-)Action on your StartForm could hide it. My Suggestion will work only once at Application-Start. The posted Soultions will Show your MessageForm each time the MainForm is shown again. You must decide ...

First of all, the design you are trying to implement is pretty ugly, but I could only advise you something better if I knew your ultimate design goals. But the problem is simple; you don't need any additional thread, no unsafe code nothing. Your suggestion to use Form.Shown is correct, and your idea that it can block anything is just a misconception. But perhaps you don't know what a dialog is.

A custom dialog is the same as the form, only it is shown using Form.ShowDialog. Then, during the modal state of it, you cannot select any other form of the same application. If you do need to access other forms, it should be Show instead. The simplest code I could think of would be all in the entry-point method, provided you already have two form types. It can look like this:
C#
using System;
using System.Windows.Forms;

//...

[STAThread]
static void Main() {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    var mainForm = new FormMain();
    mainForm.Shown += (sender, eventArgs) => {
        new FormDialog().ShowDialog();
    };
    Application.Run(mainForm);
}


—SA
 
Share this answer
 
Comments
mr.abzadeh 19-Aug-15 1:45am    
My code is exactly as above. By blocking I mean that the Shown event I have added to main form returns after foorm2.ShowDialog() returns, and calling some main form events is called then, which is not a proper way. One effect of this design is the main form flickers after form2 is closed.
Sergey Alexandrovich Kryukov 19-Aug-15 1:59am    
You did not "add event", you added an event handler to the invocation list of the event instance. Yes, your handler will be blocked until ShowDialog returns, so what? Is your "not a proper way" a conceptual concern, or you got some behavior you did not expect? What flicker? Generally, to avoid "blocking", you can use Control.BeginInvoke, then you can launch a blocking call, but you don't need it in this case. You just need to understand what is the modal behavior.
—SA
mr.abzadeh 19-Aug-15 7:32am    
Yes, I wasn't exact. It's event handler. By calling form2.ShowDialog() The only problem is that the main window have some shakes when form2 is closed.
And it seems bad programming to postpone the handling the Shown event and the events after it. I'm new to windows forms, and I don't know if it's a bad idea to show a modal dialog after a form is created? If so, what is the alternative?
Sergey Alexandrovich Kryukov 19-Aug-15 9:12am    
I think this "bad programming" is nothing but your misconception. After all, everything happens as a result of calling event handler. Do you know any other way of calling Form.ShowDialog? :-)
—SA
mr.abzadeh 19-Aug-15 18:12pm    
In some situations it is needed. Surely windows forms has a way to call form.ShowDialog() straightly. One way is to have a command1 in menu to click to call Showform.Dialog(), If it's possible to mainform to request the menu to click command1, some thing link win32 PostMessage.
But I'm not familiar enough to do this in .net
C#
private void Form1_Shown(object sender, EventArgs e)
       {
           Form2 foo = new Form2();
           foo.Show();
       }


This is non blocking and both forms will be shown at runtime.
 
Share this answer
 
Comments
mr.abzadeh 19-Aug-15 18:14pm    
It's a good idea. Some code in form.Show will be changed because of syncronization between mainform and form.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900