December 15, 2015

Scroll bar disappears on minimizing of window - C# .NET winform

Introduction

While working on windows based project, I found a strange issue - on minimizing of window to task bar and returning to its normal state by maximizing, some of its controls and scroll bar were disappearing. Those get displayed automatically on changing of window’s state or re-sizing of the form.

I tried lots to find out its solution over internet but I did not found any good solution, what I found is that this is a .NET bug which has been fixed in framework 4.5, whereas I was using .NET framework 3.5, and therefore looking for alternate solution. For detail, you can visit:

http://www.codeproject.com/Questions/124373/scrollbar-disappears-while-resizing-the-window


Solution


While discussing with my senior, I found a hint about to override base class method OnSizeChanged(). After fighting with code lines while RnD and using Visual Studio class editor help, I found the solution what exactly I was looking, and the solution is given below:


protected override void OnSizeChanged(EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized || 
this.WindowState == FormWindowState.Normal)
       {
              base.OnSizeChanged(e);
       }
       if (this.WindowState == FormWindowState.Minimized)
       {
              this.ctrlId.ResumeLayout();
       }

}



Understanding the code

Override base class method OnSizeChanged() in your form class to restore form state, on conditional basis. For window’s minimize state, call ResumeLayout() method to resume controls layout as well its data. For window’s maximize and normal state, call base class method OnSizeChanged() to refresh the form controls.


Remark

In my case, ‘Dock’ property could not resolve my issue. This solution is not a rocket science but as this issues I have faced was strange and therefore, I wish to share it, someone my take help from this.