Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Opening & Closing Forms

0 views
Skip to first unread message

CSharp-Jay

unread,
Nov 19, 2009, 3:10:43 PM11/19/09
to
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();
}

Jeff Johnson

unread,
Nov 19, 2009, 3:57:24 PM11/19/09
to
"CSharp-Jay" <bluem...@gmail.com> wrote in message
news:c2430260-ee4e-463f...@m16g2000yqc.googlegroups.com...

> 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...?


Family Tree Mike

unread,
Nov 19, 2009, 7:18:01 PM11/19/09
to

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.

--
Mike

Peter Duniho

unread,
Nov 19, 2009, 7:25:56 PM11/19/09
to
Family Tree Mike wrote:
> 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

Family Tree Mike

unread,
Nov 19, 2009, 9:41:28 PM11/19/09
to
Peter Duniho wrote:
> Family Tree Mike wrote:
>> 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).


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.

--
Mike

Peter Duniho

unread,
Nov 19, 2009, 11:59:42 PM11/19/09
to
Family Tree Mike wrote:
> Ah, I found the setting, but it is in the VB application properties.

Sorry...my C# projects don't have that setting.

;)

Jeff Johnson

unread,
Nov 20, 2009, 9:33:20 AM11/20/09
to
"Peter Duniho" <no.pet...@no.nwlink.spam.com> wrote in message
news:e0W9HgXa...@TK2MSFTNGP06.phx.gbl...

> 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....


Jeff Johnson

unread,
Nov 20, 2009, 9:33:27 AM11/20/09
to
"Family Tree Mike" <FamilyT...@ThisOldHouse.com> wrote in message
news:%23zsqrbX...@TK2MSFTNGP02.phx.gbl...

>>> 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...?
>
> When the main form closes, the app closes.

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

Family Tree Mike

unread,
Nov 20, 2009, 12:23:01 PM11/20/09
to

"Jeff Johnson" wrote:

> .
>

Jeff,

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

Mike

Jeff Johnson

unread,
Nov 20, 2009, 1:36:21 PM11/20/09
to
"Family Tree Mike" <FamilyT...@discussions.microsoft.com> wrote in
message news:DA485487-B36D-400F...@microsoft.com...

> 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.)


0 new messages