I want to be able to browse files using a listbox and another list box which displays the contents of the file selected in the first listbox when the user clicks a button.
Can someone show me how to do this?
Loading
I want to be able to browse files using a listbox and another list box which displays the contents of the file selected in the first listbox when the user clicks a button.
Can someone show me how to do this?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Drew TaylorPosted Jun 15, 2007, 4:29 AM
Thanks again :) I have tried looking on the net for example code which didn't work.
Posted Jun 14, 2007, 9:51 AM
Drew TaylorPosted Jun 14, 2007, 4:29 AM
My program already reads an xml file but I need it to be formatted,
text............................
for example, return specifc information from the xml such as a specifc node and its contents. How would I specify that I want the following from an xml file?
Thanks,
Posted Jun 13, 2007, 8:22 PM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace ExamplesApp
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void startButton_Click(object sender, System.EventArgs e)
{
//Check the existence of the file
if(!File.Exists("xmlFile.xml"))
{
MessageBox.Show("File not found error.", "Error Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//Try to read the information from the file
try
{
//Create a new instance of FileStream to contain file
FileStream infoFile = new FileStream("xmlFile.xml", FileMode.Open, FileAccess.Read);
//Create a new instance of StreamReader to read info contained in infoFile
StreamReader infoReader = new StreamReader(infoFile);
fileContents.Clear();
fileContents.Text = Convert.ToString(infoReader.ReadToEnd());
}
//If information cannot be read, throw error
catch (System.IO.IOException)
{
MessageBox.Show("Could not read file.", "Error Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
}
//--------------
//-NO MORE CODE-
//--------------
All that did was open the XML file and read the contents as if it were a plain text file, pretty simple. Im not sure if it was what you were looking for but thats what it sounded like you wanted :)
Brandon
Drew TaylorPosted Jun 13, 2007, 4:51 AM
Yet another great answer thankyou! The error handling was useful. I was wondering if there is a way to lead a text/xml document directly into a panel or frame given a specific location? Rather than getting the file and reading the xml and outputting the text in a certain format. If this could be done would this be easier? because the xml document would already be formatted and all the nodes could be seen?
, and How can I specify these? Below the following XML is the code I modified from your last post. Thanks again for taking the time to explain it!
If not, or if I'm going to stick to the way I am doing it at the moment I need to know how to separate specific nodes or show certain information from the xml file. This is what my program is currently displaying when it reads the xml: (I need to get the
Kind Regards,
Drew,
GetOCSHub_Stock_Reservation_Response.xml on JCT 809
************************************************************************
072480907060800358-1
1
-1
-1
-1
983000038
986000061
986000079
986000087
private void JCT809_Click(object sender, System.EventArgs e)
{
//Check the existence of the file
if(!File.Exists(@"\\jct809\c$\Program Files\unipower\WWWRoot\CustomerOrder\OCSHubXML\GetOCSHub_Stock_Reservation_Response.xml")){
MessageBox.Show(@"File not found error. Click 'run' in the start menu and type \\jct813\c$, Username jct813\administrator, Password terminator", "Error Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else{
//Read the information from the fileSystem.Xml.XmlTextReader reader =
new System.Xml.XmlTextReader(@"\\jct809\c$\Program Files\unipower\WWWRoot\CustomerOrder\OCSHubXML\GetOCSHub_Stock_Reservation_Response.xml"); string contents = ""; while (reader.Read()){
reader.MoveToContent();
if (reader.NodeType == System.Xml.XmlNodeType.Element)contents += "<"+reader.Name + ">\n";
if (reader.NodeType == System.Xml.XmlNodeType.Text)contents += reader.Value + "\n";
}
this.textBoxXml.Clear(); //For each node in contents string header = "GetOCSHub_Stock_Reservation_Response.xml on JCT 809"; this.TillLabel.Text = "JCT 809";textBoxXml.Text = (header + Environment.NewLine + "************************************************************************"+ Environment.NewLine + Environment.NewLine + contents);
}
Posted Jun 12, 2007, 10:14 AM
Try this:
private void button12_Click(object sender, System.EventArgs e)
{
//Check the existence of the file
if(!File.Exists("C:\xmlfile.xml")
{
MessageBox.Show("File not found error.", "Error Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//Read the information from the file
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader("C:\xmlfile.xml");
string contents = "";
while (reader.Read())
{
reader.MoveToContent();
if (reader.NodeType == System.Xml.XmlNodeType.Element)
contents += "<"+reader.Name + ">\n";
if (reader.NodeType == System.Xml.XmlNodeType.Text)
contents += reader.Value + "\n";
}
this.listbox1.Items.Clear();
//For each node in contents
foreach(string strItem in contents)
{
addListItem.Items.Add(strItem);
}
}
}
What was happening was it wasnt finding the file and there was no error handler to handle the exception and it just crashed. Now itll check for the existence of the file and if it doesnt exist, itll just give you a warning message and return you back to the form.
As for adding items to the list box, you cannot add multiple items at the same time. Hence the foreach statement:
Pseudocode:
for each item in the list of collected items(contents), add the item to addList and continue until no more items exist, then exit.
I certainly hope this helps!
Brandon
EDIT:
You could also use an ArrayList to accomplish this with better divisions:
private void button12_Click(object sender, System.EventArgs e)
{
//Check the existence of the file
if(!File.Exists("C:\xmlfile.xml")
{
MessageBox.Show("File not found error.", "Error Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//Initialize the ArrayList
ArrayList[] contents = new ArrayList;
//Read the information from the file
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader("C:\xmlfile.xml");
while (reader.Read())
{
reader.MoveToContent();
if (reader.NodeType == System.Xml.XmlNodeType.Element)
contents.Add(Convert.ToString("<" + reader.Name + ">"));
if (reader.NodeType == System.Xml.XmlNodeType.Text)
contents.Add(Convert.ToString(reader.Value));
}
this.listbox1.Items.Clear();
//For each node in contents
foreach(string strItem in contents)
{
addListItem.Items.Add(strItem);
}
}
}
Keep in mind I have no way to actually test this code right now, so it might have a few small glitches in it but nothing too big to fix :)
Drew TaylorPosted Jun 11, 2007, 6:15 AM
Thanks thats great! I have now got a listbox that displays files from the specified location and a text box which displays the contents of the file when you press a button.
However, when I try and read a certain type of file this error occurs:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
I deleted internal from the filepath string variable declaration which allowed it to read from a specified location and it diaplays the files in the list box but when I press the button to get the contents for one of those files I get that error.
I found out how to read xml files but I don't know how to incorporate the selected items bit! I can read xml files if I specify the location but I want to be able to read any file that is selected. Can you help with this? This is the code I have used to read the xml
private void button12_Click(object sender, System.EventArgs e)
{
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader("c:\\xmlfile.xml");
string contents = "";
while (reader.Read())
{
reader.MoveToContent();
if (reader.NodeType == System.Xml.XmlNodeType.Element)
contents += "<"+reader.Name + ">\n\n";
if (reader.NodeType == System.Xml.XmlNodeType.Text)
contents += reader.Value + "\n\n";
}
this.listbox1.Items.Clear();
addListItem(contents);
}
Posted Jun 8, 2007, 12:22 PM
using
System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ExamplesApp
{
public partial class MainForm : Form{
//Declare the file path, it can be anything you want it to beinternal string filePath = ("C:/Documents and Settings/a/My Documents/txt files/"); public MainForm()
{
InitializeComponent();
}
private void startButton_Click(object sender, EventArgs e){
//-------------------//-startButton_Click-
//------------------- //Get all files listed in specified directory
DirectoryInfo dInfo = new DirectoryInfo(filePath);
FileInfo[] fInfo = dInfo.GetFiles("*.txt"); //For each file found, display the name in a list box
foreach (FileInfo FI in fInfo)
{
filesListBox.Items.Add(FI);
}
}
private void filesListBox_SelectedIndexChanged(object sender, EventArgs e){
//-----------------------------------//-filesListBox_SelectedIndexChanged-
//----------------------------------- string selectedFile; //Grab the file name that is selected
selectedFile = Convert.ToString(filesListBox.SelectedItem); //Open the file and assign a StreamReader to read the contents
FileStream fsFileInfo = new FileStream(filePath + selectedFile, FileMode.Open, FileAccess.Read);
StreamReader srFileInfo = new StreamReader(fsFileInfo); //Finally, display the entire contents of the file
fileContents.Text = Convert.ToString(srFileInfo.ReadToEnd());
}
}
}
//--------------
//-NO MORE CODE-
//--------------
Drew TaylorPosted Jun 8, 2007, 4:52 AM
That sounds good how would I do that?
But I want to be able to select a folder in a listbox (first) which will then display files within that folder in another list box and when these files are selected the contents will be displayed in a rich text window (they will all be text files)
How can I do this?
Posted Jun 7, 2007, 10:20 AM
Couldnt you have the user select a file name from the listbox, then have the contents displayed in a rich text box when the user clicks a button? Just a thought that would be a bit easier to work with :)
-Scrap