[.net] Super easy n00b problem, x2 forms Show()

This topic is 4232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi guys, i think im just having a moment going back to lowerlevel stuff but: I've just writen some multihead directX stuff, it seems to be working the only problem is im calling from.show(); from my class but the forms are being closed as soon as they are diplayed, i can't use ShowDialog(); because that seems to pause the code untill that form is closed, is there any way do keep the form open using Show(); by adding an eventhandler or something?, the forms are not actualy created there Form from1 = new Form; thanks guys, im sure im just missing something now, im on the edge of a breakthrough :D [Edited by - blackpenny15 on November 15, 2006 6:19:12 PM]

Share on other sites
You would normally (well, if you follow the default template) keep an application alive by calling Application.Run(Form), which keeps the application running until that form is closed. So,

Form Form1 = new Form();Form Form2 = new Form();Form2.Show();Application.Run(Form1); // Shows Form1// Will only exit when Form1 is closed

I'd probably make it so that Form1 creates and displays Form2 itself, so that it can detect when Form2 is being closed (and so close itself too).

Share on other sites
but that still pauses the code right, it will still stop at application.run and wait for that to finish

Share on other sites
Form1's code will now be running. I'm not really sure what you're up to, but I suppose you could do something like this in your Main

Form Form1 = new Form(); Form1.Show();Form Form2 = new Form(); Form2.Show();while (Form1.Visible && Form2.Visible) {    // Main render loop    Thread.Sleep(0);}// You'll only get here when either form is closed

Share on other sites
ok i'll try explain what i was doing, i;ve wrote some multihead code in diretx, it works when the code is in a form, what i wanted to do was make my class create two forms and render to them, the problem is using Show(); works but the forms close super quick, and ShowDialog(); pauses the code.

If i multithred the app, were talking cross thredcalls and i've had errors regarding that already so i don't really want to look at multithreading yet.

I've got my code working with the cool Tom Miller loop, but all my init code is inside form1, is that ok, i thought best practive was to do all this stuff with classes and just pass the class a form as a render target.

I wrote an engine that does exactly that, the problem is in my main loop, diplaying one form with application.run is ok, its just displaying two without multithreading is kinda hard, am i even making sence anymore?

seriously thanks for working though this issue with me

si

Share on other sites
If you .Show() a form it will not mysteriously close by itself. I'm guessing that you don't have something keeping the application alive, and it's exiting and thus closing the forms for you. That's a guess though. I'd have to see the code for creating and showing the forms to work out where exactly the problem is.

Safe cross-thread calls are relatively painless if you work through the documentation's suggested method. How to: Make Thread-Safe Calls to Windows Forms Controls.

Share on other sites
Yes thats exactly the problem, i only want the forms as canvases for DirectX, they don't need any code, they just need to sit there, thats it, is there a event handler i can add or something? to make them just sit there, as i said i've moved all the game code from the class into from one and have form one open form two, no problem, but this isnt the ideal solution.

Share on other sites
Why not do what benryves suggested earlier, but wrap your rendering code into a class:

Form Form1 = new Form();Form Form2 = new Form();RenderClass render = new RenderClass();Form1.Show();Form2.Show();//hook the forms to your render code hererender.Start();Application.Run();

In each form's Closing event do:

if (Application.OpenForms.Count == 1)  //last form is closing   Application.Exit();

Share on other sites
I believe what you're differentiating between is modal and modeless forms. When you use the .ShowDialog() you are creating a modal form, which means that you cannot access the rest of the application until that form has been handled. When you use .Show(), it's modeless so control can return to your other instantiated forms.

In terms of the initial form, Application.Run is mandatory as far as I know.

• 18
• 11
• 16
• 9
• 49
• Forum Statistics

• Total Topics
631395
• Total Posts
2999780
×