Opt In (Do Not Edit Here)

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Jan 12, 2013

Populating TreeView Control from XML file

The blog post demonstrates how to populate TreeView Control using XML file in C#.NET.

The TreeView control has a Nodes collection with root TreeNode objects. Each TreeNode in turn has its own Nodes collection that holds more than one child TreeNode.

<TreeNode object>.Nodes.Add (TreeNode node) method adds a new TreeNode element with a specified text to the end of the current TreeNode collection.

Step 1: Variables Declaration
DataSet set = new DataSet();  //To store Tables from XML Document

TreeNode parent;              //store Main Table Names
TreeNode child;               //store Contents of Main Table
Listing 1

Step 2: XML file contents tables as shown in below format.
      <Customer CustID="202" Name="Pratik Ghanwat" Contact="8657413501"/>
      <Customer CustID="203" Name="Sohan Dal"      Contact="9746292005"/>
      <Customer CustID="204" Name="Sujit Dalvi"    Contact="8855572178"/>
      <Customer CustID="205" Name="Sushant Shinde" Contact="9835621704"/>
            <Customer CustID="206" Name="Amey Kulkarni"  Contact="9845671203"/>
Listing 2

Step 3: Now retrieve the values from XML File and add it to the Nodes Collection of TreeView Control.
//Read Contents of XML Document

for (int i = 0; i < set.Tables.Count; i++) 
      //Get the Name of Main Table
      parent = treeView1.Nodes.Add(set.Tables[i].TableName);

      //Get Contents of Main Table
      for (int j = 1; j < set.Tables[i].Rows.Count; j++) 
                    child = parent.Nodes.Add(set.Tables[i].Rows[j][2].ToString());
Listing 3

Intended Result:

Figure 1: Output


In this writing, we learned how to populate content from XML file to TreeView Control in C#.


Aug 9, 2012

Retrieve fonts installed on the system


About Classes used -

#1: An object of InstalledFontCollection class looks for fonts installed in Windows before the object is created.

#2:  This FontFamily class encapsulates a set of fonts that make up a font family.
A font family is a group of fonts that have the same typeface but different styles.

Here I implemented the Code for retrieving the fonts installed on the System.

The Code:
1. Put the following code at OnClick Event of the Combobox control to fetch the fonts into it.
private void cbSelectFont_Click(object sender, EventArgs e)
      //retrieves fonts collection on the system
      using (InstalledFontCollection ff = new InstalledFontCollection())
            foreach (FontFamily item in ff.Families) //retrieves font families
            { cbSelectFont.Items.Add(item.Name.ToString()); }
Listing 1

2. (Optional) Now you can apply these fonts for other control(s) also. (Here I used it for a Label Control)
3. Now execute the Application and see the result (Figure 1).

Intended Result:

Figure 1


          In this piece of writing, we have seen how to retrieve the installed fonts of the system using C# Language.

Mar 11, 2012

File Search (By Extension) in C#

          The article demonstrates how to search the files from the specified path of directory based on which type of file are you finding. This can be useful further if you want to code for finding the files from sub-directories.

Basic Requirements:
o   One TextBox Control
o   One ComboBox Control
o   One ListBox Control
o   Two Button Control

Step 1: Arrange the mentioned controls on windows form as follows.

Figure 1

Step 2: Write Code section for accessing path as String.
          private void btnFolder_Click(object sender, EventArgs e)
                   FolderBrowserDialog fbd = new FolderBrowserDialog();
                   textBox1.Text = fbd.SelectedPath.ToString();
Listing 1

Step 3: Now, select Extension Type from ComboBox Control for Searching Files.
Figure 2

Step 4: Now, Code to search the desired files from given path.
          private void btnSearch_Click(object sender, EventArgs e)
                   String path = textBox1.Text;
                   string[] files = Directory.GetFiles(@path);
                   bool search = false;  //returns true if file(s) found

                   for (int i = 0; i < files.Length; i++)
                             if (String.Compare(Path.GetExtension(files[i]), comboBox1.Text , true) == 0)
                                      search = true;

                   if (search == false)
                             listBox1.Items.Add("Search is Complete. There are no results to display.");
                             label1.Text = "Total Files: 0";
                   {        label1.Text = "Total Files: " + listBox1.Items.Count.ToString();            }
Listing 2

Step 5: Click the “Search” Button Control.
Step 6: If intended results found then the Name of that files are displayed in ListBox Control.

see Intended Result Section.

Intended Result:

Figure 3

          In this article, we have seen how we to find the file names (of particular extension) from specified directory using C#.

All Rights Reserved. 2014 Copyright SIMPLITONA

Powered By Blogger | Published By Gooyaabi Templates Designed By : BloggerMotion