Tuesday, December 15, 2009

To close a dialog by pressing esc key

If the dialog has a cancel button, when the esc key is pressed, the dialog will be closed. But if there is no ‘Cancel’ button, pressing esc key will not close the dialog. To make the dialog close when esc is pressed, add the following code in the handler for WM_COMMAND
switch (HIWORD(wParam))
{
case BN_CLICKED:
{
switch (LOWORD(wParam))
{
case IDCANCEL:
EndDialog( hDlg, nResult);
break;
}
}
}

Eventhough cancel button is not present, when esc key is pressed, WM_COMMAND message is sent to the dialog window with BN_CLICKED and ID IDCANCEL

For a standard dialog box,
Clicking on the close button sends a WM_SYSMESSAGE message with code SC_CLOSE to the dialog box
Hitting the ALT+F4 key combination performs the same effect, i.e. a WM_SYSMESSAGE message with SC_CLOSE
Hitting the ESC key simulates a click on the "Cancel", i.e. a WM_COMMAND message with BN_CLICKED and ID IDCANCEL

Enumerating Device Fonts from ppd file

The ppd file contains list of fonts resident in the printer. To list these fonts, we can use the EnumFontFamiliesEx function. Usually, EnumFontFamiliesEx will give the list of all the fonts in the system, and not those listed in ppd. It contains the TrueType fonts also. But to get an enumeration of fonts from the ppd file, use EnumFontFamiliesEx function, preceded by SetGraphicsMode( hDC, GM_ADVANCED).


HDC hDC = CreateDC( TEXT("WINSPOOL"), TEXT(""), NULL, NULL );

SetGraphicsMode(hDC, GM_ADVANCED);

EnumFontFamiliesEx(hDC, NULL, reinterpret_cast(&EnumFontsProc),
reinterpret_cast( &fontlist ), 0);

Disabling validation controls in asp.net

When using validation controls in ASP.NET pages, we might want to disable validation in certain situations. For example, if we don't want the validation controls to work in case of "cancel" or "log off", we can avoid validations in these cases.

The simplest method for this is to set the control's CausesValidation property to False. If done so, this control will no more cause the validators run.

asp:Button id="CancelButton" runat="server" Text="Cancel" CausesValidation="False" /)

Monday, December 14, 2009

Convert String to DateTime

[ Courtesy : www.high-flying.co.uk ]

Here we'll look at a way of converting a String into a DateTime object in C#. (.NET 2.0)

You may have a string value of a date, (for instance from a TextBox) and you need to convert that string into a DateTime (so you can compare dates for instance).

One way you can do this is to use the Convert.ToDateTime(string value). However, this isn't really the best way unless you can ensure the format of your string. For instance, if your string should end up containing invalid values such as 'Hello' then an exception will be thrown and we don't really want to cause exceptions. If you do use this approach, validate your string value before passing it to be converted. So use the following example with caution:


// take a string containing a datetime value
// ensure you have validated your string value as in a DateTime format
string _date = @"15/09/2008";

// attempt to convert, potential to cause an exception
DateTime _convertDate = Convert.ToDateTime(_date);


Another way to approach this is with the DateTime.TryParse(string _date, out DateTime _convertedDate) method. You need to create the _convertedDate DateTime object first, as it's value is created in the DateTime.TryParse and passed as as output parameter. If the attempt to parse the string into a datetime value fails, then the DateTime value will be set to DateTime.MinValue, and an exception will not be thrown in the application. So all you need to do is check that the DateTime has a value of greater than DateTime.MinValue, or use a boolean to carry out the DateTime.TryParse method and the boolean will return true or false, true for a successful conversion, or false for a failed conversion.


// create our objects
DateTime _startDate = DateTime.MinValue;
string _startDateString = @"21/03/2007";

// attempt the parsing
DateTime.TryParse(_startDateString, out _startDate);

// Check the parsing worked
if (_startDate > DateTime.MinValue)
{
// carry on actions
}


Or, as mentioned above you can use a boolean to assess the conversion:


// create our objects
DateTime _startDate = DateTime.MinValue;
string _startDateString = @"28/08/2011";

// attempt the parse and record success of conversion in a boolean
bool _didDateConvert = DateTime.TryParse(_startDateString, out _startDate);


Note how we use literal strings to store the date formatting in the string and prevent them acting as escape characters.

Difference between convert.ToDateTime and System.DateTime.Parse

Convert.ToDateTime uses DateTime.Parse internally, with the current
culture. But it handles null values as well. If null is passed, it returns DateTime.MinValue.


This is Convert.ToDateTime(string)

public static DateTime ToDateTime(string value)
{
if (value == null)
{
return new DateTime((long) 0);
}
return DateTime.Parse(value, CultureInfo.CurrentCulture);
}

And this is DateTime.Parse(string)

public static DateTime Parse(string s)
{
return DateTimeParse.Parse(s, DateTimeFormatInfo.CurrentInfo,
DateTimeStyles.None);
}


That calls


internal static DateTime Parse(string s, DateTimeFormatInfo dtfi,
DateTimeStyles styles)
{
DateTimeResult result1 = new DateTimeResult();
result1.Init();
if (!DateTimeParse.TryParse(s, dtfi, styles, ref result1))
{
throw DateTimeParse.GetDateTimeParseException(ref result1);
}
return result1.parsedDate;
}