Form controls not responding after refresh

G

Guest

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

William Ryan eMVP

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

Guest

Thanks for your reply. Well, I'm only using Refresh because the Show method does not show the form at all unless the Refresh method is called. Could it be that the event handler must complete before giving focus to another form?
 
W

William Ryan eMVP

Bob:

Something else is causing that. You can just call .Show once you've
instantiate an instance of a form and it will show . The code I posted is
the actual stuff I used and it works. As far as waiting for the event
handler to complete..that's not it. I put messageboxes for instance right
after show to verify the properties and the form will show, then the
messagebox pops up which gives the impression of the second form calling the
messagebox.

I'm just wondering though, you have a main form, then you call the Show of
the second form and that's not working? Is anything else going on that you
can think of? You don't even need to 'hide' the first form, as soon as you
show the new form, it will cover up the old one. Then when the second one
closes, the first one will 'appear' again. All Refresh does is cause an
invalidate on the form's visible/client area and then repaint itself and
whatever controls it may contain. The only difference between these two
fragments is that if I call Hide for the first form, when I close form2,
Form1 isn't visible anymore (I'm looking at the today screen). Otherwise, as
far as showing the second form, they are identical (well one says "test" and
the other says "not a test" but that's it).
Form2 _formMain ;

private void button1_Click(object sender, System.EventArgs e){

_formMain = new Form2();

_formMain.TestProperty = "test";

_formMain.Hide();

_formMain.Show();

MessageBox.Show(_formMain.TestProperty); }

private void button2_Click(object sender, System.EventArgs e){

_formMain = new Form2();

_formMain.TestProperty = "Not a test";

_formMain.Show();

_formMain.Refresh();

MessageBox.Show(_formMain.TestProperty);

this.Hide();

}


Can you post the code ?
Bob said:
Thanks for your reply. Well, I'm only using Refresh because the Show
method does not show the form at all unless the Refresh method is called.
Could it be that the event handler must complete before giving focus to
another form?
 
A

Alex Feinman [MVP]

It looks like you are calling UI-related methods inside a socket callback.
You need to use Form.Invoke to perform your UI operations (refresh, show) in
the STAThread context.

--
Alex Feinman
---
Coming to MDC? make sure to stop by the session CLI345
on Friday for OpenNETCF talk

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?
 

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