Opt In (Do Not Edit Here)

Showing posts with label Windows Application. Show all posts
Showing posts with label Windows Application. Show all posts

Jan 12, 2013

Populating TreeView Control from XML file


Description:
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.

Procedure:
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>
      <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"/>
      </Customer>
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
set.ReadXml(@"..\..\XMLData.xml");

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


Summary:

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

                                                                                                              








Dec 19, 2012

TextSearch (Release 1.0) --- From Multiple Text Documents



The TextSearch-1.0 reveals the track to enhance the search functionality of Text Documents(s) belongs to same path/folder.

Description:
TextSearch-1.0, allows user to search a specific word from many text documents (File Format: *.txt) that belongs to same directory, with no need to open individual file(s) to search a word/text. It has simple GUI.


About Classes used -

#1: FolderBrowserDialog class allows user to select a folder.
#2: Directory class allows user to enumerate the file(s) available at specified directory path.
#3: StreamReader class provides an access to read the data from Stream such as Text File.
#4: Process class, to allow user to start a specific process.


Namespace Required         -           System.IO,
System.Diagnostics

Controls Used           -          
            1. TextBox Control (txtFolder, txtSearch)
          2. ListBox Control (lbFiles)
          2. Button Control (btnFolder, btnSearch, btnReset)
           
Here I present a Code for finding a text file(s) with a specified word/text provided as search string.
           

The Code:
1. Variable Declarations
            String target = ""; // Target string to Search
            String path = "";   // Path of Directory
            string[] files;     // Store file name(s) from given path
            bool search;        // Determine whether Target string present or NOT
            string[] list;      // Store file name(s) which includes Target string

Listing 1

2. List All Text File(s) (File Format: *.txt)
           
      void getFiles()
            {
                  lbFiles.Items.Clear();    //Clears the Previous file list (if any)
                  path = txtFolder.Text;    //Current path of selected folder
                 
              if (path != ""){

                  // Collects the name of files with extension of ".txt"
                  files = Directory.GetFiles(@path, "*.txt");
                 
                  foreach (string item in files)  //Add File Name to ListBox
                    lbFiles.Items.Add(Path.GetFileName(item));

                  //Display Total No. of Files
                  lblTotal.Text = "Total Files: " + lbFiles.Items.Count.ToString(); }
            }
Listing 2
   

3. After Click the “Folder” button, assign the selected path from to txtFolder TextBox, & call the getFiles() method to list all files within that folder.

4. List All Text File(s) which includes a string to be search.

      void Search()
      {
            int j = 0;        // to set the index of “list” array
            target = txtSearch.Text;
            list = new string[lbFiles.Items.Count];

            for (int i = 0; i < lbFiles.Items.Count; i++)
            {
                  search = false;

                  //Check whether Target String available within List of Text File(s)
                  if (!((new StreamReader(path + "\\"
                                    + lbFiles.Items[i]).ReadToEnd()).Contains(target)))
                  { search = true; }

                  list.SetValue(search.ToString(), j);
                  j++;
            }

            //Delete File(s) from Search (if Search Not Found)
            for (int i = list.Length - 1; i >= 0; i--)
            {
                  if (list.GetValue(i).ToString() != "False")
                        lbFiles.Items.RemoveAt(i);
            }
      }

Listing 3

5. Call a Search() method to list a resultant text file(s) at an OnClick Event of “Search” button.

6. Now execute the Application & see the intended result (Figure 1 & 2).
         
Intended Result:


Figure 1: After Select a Folder

Figure 2: After specifying a string to search



Additional Functionality:

  •  TextSearch-1.0, allow to regain a list of initial text files as before search (“Reset Search”).
  •   User can open a file, by double clicking the file name.

Summary:
          The Program TextSearch-1.0 is written is Visual C#, which allows a user to search a text file(s) which includes a specified word/text.





All Rights Reserved. 2014 Copyright SIMPLITONA

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

Top