bradcarman

I have a simple form that contains a WebBrowser control. Sometimes when clicking the minimize button, it can be seen that the form tries to minimize, but immediately returns to the original state. I have a feeling that is has something to do with the focus on the WebBrowser control, but I'm not sure how to fix or diagnose the problem. Any suggestions

Re: Windows Forms General Form Will Not Minimize Sometimes

bradcarman

By the way, I'm using VB 2005 Express and the program is running on Win XP Pro. As a side question. I'd also like to know how to remember the window state when the program is closed. For example, if a user has the program maximized when he closes it, it should then open maximized the next time he opens it.

Also, after playing with the program a little more, I've noticed that what is happening is that the form will minimized once, after the program has been started, but after that it will no longer minimize. I do not have any form events except for the Form_Load event. I also tested the program and commented out all of my code and still had the same problem. So I'm sure I do not have an event that is being called and causing this behavior. I must have a form setting wrong or something. Please help!!





Re: Windows Forms General Form Will Not Minimize Sometimes

nobugz

Restoring the window state is surprisingly tricky. There's lots of ways it won't work properly. Project + Properties, Settings tab, add the following settings:
- Location, System.Drawing.Point, User, [10, 10]
- Size, System.Drawing.Size, User, [300, 300]
- State, System.Windows.Forms.FormWindowState, User, [Normal]

Make your form's code look like this:

public partial class Form1 : Form {
private bool mInitialized;
public Form1() {
InitializeComponent();
this.Load += Form1_Load;
this.FormClosing += Form1_FormClosing;
this.Resize += Form1_Resize;
this.LocationChanged += Form1_LocationChanged;
}

private void Form1_Load(object sender, EventArgs e) {
this.StartPosition = FormStartPosition.Manual;
this.Location = Properties.Settings.Default.Location;
this.Size = Properties.Settings.Default.Size;
this.WindowState = Properties.Settings.Default.State;
mInitialized = true;
}

private void Form1_Resize(object sender, EventArgs e) {
if (mInitialized && this.WindowState != FormWindowState.Minimized) {
Properties.Settings.Default.Size = this.Size;
Properties.Settings.Default.State = this.WindowState;
}
}

private void Form1_LocationChanged(object sender, EventArgs e) {
if (mInitialized && this.WindowState != FormWindowState.Minimized)
Properties.Settings.Default.Location = this.Location;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
Properties.Settings.Default.Save();
}
}








Re: Windows Forms General Form Will Not Minimize Sometimes

bradcarman

Well I have figured out a couple things. I'm definitally sure now that the System.Windows.Forms.WebBrowser control is the cause of the problem. It appears that the actual website the control is navigated to is trying to set focus to a textbox or something, which I believe is making the form re-appear. So if the WebBrowser control is blank, hidden, or navigated to a website that is problem free, such as "google.com", then I do not have the problem. But if I am navigated to www.mcmaster.com then I do have the problem. I'm sure there are other websites that might cause similar behavior.

So to re-produce the problem, simply create a new WindowsForms project, add the WebBrowser control and set its URL to www.mcmaster.com . You will see that you will not be able to minimize the form more than once.





Re: Windows Forms General Form Will Not Minimize Sometimes

nobugz

I could reproduce your problem with that specific web site. There's some Javascript in the page but nothing really jumps out. Not sure what's going on. I came up with a workaround:

public partial class Form1 : Form {
private FormWindowState mLastState;
public Form1() {
InitializeComponent();
timer1.Enabled = false;
timer1.Interval = 1000;
timer1.Tick += Timer1_Tick;
mLastState = this.WindowState;
}
private void Timer1_Tick(object sender, EventArgs e) {
timer1.Enabled = false;
}
protected override void OnResize(EventArgs e) {
// Start restore timer if form got minimized
if (this.WindowState != mLastState && this.WindowState == FormWindowState.Minimized)
timer1.Enabled = true;
mLastState = this.WindowState;
base.OnResize(e);
}
protected override void WndProc(ref Message m) {
// Trap WM_QUERYOPEN and disable restore if the form was just recently minimized
if (m.Msg == 0x13 && timer1.Enabled) {
m.Result = IntPtr.Zero;
return;
}
base.WndProc(ref m);
}
}






Re: Windows Forms General Form Will Not Minimize Sometimes

bradcarman

Thanks so much for your reply. I tried this but could not get it to work. I may have not implemented it correctly because I'm converting your code to VB.net. I wasn't sure how to write 0x13 in VB, I tried &H13, but I'm not possitive this means the same thing. Anyway, I tried another trick based on your idea and got this to work:

Private Sub MainForm_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize

'Start restore timer if form got minimized

If Not (Me.WindowState = lastWinState) And Me.WindowState = FormWindowState.Minimized Then

Me.MainWebBrowser.Hide()

Me.MainTimer.Enabled = True

lastWinState = Me.WindowState

End If

If Not (Me.WindowState = lastWinState) And Me.MainTimer.Enabled Then

Me.WindowState = FormWindowState.Minimized

End If

If Me.WindowState = FormWindowState.Normal And Not Me.MainTimer.Enabled Then

Me.MainWebBrowser.Show()

lastWinState = FormWindowState.Normal

End If

End Sub