Archives

Archives / 2009 / July
  • Assigning Multiple FontStyle to a single instance of font class in Dot net Windows Application

    Hi,

    While working with Textbox in a windows application I wanted to show the text of the textbox to be both Bold and Underlined. I had to do this based on certain condition.

    I was surprised to see that the bold and underlined properties in the font class are readonly. What this means is that we can only set these properties in the constructor of the class and there is no other way to set the property.

    In the constructor we can only send one type of fontstyle. This meant there is no way to set up multiple font style (Bold font style and Underline font Style are two different styles) in the font.

    After googling a bit I found that we can send more than one fontstyle in the constructor of the font class with the help of “OR” keyword. Below is an example of how to provide multiple fontstyle in the font.

    Font ft = new Font("Microsoft Sans Serif", 8, FontStyle.Regular | FontStyle.Bold);

    Vikram

  • Setting and retrieving the default printer from code

    Hi,

    There came a requirement in one of my colleague’s project whereby it was needed to set the printer to be used for the lotus notes (The project is about integrating with Lotus Notes.).  

    To solve the issue instead of looking into the Lotus notes API I thought it should be much easy to set the default printer for the machine. Once the Default printer is set Lotus notes will use this printer to print any document.  Below is the code needed to set the default printer of any machine.

    [DllImport("winspool.drv", SetLastError = true)]
    private static extern void SetDefaultPrinter(string name);

    To use the code, you need to import the System.Runtime.InteropServices namespace as below.

    using System.Runtime.InteropServices;
    You might also require to find the default printer in the machine. For that the code is given below.

    System.Drawing.Printing.PrinterSettings oPS = new System.Drawing.Printing.PrinterSettings();
    functionReturnValue = oPS.PrinterName;
    Vikram
    P.S. Thanks to Anindita for her Help with Code

  • Path.combine to combine two paths and backslash in second parameter

    Hi,

    system.IO.Path comes with many good function to work with the File system. some of them are ChangeExtension, GetDirectoryName, GetExtension, GetFileNameWithoutExtension, IsPathRooted etc.

    The system.IO.Path.Combine is also include as a function in system.IO.Path to combine two paths provided to it. A few days back I was working with it to add paths provided by user. But I started getting erros where by sometimes the path would not be combined and only the second provided path was returned. After doing some research on this I got to the root of the cause.

    The problem was only occuring when the second path provided in the method was starting with "\".

    To make my understanding more accurate I made a small console applictaion to show what would be the result in differenct scenarios. Below are the situation and the output I got for them.

    Console.WriteLine(System.IO.Path.Combine(@"D:\V\", @"S\test1.doc"));
    Console Output - D:\V\S\test1.doc

    Console.WriteLine(System.IO.Path.Combine(@"D:\V\", @"\S\test1.doc"));
    Console Output - \S\test1.doc

    Console.WriteLine(System.IO.Path.Combine(@"D:\V", @"S\test1.doc"));
    Console Output - D:\V\S\test1.doc

    Console.WriteLine(System.IO.Path.Combine(@"D:\V", @"\S\test1.doc"));
    Console Output - \S\test1.doc

    As we can see from the above results whenever the second path starts with "\" the result only includes the first path.

    Hence we should be cautions when we are using the Path.Combine function to combine two function.

    Vikram