Hello,
I'm new to this forum and I've found some great information here so far! Great site!
Here's my dilemma...
I have a windows form application which is currently populating a multiline textbox with an array result of files that is found in a specified directory (I can also do it in a richtextbox). What I want to do with these files is create links to them using something like "file://path/file.txt" (but display only the filename in the visible link) and open in an existing richtextbox.
I can get files to populate into the richtextbox with no problem, but I'd like to make any files I find in that list linked so that when the user clicks on the file in the result, it will read the contents and display in a larger richtextbox. I've been searching all over the place and cannot find anyway to do this without creating individual link labels, which cannot be rendered dynamically based on the array result.
As a side note, I'm eventually going to be putting the paths of the text files in an SQL database (which I'm not quite sure yet how I will approach this). I'm sure this is trivial for a veteran programmer, but I'm a newbie to C# and I'm learning the ropes. Any help here would be greatly appreciated.
Thanks
Loading
Nilanka DharmadasaPosted Dec 21, 2009, 5:11 AM
I have the solution to your problem. Check my code.
I changed your code. Added a datagridview and also did some changes in filesystem watcher.
If you find my answer useful, please do not forget to tick 'Do you like this answer' checkbox.
JosePosted Dec 21, 2009, 7:52 AM
Jose
Sam HobbsPosted Dec 21, 2009, 1:02 AM
JosePosted Dec 18, 2009, 2:32 PM
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Configuration;
using System.IO;
namespace DirectoryMonitorWinForm
{
///
/// Summary description for DirectoryMonitor.
///
public class DirectoryMonitorForm : System.Windows.Forms.Form
{
// The path where the FileMonitor will watch for files...
private static string Path = @"C:\test";
private static string Filter="*.txt";
private static bool IncludeSubs=false;
private System.IO.FileSystemWatcher FileMonitor;
private RichTextBox richTextBox2;
private RichTextBox richTextBox1;
private DataGridView dataGridView1;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public DirectoryMonitorForm()
{
// Required for Windows Form Designer support
InitializeComponent();
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows Form Designer generated code
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new DirectoryMonitorForm());
}
public void FileMonitor_Changed(object sender, System.IO.FileSystemEventArgs e)
{
string ChangeType = e.ChangeType.ToString();
FileMonitor.Path = Path.ToString();
//display a message box for the appropriate changetype.
if (ChangeType=="Created")
{
// This adds the "file://" to the file to make it appear as a link
richTextBox2.AppendText("file:/" + "/" + FileMonitor.Path + e.Name);
}
public void DirMon_Load(object sender, System.EventArgs e)
{
FileMonitor.Path = Path.ToString();
FileMonitor.Filter = Filter.ToString();
FileMonitor.IncludeSubdirectories = IncludeSubs;
FileMonitor.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
//Begin monitoring.
FileMonitor.EnableRaisingEvents = true;
//Get the list of existing files from the folder and list in the textbox
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\test");
//to retrieve *.TXT files alone
System.IO.FileInfo[] rgFiles = di.GetFiles("*.txt");
richTextBox2.AppendText("***Contents of Folder***" + "\r\n");
foreach (System.IO.FileInfo fi in rgFiles)
{
string filePath = fi.Name;
string line;
if (File.Exists(filePath))
{
StreamReader file = null;
try
{
file = new StreamReader(filePath);
while ((line = file.ReadLine()) != null)
{
richTextBox2.AppendText(line + "\r\n");
}
}
finally
{
if (file != null)
file.Close();
}
}
richTextBox2.AppendText(fi.Name + "\r\n");
}
}
Sam HobbsPosted Dec 17, 2009, 11:40 PM
Assuming you already have a DataTable with the data you want to show, then you can set a DataGridView's DataSource Property to the DataTable and the data for the DataGridView will be filled in by automatically. Just add a DataGridView to the form. If you want all fields of the DataTable to be shown as columns in the DataGridView in the order they appear in the DataTable then you don't want to specify the columns at design time. The default for the AutoGenerateColumns Property is true so you can leave that as-is. If you need to specify columns in the DataGridView that are not all the fields in the DataTable then you will need to create the columns yourself. If the data in the DataTable changes, then you probably will need to create a new DataTable and fill it with the new data and change the DataGridView's DataSource Property to the new DataTable.
When you have all that working, look at the documentation of the DataGridView.CellClick Event. Do you know how to add event handlers for forms? Add a handler for th DataGridView.CellClick Event and note that the example in the DataGridView.CellClick documentation shows how to determine what row and column that the click event occured for. Let us know if you need help with that.
JosePosted Dec 17, 2009, 10:25 AM
What I would like to do it be able to click any of the files in the textbox (or cell of a table) and load the contents of the file into the larger richtextbox upon clicking the file (or double click). If you need the existing code I have, let me know.
Sam HobbsPosted Dec 17, 2009, 1:40 AM
I am not sure how to use a RTF box to do what you are describing, but it probably is possible. Another possibility is to use HTML in a WebBrowser control instead of a RTF control.