Opening & Closing Forms

C

CSharp-Jay

I have a bit of a weird problem, I am currently a student learning
advanced C#. Today I decided to make a little program that tests my
knowledge while allowing me to learn new things and I hit a little
hiccup. Basically my initial form is named frmBackground. I've
programmed it to full truescreen mode. As soon as it is loaded, it
loads a child form called frmMain which stays on top of
frmBackground. The user from there can make a selection via a button
and when that selection is made, frmMain needs to close and the new
form which the user selected needs to pop up. The problem is that I
can get the new form to pop up but frmMain won't go away. If I close
the new form, THEN it goes away and I am left with the full screen
black background. I get the feeling that frmMain is still visible
although not loaded or something and it is hanging out there
completely dependent on the new form closing. Can somebody help with
this? I think I just need to know how a use can click a button in
frmMain, make frmMain unload itself AND load a new form. Here is the
code snip for frmBackground:

public partial class frmBackground : Form
{
frmMain main = new frmMain();

[DllImport("user32.dll")]
private static extern int FindWindow(string className, string
windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);

private const int SW_HIDE = 0;
private const int SW_SHOW = 1;

public frmBackground()
{
InitializeComponent();
}

private void frmBackground_Load(object sender, EventArgs e)
{
int hwnd = FindWindow("Shell_TrayWnd", "");
ShowWindow(hwnd, SW_HIDE);
main.Show();
}
 
J

Jeff Johnson

I think I just need to know how a use can click a button in
frmMain, make frmMain unload itself AND load a new form.

private void ShowFormXButton_Click(object sender, EventArgs e)
{
FormX newForm = new FormX();

newForm.Show();

this.Close();
}

Am I missing something...?
 
F

Family Tree Mike

Jeff said:
private void ShowFormXButton_Click(object sender, EventArgs e)
{
FormX newForm = new FormX();

newForm.Show();

this.Close();
}

Am I missing something...?

When the main form closes, the app closes. You can use this.Hide(). I
think there is a setting to close the app when the main form closes, or
when all forms close. I'm using express at home and cannot find the
setting that the moment.
 
P

Peter Duniho

Family said:
When the main form closes, the app closes. You can use this.Hide(). I
think there is a setting to close the app when the main form closes, or
when all forms close. I'm using express at home and cannot find the
setting that the moment.

There's no setting, but it can be accomplished by showing the initial
form explicitly and using the parameterless Application.Run() overload
(requires editing the Program.cs file).

Not that that appears to be the problem the OP is concerned about. His
post (admittedly vague) seems to be suggesting that he has some problem
with his full-screen window NOT being closed.

Unfortunately, he didn't post a concise-but-complete code example, nor
explain why he's using p/invoked functions to manipulate his window. I
think it will be difficult to know exactly what the expected and useful
answer is without some elaboration of the question.

Pete
 
F

Family Tree Mike

Peter said:
There's no setting, but it can be accomplished by showing the initial
form explicitly and using the parameterless Application.Run() overload
(requires editing the Program.cs file).


Ah, I found the setting, but it is in the VB application properties.
Not that that appears to be the problem the OP is concerned about. His
post (admittedly vague) seems to be suggesting that he has some problem
with his full-screen window NOT being closed.

Unfortunately, he didn't post a concise-but-complete code example, nor
explain why he's using p/invoked functions to manipulate his window. I
think it will be difficult to know exactly what the expected and useful
answer is without some elaboration of the question.

Pete

I was actually answering Jeff's post, but you are right on about the
original post.
 
J

Jeff Johnson

Not that that appears to be the problem the OP is concerned about. His
post (admittedly vague) seems to be suggesting that he has some problem
with his full-screen window NOT being closed.

I didn't get that at all from what the poster said:
The user from there can make a selection via a button and when that
selection
is made, frmMain needs to close and the new form which the user selected
needs to pop up. The problem is that I can get the new form to pop up but
frmMain won't go away.

He WANTS frmMain to close and does NOT want the background to close.
(Although if the child-child form opened by frmMain is closed and the user
is left with only the background form, I'm not sure how he's supposed to
proceed outside of hitting Alt+F4....)

But yes, the post is quite vague, with perhaps the most confusing piece
being this one:
I've programmed it to full truescreen mode.

"Truescreen" mode? Damned kids and their newfangled technology....
 
F

Family Tree Mike

Jeff Johnson said:
But frmMain is NOT the main form, according to the initial post:



.

Jeff,

Yep, you are right. I saw the frmMain and a button click, and assumed
frmMain was "the main form"...

Mike
 
J

Jeff Johnson

Yep, you are right. I saw the frmMain and a button click, and assumed
frmMain was "the main form"...

Yeah, in addition to vagueness the naming convention is horrible.

(Starting with the classic VB "frm..." naming! I'm a convert: bastardized
Hungarian must die.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top