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.

December 1, 2015

Format string with String.Format() method in C#

While programming you may need to build human understandable line(s) or para with dynamic values. In such cases, mostly developers either choose string concatenation or StringBuilder option in C# language.

Manipulating strings with these results in mutability and immutability. For detail, please read difference between string concatenation and StringBuilder. Is it ok for frequent use, or in case of looping, or for large strings manipulations?

Consider second case of formatting string with some calculated dynamic values, e.g.
  1. Decimal values to currency format,
  2. Splitting of date and time from DateTime value,
  3. Multiple string manipulation etc.

These may become tricky or lengthy, if you choose general programming concepts. Also, to display user friendly messages, always in demand.

With this article, we will understand how effectively we can format dynamic value with certain constant string line(s) or in paragraph, with less number of code lines. C# provided String.Format() method to do so. Let see few examples to understand it easily:

Example: 1


decimal d = 19.789M;
String.Format("The currency value is {0:C2}", d);

Output: The currency value is $19.79

// $ will return if you have chosen culture en-US, otherwise respective currency symbol will return.
Suffix ‘2’ with ‘C’ defines number of precision pint after decimal.
---------------------------------------------------------------------------------

Example: 2

String.Format("This message generated on {0:d} at {0:t}", DateTime.Now);

Output: This message generated on 12/1/2015 at 2:24 PM
---------------------------------------------------------------------------------

Example: 3

string name1 = "John", name2 = "Bob";
String.Format("Hello {0}, you have a message from {1}", name1, name2);

Output: Hello John, you have a message from Bob
---------------------------------------------------------------------------------


For detail about format standards, please visit https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx