Visual Studio 2010 - C Sharp
Open your Windows Forms program in the Visual Studio designer and open the Toolbox pane. Find the OpenFileDialog entry and double-click on it, or drag and drop to your form.
Add a Button and richTextBox to use with OpenFileDialog, find the button and richTextBox icon in the Toolbox and drag it to an area in your Windows Forms window. Click on button1 and in properties window find Text button1 and rename button1 to Open.
Select Form1.cs and add code:
using System.IO;
Select Form1.cs [Design]
Add an event to the button click by double-clicking on the "Open" in the designer.
Then write:
private void button1_Click(object sender, EventArgs e)
{
Stream myStream;
// Create an instance of the open file dialog box.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Open Text File";
openFileDialog1.Filter = "TXT files|*.txt";
openFileDialog1.FileName = "";
// or
// openFileDialog1.FileName = String.Empty;
openFileDialog1.InitialDirectory = @"C:\";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
string strfilename = openFileDialog1.FileName;
string filetext = File.ReadAllText(strfilename);
richTextBox1.Text = filetext;
}
}
}
Previous page: Format Factory
Next page: PHP, AJAX UPLOAD scripts and tutorials