Windows Develop Bookmark and Share   
 index > Windows Forms General > Loading a text file via openFileDialog into a listBox
 

Loading a text file via openFileDialog into a listBox

I'm trying to figure out how to load a text file via openFileDialogue into a listBox control using C#.
My goal is a very simple program where you load a file into a listBox,and the user can then click on a line of text in that listBox control and automatically havethe clicked linecopied to the clipboard.

I would appreciate any help, thank you. :)
Pacem  Wednesday, September 16, 2009 3:12 PM
try this out :
using System;
using System.Windows.Forms;
using System.Management;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace msdn
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DialogResult dr = this.openFileDialog1.ShowDialog();
            //MessageBox.Show(openFileDialog1.FileName);
            string[] lines = System.IO.File.ReadAllLines(openFileDialog1.FileName);
            foreach (string line in lines)
                listBox1.Items.Add(line);
            this.listBox1.Click += new EventHandler(listBox1_Click);
        }

        void listBox1_Click(object sender, EventArgs e)
        {
            string line = listBox1.Items[listBox1.SelectedIndex].ToString();
            MessageBox.Show(line);
            Clipboard.SetDataObject(line, true);
        }
    }
}


hope this helps :)
Paras
paras kumar  Wednesday, September 16, 2009 4:18 PM

add following code before ShowDialogue() :

this.openFileDialog1.InitialDirectory = @"D:\E3\TS";

Paras
paras kumar  Wednesday, September 16, 2009 8:57 PM
Which part of this specifically are you having trouble with?

A very simple example of this could be the following:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Net;

using System.Text;

using System.Windows.Forms;

namespace ConsoleApplication42

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

if (openFileDialog1.ShowDialog() != DialogResult.Cancel)

{

string[] lines = File.ReadAllLines(openFileDialog1.FileName);

listBox1.DataSource = lines;

}

}

private void listBox1_DoubleClick(object sender, EventArgs e)

{

Clipboard.SetText(listBox1.SelectedItem.ToString());

}

}

}


Of course, this requires you to have added a ListBox, a button and an OpenFileDialog to the form, and you've handled the button's click event, and the listBox's DoubleClick event.


Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton  Wednesday, September 16, 2009 4:18 PM
Which part of this specifically are you having trouble with?

A very simple example of this could be the following:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Net;

using System.Text;

using System.Windows.Forms;

namespace ConsoleApplication42

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

if (openFileDialog1.ShowDialog() != DialogResult.Cancel)

{

string[] lines = File.ReadAllLines(openFileDialog1.FileName);

listBox1.DataSource = lines;

}

}

private void listBox1_DoubleClick(object sender, EventArgs e)

{

Clipboard.SetText(listBox1.SelectedItem.ToString());

}

}

}


Of course, this requires you to have added a ListBox, a button and an OpenFileDialog to the form, and you've handled the button's click event, and the listBox's DoubleClick event.


Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton  Wednesday, September 16, 2009 4:18 PM
try this out :
using System;
using System.Windows.Forms;
using System.Management;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace msdn
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DialogResult dr = this.openFileDialog1.ShowDialog();
            //MessageBox.Show(openFileDialog1.FileName);
            string[] lines = System.IO.File.ReadAllLines(openFileDialog1.FileName);
            foreach (string line in lines)
                listBox1.Items.Add(line);
            this.listBox1.Click += new EventHandler(listBox1_Click);
        }

        void listBox1_Click(object sender, EventArgs e)
        {
            string line = listBox1.Items[listBox1.SelectedIndex].ToString();
            MessageBox.Show(line);
            Clipboard.SetDataObject(line, true);
        }
    }
}


hope this helps :)
Paras
paras kumar  Wednesday, September 16, 2009 4:18 PM
Thank you so much for your help, the samples were really helpful. :)
I'm designing a program for use with a game called Ultima Online for roleplay and other purposes where you have a pre-made script in .txt format that you can load into it and use without having to constantly tab back and forth to highlight/copy (it will be a top-most program). Thanks again.

One more thing... is there a way for openFileDialogue to open to a default folder?
Pacem  Wednesday, September 16, 2009 8:32 PM

add following code before ShowDialogue() :

this.openFileDialog1.InitialDirectory = @"D:\E3\TS";

Paras
paras kumar  Wednesday, September 16, 2009 8:57 PM
Ok LAST question I promise, I know I'm sounding like a noob here... I would not ask if I couldn't first figure it out on my own. :)
I'm looking for one final bit of functionality to my program.
Is it possible to program in a function key to enable the program to move down one line in the listBox and copy the line it moved down to? My goal here is to take out the tabbing back and forth all together for smooth operation in gaming and other environments.

So for example, if I'm within a MMORPG and doing role play, I would like to hit the F1 key to have the program move down to the next line of text in the listBox and copy it, so that all I would have to do is paste it within the game.

Thank you so much in advance, and I'll continue to try to figure it out till I see something here. :)
Pacem  Friday, September 25, 2009 3:07 PM
David M Morton  Friday, September 25, 2009 3:08 PM

You can use google to search for other answers

Custom Search

More Threads

• How can i draw graphics in a picturebox
• How to Convert from hex to dec
• MDI and child forms
• WebBrowser and BindingNavigator
• IOException error
• VC++ variables in labels
• ToolStripManager.Merge does not work
• Creating custom controls with interactive design
• How to remove the MouseHover effect on ToolStrip Control
• C# Passing Values To and Fro Form using EventArgs