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

April 20, 2015

Nuget error: The underlying connection was closed

ERROR: Visual Studio 2010 nuget error: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.


Solution:
Go to VS2010 -> Tools -> Library Package Manager -> Package Manager Settings
Choose Package Manager -> Package Sources.
1. Add a new package source as:
Name= NugetSource
Source= http://packages.nuget.org/v1/FeedService.svc/
2. Move Up the newly added package source to first position.
3. UnCheck existing "Nuget official package source"
4. Restart VS2010.

March 16, 2015

Read HTML table using JavaScript or jQuery

Read complete HTML table- rows & columns values using JavaScript/jQury. To understand this article, you should know about basics of HTML & JavaScript.

Read HTML table by its id attribute:
var table = document.getElementById('table_id');

Read rows count in the HTML table:
var row_count = table.rows.length;

Read columns count in the HTML table:
var col_count = table.rows[i].cells.length;


You can iterate to read all column values including headers:

for (var i = 0; i < table.rows.length; i++) 
{
var colmns= table.rows[i].cells;

for (var j = 0; j < colmns.length; j++) 
{
if (i == 0)
// Read column-header values:
table.rows[i].cells[j].children[0].innerText;
else
// Read column-rows values:
table.rows[i].cells[j].innerText;
}
}

.innerHtml - returns HTML element within the specified cell.


.innerText - returns text value within the specified cell.

January 15, 2015

Eliminate dead codes to avoid extra code maintenance

Dead/unreachable codes and member variables:

While working with large projects, silly mistakes are common and those may cause memory or performance issues. Developers used to write method and later some methods either they change or comment. This generally happens in case of frequent requirement change. If you have commented the method and function call then there is –“no comment”. But in case you have commented method call and you haven’t commented method definition, or method defined but never used, or variables declared but never used, what about the memory occupied by instance of such class? These codes called as dead codes or in general unreachable codes. Generally, Visual Studio does provide blue under line for unused variables but they have no such facility to unused methods.

The problem with these are- more about that you have to care and maintain extra code that is not in use. Also typically when you are about to delete an unused code elements, this might provoke asking questions which lead to interesting answer and even bug finding. Is it simple to handle all these for large projects where hundreds of classes designed and thousands of lines coded? NDepend provides to eliminate such issues within a minute.



Here, I have defined two variables and one method but never used those. And these we can call it as dead codes. NDepend installed Visual Studio provides a red bubble on bottom right, on click to this a window will popup which lists out all dead codes.




Or, in alternative way: