Find child control of a control in WPF

Sometime we want to get reference of child control of container control of specified type. Example we want to find CheckBox control inside ListViewItem or we want to find another container Panel inside a Grid.

Here is a method through which we can find child control inside a parent control. The method below will return the first matching control of the specified type (T), if control of specified type is not found in the provided parent control it will return null.

/// <summary>
/// Method to get child control of specified type
/// </summary>
/// <typeparam name="T">Type of child control queried</typeparam>
/// <param name="parent">Reference of parent control in which child control resides</param>
/// <returns>Returns reference of child control of specified type (T) if found, otherwise it will return null.</returns>

private static T FindVisualChild<T>(DependencyObject parent)
where T : DependencyObject
{
     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
     {
          DependencyObject child = VisualTreeHelper.GetChild(parent, i);

          if (child != null && child is T)
               return (T)child;
          else
          {
               T childOfChild = FindVisualChild<T>(child);
               if (childOfChild != null)
                    return childOfChild;
          }
     }
return null;
}

To find parent control of a control in WPF, refer to Getting Parent Control of a Contol in WPF through Code or at Run-time.

Hope you will find it helpful, if you have any doubt please feel free to ask.

Getting parent control of a control in WPF through code or at runtime

I was working on a WPF project where I came across a problem where I need to refer to the parent container/control of specific type of control/item in WPF. So I did some RnD and search on the net for the problem and finally I came across a solution which I want to share.

Suppose we want to get ListViewItem on which a Button control is placed or we want to get the reference of that ListView in WPF.

Here is a method through which we can find parent of a control.

/// <summary>
/// Finds a parent of a given control/item on the visual tree.
/// </summary>
/// <typeparam name="T">Type of Parent</typeparam>
/// <param name="child">Child whose parent is queried</param>
/// <returns>Returns the first parent item that matched the type (T), if no match found then it will return null</returns>

public static T TryFindParent<T>(this DependencyObject child)
where T : DependencyObject
{
     DependencyObject parentObject = VisualTreeHelper.GetParent(child);
     if (parentObject == null) return null;

     T parent = parentObject as T;
     if (parent != null)
          return parent;
     else
          return TryFindParent<T>(parentObject);
}

To find child control of a control, refer to Find Child Control of a Control in WPF.

Hope you will find this blog helpful.

How to create Windows 7 Bootable USB Flash Drive (Pen Drive)

There are situations when we want to have a bootable Windows 7 USB Flash Drive/ Pen Drive. The reason could be any, may be the system on which you have to install Windows 7 does not have a DVD Rom or you don’t want to burn ISO of Windows 7 to a DVD Rom but the main point is how you can create a bootable Windows 7 USB Flash Drive, so that you can install Windows 7 through that USB Drive.

As I also had the need of bootable Windows 7 USB Flash Drive / Pen Drive I searched on the net and found many solutions. There are many tools available in the market with which we create Bootable Windows 7 USB Flash Drive but I preferred to use the tool provided by Microsoft to create the bootable flash drive.

As I don’t want others wasting time on internet and searching for the best tool to create a Windows 7 USB Flash Drive, I am going to share the method of how to create a bootable flash drive and the link from where you can download the Microsoft tool to create a bootable USB Flash Drive.
Windows 7 USB/DVD Download Tool

When you choose to download most software from the Microsoft Store, you have the option of using Download Manager or downloading from your browser. However, when you purchase Microsoft Windows 7 from Microsoft Store, you have an additional choice to make: whether to download a collection of compressed files or to download an ISO file. An ISO file combines all the Windows 7 installation files into a single uncompressed file.

If you choose to download an ISO file (so that you can create a bootable file from a DVD or USB flash drive), copy your Windows 7 ISO file onto your media and, run the Windows 7 USB/DVD Download Tool. After you do this, you can install Windows 7 onto your computer directly from the USB flash drive or DVD.

Here is the link to download Windows 7 USB/DVD Download Tool

After downloading the tool install it on your system and execute it. After executing the software you will see the following following screen.

Untitled

Browse the ISO of Windows 7 from your Hard Drive or from any media connected to your system, containing the ISO, then click on next to move to next step. In next step you will be asked to choose the media where you want to burn the ISO, choose USB Drive.

Choose-media

After choosing USB Drive you will be forwarded to step 3  where you have point to the USB Drive which you have to make bootable.

USB

In this step all you have to do is make sure that you are choosing the correct USB device. If you have other data on the device, move it to your hard drive, another USB device, or somewhere else before proceeding otherwise all your data will be lost. Now click on Begin Copying, after clicking you will be prompted to format the USB Drive, if it founds data on your drive, in order to create a bootable Windows 7 USB Flash Drive.

Erase

Choose to Erase USB Drive, it will again prompt you to ask for your confirmation to erase all your data. If you have backed up all your data then give it a positive reply to erase the data.

