Strange problem with show/hide

J

Jimmy

Hey

A strange problem happens when i use show and hide. Form1 shows Form2 and
afterwards Form2 hides Form1. Form2 does stay in the foreground (before,
Form1 would stay in the foreground), but the title bar shows "Form1". When i
Close Form2, nothing happens. If i try to close it again i get an exception.
This makes sense, as i think that im actually closing Form1 at the first
click, wich puts Form2 in front. On the second click i try to show Form1
wich is disposed.

Now, isnt that strange?
This is a big problem for me!

I really hope you can help me. Why cant i hide Form1 properly?

Heres some code:

This is Form1:

private void dataGrid1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
try
{
DataGrid.HitTestInfo hitTestObj;
hitTestObj = dataGrid1.HitTest(e.X, e.Y);
if (hitTestObj.Type == DataGrid.HitTestType.RowHeader)
{
if ((int)dataGrid1[hitTestObj.Row,0] == 1)
MessageBox.Show("blablabla", "", MessageBoxButtons.OK,
MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
else
{
Form2 frm_2 = new Form2(this);
frm_2.Show();
}
}
}
catch (Exception f)
{
MessageBox.Show(f.ToString());
}
}

This is Form2:

private void Form2_Load(object sender, System.EventArgs e)
{
frm_1.Hide(); //This does hide Form1 but leaves the title bar with the
name of the Form1
}

private void frm_materialeDetail_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
frm_1.Show();
}
 
D

Dan

Rather than trying to hide/show windows in two different places, it would be
better practice to do it in one...

Form1:

// ....
Form2 frm_2 = new Form2(this);
frm_2.Show();
this.Hide();
// edit the title caption here to what you want it to say
// ...


still Form1:

//...
// when you're finished with Form2
this.Show();
frm_2.Hide();
//...


Finally, why not call

Form1:

// ....
Form2 frm_2 = new Form2(this);

this.Hide();
// edit the title caption here to what you want it to say
frm_2.ShowDialog(); // show's and hides the dialog
// ...
 
S

Serg Kuryata [MS]

Hello Jimmy,

Here are two things that I would recommend you to do:
1) Try to use the MouseUp event instead of the MouseDown. The OS can
perform some operations between the MouseDown and MouseUp events that could
cause the problems that you are seeing.
2) Use the ShowDialog() method instead of the Show() method to open the
second form. In this way Form2 will be modal and, as a result, there is no
need to hide/re-show the Form1 because it will be inaccessible until the
Form2 closes (when Form2 closes, the Form1 will be automatically made
visible and accessible by the OS).

Hope this helps.
Thank you
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| From: "Jimmy" <[email protected]>
| Subject: Strange problem with show/hide
| Date: Fri, 10 Sep 2004 16:00:11 +0200
| Lines: 65
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: 0x3ef37e0d.boanxx10.adsl.tele.dk 62.243.126.13
| Path:
cpmsftngxa10.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12
..phx.gbl
| Xref: cpmsftngxa10.phx.gbl
microsoft.public.dotnet.framework.compactframework:61014
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Hey
|
| A strange problem happens when i use show and hide. Form1 shows Form2 and
| afterwards Form2 hides Form1. Form2 does stay in the foreground (before,
| Form1 would stay in the foreground), but the title bar shows "Form1".
When i
| Close Form2, nothing happens. If i try to close it again i get an
exception.
| This makes sense, as i think that im actually closing Form1 at the first
| click, wich puts Form2 in front. On the second click i try to show Form1
| wich is disposed.
|
| Now, isnt that strange?
| This is a big problem for me!
|
| I really hope you can help me. Why cant i hide Form1 properly?
|
| Heres some code:
|
| This is Form1:
|
| private void dataGrid1_MouseDown(object sender,
| System.Windows.Forms.MouseEventArgs e)
| {
| try
| {
| DataGrid.HitTestInfo hitTestObj;
| hitTestObj = dataGrid1.HitTest(e.X, e.Y);
| if (hitTestObj.Type == DataGrid.HitTestType.RowHeader)
| {
| if ((int)dataGrid1[hitTestObj.Row,0] == 1)
| MessageBox.Show("blablabla", "", MessageBoxButtons.OK,
| MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
| else
| {
| Form2 frm_2 = new Form2(this);
| frm_2.Show();
| }
| }
| }
| catch (Exception f)
| {
| MessageBox.Show(f.ToString());
| }
| }
|
| This is Form2:
|
| private void Form2_Load(object sender, System.EventArgs e)
| {
| frm_1.Hide(); //This does hide Form1 but leaves the title bar with
the
| name of the Form1
| }
|
| private void frm_materialeDetail_Closing(object sender,
| System.ComponentModel.CancelEventArgs e)
| {
| frm_1.Show();
| }
|
|
| --
|
|
| Jimmy
|
|
|
 

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