Hi Bob:
I'm not sure what else is happening, but Refresh isn't necessary to show a
form. I created a two form app, with Form1 and Form2 where Form1 is my
startup object. I have buttons on both forms. Form1 has a button with text
Form1Button and Form2 has Form2Button. Behind form1's button I have this
code, it's identical to yours
Form2 _formMain = new Form2();
_formMain.Show();
This shows Form2 just as expected. Notice that I declared and instantiated
_formMain in the block.
Here's what I can get to happen. I call the block you use with the Hide
method called. Then I show Form2 and I close form2. Form1 is no longer
visible. If I don't call Hide, then Form2 will show, and if I close it,
Form1 will be visible. I'm wondering if you don't have another Hide
somewhere referencing the form you are expecting to see. Also, unles you
have something removing the event handlers, they should be firing. How have
you verified that they aren't? If you put a messagebox or a breakpoint
behind the two buttons, they aren't firing at all?
This looks like a log-in screen of some sort and I'm wondering if some other
code isn't causing something to disappear. Also, if you set _formMain as the
startup object, will the buttons work then? I just tried this same snippet
with the startup form hidden after I showed it and it definitely doesn't
behave in any way that you'd want.
What form is the startup object?
All in all, I'd get rid of the hide call and just show the login form from
my startup form. Then I'd return a DialogResult or some enum of my own
indicating that thing was authenticated correctly or not. Then, depending
on the return value, I'd either continue in the app or tell the user they
failed and either ask them to try again or close the app. Here's a sample
of something I've used before:
Logon f = new Logon();
if(f.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("You must enter a password!", "Password Error!");
Application.Exit();
}
else
{
f.Dispose();
fp.Show();
}
}
else
{
fp.Show();
}
HTH,
Bill
Bob said:
I am trying to show a form from an event handler of another form. As in the following:
private void _serverSocket_OnLoginAccepted(object sender)
{
_formMain = new frmMain();
_formMain.Show();
_formMain.Refresh();
this.Hide();
}
2 questions:
1) If I omit the refresh method, the _formMain instance does not show. Why??
2) Assuming the Refresh() method is required, none of the events of the
_formMain instance controls are firing. I have two buttons on the _formMain
form, but the Click event is not being fired. Why?