C#

Controls Enable Disable Refactoring

How many times we have written such code to enable/disable controls, based on some conditions:

if (string.IsNullOrEmpty(textBoxFind.Text))
{
  buttonFindNext.Enabled = false;
  buttonReplace.Enabled = false;
  buttonReplaceAll.Enabled = false;
}
else
{
  buttonFindNext.Enabled = true;
  if (string.IsNullOrEmpty(textBoxReplace.Text))
  {
    buttonReplace.Enabled = false;
    buttonReplaceAll.Enabled = false;
  }
  else
  {
    buttonReplace.Enabled = true;
    buttonReplaceAll.Enabled = true;
  }
}

The power of refactoring?

Check out this piece of code taken from CodeProject.

bool son;

if (mShowShadow == false)
  son = false;
else
{
  if (DropShadowSupported() == false)
    son = false;
  else
  {
    son = true;
  }
}

if (son)
{
  parameters.ClassStyle += CS_DROPSHADOW;
}

The code doesn't made any sense to me, so I refactored and guess what the outcome:

if (mShowShadow && DropShadowSupported())

XDocument can't load a simple xml with version 1.1

using System;
using System.Xml.Linq;

class Test
{
  static void Main(string[] args)
  {
    string xml = "<?xml version=\"1.1\" ?><root><sub /></root>";
    XDocument doc = XDocument.Parse(xml);
    Console.WriteLine(doc);
  }
}

Unhandled Exception: System.Xml.XmlException: Version number '1.1' is invalid. Line 1, position 16.
  at System.Xml.XmlTextReaderImpl.Throw(Exception e)
  at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
  at System.Xml.XmlTextReaderImpl.ParseXmlDeclaration(Boolean isTextDecl)

Detect & Install .NET 3.5 Framework using Inno Setup.

In this article, we would see how to detect whether the .NET Framework is present or not, and how to install the .NET Framework automatically from the internet (Microsoft Website), if its already not present. The installation won't continue, until the .NET Framework is installed.