Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:20:14+00:00 2026-06-10T07:20:14+00:00

I have this Code in Form1. Im doing a search for xml files. When

  • 0

I have this Code in Form1. Im doing a search for xml files. When i find them im using listBox1 selected index changed event and i want to do that when i select item in the lixtBox it will consider it as a file will parse it content and show me the parsed content.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.IO;
using System.Collections;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        DirectoryInfo dirinf = new DirectoryInfo(@"C:\");
        List<FileSystemInfo> fsi = new List<FileSystemInfo>();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            backgroundWorker1.RunWorkerAsync();
            button1.Enabled = false;
        }

        private void ParseAndDisplayXml(string filename)
        {
            XDocument document = XDocument.Load(filename);
            var list = document.Root.Elements("Message")
                .Select(
                e => new
                {
                    Date = e.Attribute("Date").Value.ToString(),
                    Time = e.Attribute("Time").Value.ToString(),
                    Text = e.Element("Text").Value.ToString()
                }
                );
 string result="";
            foreach (var item in list)
            {
               result += string.Format("Date--{0},Time--{1},Text--{2}", item.Date, item.Time, item.Text + Environment.NewLine);
            }
        }




        public void Search(string strExtension,
                            DirectoryInfo di,
                            List<FileSystemInfo> pResult)
        {
            try
            {

                foreach (FileInfo fi in di.GetFiles())
                {
                    if (InvokeRequired)
                    {
                        BeginInvoke(new Action(() => label2.Text = fi.Name));
                    }
                    if (fi.Name == "MessageLog.xsl")
                    {
                        foreach (FileInfo fii in di.GetFiles())
                        {
                        if (fii.Extension == strExtension)
                            pResult.Add(fii);
                        }
                        if (InvokeRequired)
                        {
                            BeginInvoke(new Action(() => label4.Text = pResult.Count.ToString() + Environment.NewLine));
                        }

                    }
                }

                    foreach (DirectoryInfo diChild in di.GetDirectories())
                        Search(strExtension, diChild, pResult);

            }
            catch (Exception e)
            {
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Search(".xml", dirinf, fsi);
            backgroundWorker1.ReportProgress(100);

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            for (int i = 0; i < fsi.Count; i++)
            {                
                    listBox1.Items.Add(fsi[i].Name + Environment.NewLine);
            }


        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label2.Text = listBox1.SelectedItem.ToString();
        }            
    }
}

Im starting the search from C:\
Then when the search so over completed im adding the items it found to the listBox1.

For example now in my listBox1 i have 4 files:

danny.xml
adi.xml
sharon.xml
yoval.xml

In the selectedindexchanged i added option so the user can move between the items.

Now what i want to do is when the user select some index for example index [1] in the listBox and only if he clicked enter with the keyboard or clicked with the mouse left click it will call/use the function: ParseAndDisplayXML.

Then it will parse the selected index wich need to be translated to a file so in the backgroundWorker1_RunWorkerCompleted event i madding the files to the listBox as items but only with the names of the files. If i did .FullName instead .Name it was adding the files names with the directories too.

So i need somehow to get the FullName of the files in the completed event i think then when selecting one of the FullName items to parse it and display it in the listBox.

The parse function should take the specific content from the xml files and it worked i checked this function before alone.

The problem is how do i make that the user will select the index by click/key enter and how to parse and display it ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:20:16+00:00Added an answer on June 10, 2026 at 7:20 am

    When you add something to a listbox.
    It expects an object, and sets the text to object.ToString()

    e.g.

    MyListBox.Add(100);
    

    Would box 100 and display “100”

    Couldn’t find if FileSystemInfo’s ToString() method has been overridden but first thing to try would be

    private void backgroundWorker1_RunWorkerCompleted(object sender,
    RunWorkerCompletedEventArgs e)
    {
    // newline is unnecesary and you should be using foreach
      foreach(FileSystemInfo f in fsi)
      {                
        listBox1.Items.Add(f);
      }
    }
    // display full name of file
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
      label2.Text = ((FileSystemInfo)listBox1.SelectedItem).Fullname;
    }         
    

    If FileSystemInfo.ToString() doesn’t return Name, there are a few ways to deal with that.
    If you don’t want to hold on to the FileSystemInfo instances, we can deal with that too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code <html> <head> <script type=text/javascript src=my-search.js></script> </head> <body onLoad=my_Init();> <div> <form
I have this code in my forms.py : from django import forms from formfieldset.forms
I have this code in my view: echo form_label('State', 'state'); $options = array( 'No
I have this code in my cfm, which works <cfif not StructIsEmpty(form)> <cfset larray
I have this form submit code: Event.observe(window, 'load', init, false); function init() { Event.observe('addressForm',
I currently have this useful code that I found elsewhere on StackOverflow: form.DrawToBitmap(bmp, new
Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
I have my form in a table and I use this code to add
I have a delphi 7 form: and my code: when I run this form
I have the following code: echo ' <td> <input type=button name=delete value=X onclick=clearSelection(this.form, '.$type.');this.form.submit();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.