Format

The format will be very quick, while the copying of the files will take a little bit more time (about 10 to 15 minutes). Once the process is complete, you will get the confirmation message “Backup Completed”. At this point you can close the tool and use the USB drive to install Windows 7.

Finish

Note: You will have to choose USB as your first boot device in you bios setting in order to install Windows 7 from Bootable Windows 7 USB Flash Drive / Pen Drive created above.

Hope this will help you, if you find and mistake and if you have any suggestion please post your comments. Your comments are valuable.

Enumeration in C#

There are many situations when we want to give specific names to values. In order to accomplish this task we use Enumeration.

In this blog I am going to explain how to create and use enumeration (enum).

Enumerations are special sets of named values which all maps to a set of numbers, usually integers. The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.

Usually it is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience. However, an enum can also be nested within a class or struct.

By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example, in the following enumeration, Sunday is 0, Monday is 1, Tuesday is 2, and so forth.

Example:

enum Days 
{ 
     Sunday, 
     Monday, 
     Tuesday, 
     Wednesday, 
     Thursday, 
     Friday, 
     Saturday 
};

static void Main()
{
     int x = (int)Days.Sunday;
     int y = (int)Days.Friday;
     Console.WriteLine("Sunday = {0}", x);
     Console.WriteLine("Friday = {0}", y);
}

Output:

Sunday = 0

Friday = 5

Enumerators can have initializers to override the default values.

Example:

enum Days 
{ 
     Sunday=1, 
     Monday, 
     Tuesday, 
     Wednesday, 
     Thursday, 
     Friday, 
     Saturday 
};

Now in this case the output of the above code will be

Sunday = 1

Friday = 6

By default the underlying type of each element in the enum is int. You can specify another integral numeric type by using a colon, as shown in following example.

enum Months : byte 
{ 
     Jan, 
     Feb, 
     Mar, 
     Apr, 
     May, 
     Jun, 
     Jul, 
     Aug, 
     Sep, 
     Oct, 
     Nov, 
     Dec 
};

Conversions of enum in c#:

For demonstrating conversion I’m going to use the following enumeration:

enum Days 
{ 
     Sunday=1, 
     Monday, 
     Tuesday, 
     Wednesday, 
     Thursday, 
     Friday, 
     Saturday 
};

Converting enum to string:

Days day = Days.Monday;
string sDay = day.ToString();

In the above code sDay will hold “Monday” as a value.

Converting enum to int:

Days day = Day.Monday;
int iDay = (int)day;

After conversion iDay will be equal to 2.

Converting string to enum:

// Possible user input:
string sDay = "Monday";

// Try to convert the string to an enum:
Days day = (Days)Enum.Parse(typeof(Days), value);

Note: You can cause exceptions to be raised when parsing enums. When the contents of the string you try to parse is not represented in the enum, you must handle the exception. Handling exceptions is often one of the most important parts of your program.

Converting int to enum:

// Possible user input:
int iDay = 1;

// Try to convert the string to an enum:
Days day = (Days)iDay;

Hope, the above information on enum will be helpful. If you find any mistakes or want to give your suggestions please feel free to comment on the post and help me serve better.

Create parallelogram using CSS

In this simple blog I am going to provide a simple code to create a parallelogram through CSS which will be compatible on most of the browsers including Internet Explorer (IE).

Here is the CSS for creating Parallelogram.

#parallelogram 
{      
    width:             150px;     
    height:            100px;     
    -moz-transform:    skew(20deg);     
    -o-transform:      skew(20deg);     
    -webkit-transform: skew(20deg);     

    background: #FF0000;

    /* IE8+ - must be on one line, unfortunately */    
    -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(M11=0.3, M12=1.3, M21=0.7, M22=0, SizingMethod='auto expand')";      

    /* IE6 and 7 */    
    filter: progid:DXImageTransform.Microsoft.Matrix(            
            M11=0.3,            
            M12=1.3,            
            M21=0.7,            
            M22=0,            
            SizingMethod='auto expand');
}

<div id="parallelogram"></div>

You can see in the above code there is a function skew(). The function skew() accepting a parameter (here I have passed 20deg), which this the degree at which you want your parallelogram to be skewed, but does not work with Internet Explorer (IE).

For IE I have used -ms-filter and filter. These two will be used to display the parallelogram in Internet Explorer (IE).

In the method Matrix you would be seeing five parameters, ie. M11, M12, M21, M22 and SizingMethod.

  • M11 – This is used to set the angle at which you want to skew.
  • M12 – Width of Parallelogram
  • M21 – Height of Parallelogram
  • M22 – This parameter is used to rotate parallelogram to certain angle (keep this to 0 if you want your parallelogram to be horizontal).

Make changes in the parameters above and see the changes yourself.