im having some trouble, im fairly new to c# programming and im not sure what im doin wrong or havnt done.
This is for an assignment which is due tomorrow. Not asking for someone to complete it but to help me find where im goin wrong.
Heres my prob.
on my form i have a richTextBox and a button, when u click the buttong it calls LoadMyFile() which will open a txt file and i want to display the data from the file in a richTextBox. but im not sure how to do it right.
heres my code so far
public void LoadMyFile()
{
// Create an OpenFileDialog to request a file to open.
OpenFileDialog openFile1 = new OpenFileDialog();
// Initialize the OpenFileDialog to look for TXT files.
openFile1.DefaultExt = "*.txt";
openFile1.Filter = "TXT Files|*.txt";
// Determine whether the user selected a file from the OpenFileDialog.
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
openFile1.FileName.Length > 0)
{
try
{
// Load the contents of the file into the RichTextBox.
richTextBox1.LoadFile(openFile1.FileName);
// FileData = richTextBox1.Text;
}
catch (ArgumentException exc)
{
Console.WriteLine(exc.Message);
return;
}
}
}
I can select and attempt to open the file but the data doesnt display in the text box.Anyone can help me pls or point me in the right direction pls.
Any help would be most appreciated
Thank you in advance
Bongkerz
Edit** Problem Solved!
Sorry for the useless thread, i should of read around the forums before posting, i do apologise
Thanks to Siten0308, ur thread helped me fix my problem, heres ur code i used hope you dnt mind
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Text Documents only (*.txt)|*.txt|All Files|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
richTextBox1.Text = dlg.FileName;
richTextBox1.Clear();
richTextBox1.Text = File.ReadAllText(dlg.FileName);
}
else
{
MessageBox.Show("Please choose a Text File");
}


Sign In
Create Account

Back to top









