Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a .txt file into a richTextBox in C#

I want to be able to open a .txt file up into a richtextbox in c# and also into a global variable i have made called 'notes' but don't know how to do this. This is the code i have at the moment:

OpenFileDialog opentext = new OpenFileDialog();
if (opentext.ShowDialog() == DialogResult.OK)
{
    richTextBox1.Text = opentext.FileName;
    Globals.notes = opentext.FileName;
}

Only problem is it doesn't appear in neither the richtextbox nor in the global varibale, and the global allows it to be viewed in another richtextbox in another form. So please can you help, with ideally the .txt file going into both,

Thanks

like image 838
Chris Bacon Avatar asked Dec 01 '10 17:12

Chris Bacon


People also ask

How do I add text to RichTextBox?

Step 2: Drag the RichTextBox control from the ToolBox and drop it on the windows form. You are allowed to place a RichTextBox control anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the RichTextBox control to add text in the RichTextBox control.

Which is the Maxlength of RichTextBox?

Remarks. When this property is set to 0, the maximum length of the text that can be entered in the control is 64 KB of characters. This property is typically used when the RichTextBox is used to display a single line of rich text format (RTF) text.

What is RichTextBox in Visual Studio?

In C#, RichTextBox control is a textbox which gives you rich text editing controls and advanced formatting features also includes a loading rich text format (RTF) files. Or in other words, RichTextBox controls allows you to display or edit flow content, including paragraphs, images, tables, etc.

What is RichTextBox in Windows Form applications?

The Windows Forms RichTextBox control is used for displaying, entering, and manipulating text with formatting. The RichTextBox control does everything the TextBox control does, but it can also display fonts, colors, and links; load text and embedded images from a file; and find specified characters.


3 Answers

Do you mean you want to have the text displayed or the filename?

richTextBox1.Text = File.ReadAllText(opentext.FileName); 
Globals.notes = richTextBox1.Text;

You probably also want to correct this to:

if (opentext.ShowDialog() == DialogResult.OK)
like image 82
BlueVoodoo Avatar answered Sep 28 '22 17:09

BlueVoodoo


In c# there are not global variables. The closest thing you can get is to make the variable "public static". But a better solution would be to make it an instance variable of an object you have access to, for example your main window class.

like image 28
codymanix Avatar answered Sep 28 '22 16:09

codymanix


if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName);
    richTextBox1.Text = sr.ReadToEnd();
    sr.Close();
}
like image 23
Ayush Giri Avatar answered Sep 28 '22 16:09

Ayush Giri