Open a .txt file into a richTextBox in C# - 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

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)

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.

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName);
richTextBox1.Text = sr.ReadToEnd();
sr.Close();
}

FileName property of OpenFileDialog control just gives the full path of file selected by user. In order to read the content of this file, you will need to use a method like File.ReadAllText.

Try using this, I used it for a chat program and it works fine, you can set your timer rate to whatever you want. You also don't have to use a timer, you can have a button initiate the refresh of the rich text box.
private void refreshRate_Tick(object sender, EventArgs e)
{
richTextBox1.Text = File.ReadAllText(#"path.txt");
}
Hope this helps!

I hope, I am not late. It seems like there is something like:
OpenFileDialog opentext = new OpenFileDialog();
if (opentext.ShowDialog() == DialogResult.OK)
{
string selectedFileName = opentext.FileName;
richTextbox.LoadFile(selectedFileName, RichTextBoxStreamType.UnicodePlainText);
}
Refs

Related

C#,Winform - Setting arguments for Process class with Drawing.Image

I'm generating a picture in the picturebox
pictureBox1.Image = Image.FromStream(imageActions.GetImage(reader["ID_no"].ToString()));
It is working perfectly, but I am also creating an option for the user to edit it via any application (Let's example macromedia) So I created a button and did the thing.
private void button2_Click(object sender, EventArgs e)
{
Process photoViewer = new Process();
photoViewer.StartInfo.FileName = #"C:\Program Files\Macromedia\Fireworks 8\Fireworks.exe";
photoViewer.StartInfo.Arguments = ___________;
photoViewer.Start();
}
I understand that in the photoViewer.StartInfo.Arguments = you can put here the path of the image, but in my case. the image is stored in the Database as Image datatype. any ideas?
In order to load the image in an external application, you will first of all need to save it to the disk.
After the application has closed you will need to load the updated image to display to the user.
The Image property of the PictureBox control has a Save method you can call:
string tempFile = System.IO.Path.GetTempFileName();
pictureBox1.Image.Save(tempFile);
You can then pass the tempFile value as a parameter to the photoViewer process (I used MSPaint as a Proof of Concept):
Process photoViewer = new Process();
photoViewer.StartInfo.FileName = #"C:\Windows\System32\MSPaint.exe";
photoViewer.StartInfo.Arguments = tempFile;
photoViewer.EnableRaisingEvents = true;
photoViewer.Exited += photoViewer_Exited;
photoViewer.Start();
The two lines of photoViewer.EnableRaisingEvents = true and photoViewer.Exited += photoViewer_Exited; will tell your application when the photoViewer process exits, and this is a good place for you to load the image and display to your users.
private void photoViewer_Exited(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(tempFile);
}
Note: string tempFile will need to be a class member variable so it can be accessed in the two functions.

How to add watermark to all files in folder?

I am use method, in which openfiledialog and savefiledialog for second method for add watermark on one picture, press button-call method
How to realize this for all pictures in folder?
I think,neccesary used folderbrowserdialog like this:
AddWatermarkOnAllPictures_Click ()
{
folderbrowser.Dialog.ShowDialog();
{
}
}
I am right? May be, i must call method, which adds watermark and this methods work with ALL pictures in folder?
Assuming "folderbrowser" is your FolderBrowserDialog
Iterate over the selected directory:
if (folderbrowser.ShowDialog() != DialogResult.OK) return;
foreach(string file in System.IO.Directory.GetFiles(folderbrowser.SelectedPath))
{
// Do something
}

TXT file is not saving properly with Gtk# 2.0 , Xamarin studio

I am a beginner in programming and it may seem a little funny to ask questions like this, but i have this code :
protected void OnSaveActionActivated (object sender, EventArgs e)
{
FileFilter filter = new FileFilter();
filter.Name = "Text files";
filter.AddPattern ("*.txt");
FileChooserDialog fcd = new FileChooserDialog ("... ?", this,
FileChooserAction.Save, "Cancel", ResponseType.Cancel, "OK", ResponseType.Accept);
if ((int)fcd.Run () == (int)ResponseType.Accept) {
System.IO.StreamWriter sw= new System.IO.StreamWriter(fcd.Filename);
sw.Write(textview1);
fcd.Filter = filter;
fcd.Destroy ();
} else {
fcd.Destroy ();
}
fcd.Destroy ();
Tho my Open file version works perfectly, I cant seem to make files to save properly into txt files, and i just dont understand anymore.
Sos :(
If textView1 is an actual control, then
sw.Write(textview1);
will not do what you want. Instead, try
sw.Write(textview1.Text);
which will write out the actual contents of the control, not the control itself.
Also, as #HighCore mentioned, it is much simpler to do
File.WriteAllText(file_path, textView1.Text);

C# Windows Forms: OpenFileDialog Strange Issues

This is kinda strange, let me try to explain it as best as possible:
When I create a new file and Save it, it saves correctly (test.xml).
When I make changes to this file and Save it, it saves correctly (to test.xml)
When I make changes again to this file or just choose Save As, it works correctly (newtest.xml)
However, when I do a file open, make changes to a file (test.xml) and click Save it is saving to (newtest.xml).
This is in my MainForm.cs
if (this.openEditorDialog1.ShowDialog(this) == DialogResult.OK && editForm != null)
{
editForm.Close();
editForm = new EditorForm(this);
editForm.OpenFile(this.openEditorDialog1.FileName);
editForm.Closing += new CancelEventHandler(EditorForm_Closing);
editForm.MdiParent = this;
editForm.Show();
}
private void biFileSave_Click(object sender, EventArgs e)
{
if (!editForm.HasFileName)
{
if (this.saveEditorDialog1.ShowDialog(this) == DialogResult.OK)
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
editForm.FileName = this.saveEditorDialog1.FileName;
}
}
else
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
}
This is in my EditorForm.cs
public void OpenFile(string strFileName)
{
diagramComponent.LoadSoap(mainForm.openEditorDialog1.FileName);
this.FileName = mainForm.openEditorDialog1.FileName;
this.tabControl1.SelectedTab = DiagramTab;
}
I'm sure it has to do with the what I'm doing in the EditoForm but I can't seem to figure it out.
else
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
It looks like you want:
this.ActiveDiagram.SaveSoap(editForm.FileName);
It must have to do with mainForm.openEditorDialog1.FileName. Use a FileName property of the form that does the saving. When you open the file, set the fileName to mainForm.openEditorDialog1.FileName. When you SaveAs, set the FileName property there, too. This way, whenever the current file, changes you set the FileName property appropriately. Then, when it comes time to save the file, you always have the correct filename.
In summary, only use the .FileName property of the SaveAs dialog or the FileOpen dialog right after you use them.

Save Contents Of a TextBox To a File

I'm developing an application that has a TextBox. I want to write its contents to a file, but how can I do this?
There are many ways to accomplish this, the simplest being:
using(var stream = File.CreateText(path))
{
stream.Write(text);
}
Be sure to look at the MSDN page for File.CreateText and StreamWriter.Write.
If you weren't targeting the .NET Compact Framework, as your tags suggest, you could do even simpler:
File.WriteAllText(path, string);
System.IO.File.WriteAllText("myfile.txt", textBox.Text);
If you're stuck on some brain-dead version of the BCL, then you can write that function yourself:
static void WriteAllText(string path, string txt) {
var bytes = Encoding.UTF8.GetBytes(txt);
using (var f = File.OpenWrite(path)) {
f.Write(bytes, 0, bytes.Length);
}
}
Try this:
using System.Text;
using System.IO;
static void Main(string[] args)
{
// replace string with your file path and name file.
using (StreamWriter sw = new StreamWriter("line.txt"))
{
sw.WriteLine(MyTextBox.Text);
}
}
Of course, add exception handling etc.
For a richTextBox, you can add a "Save" button for this purpose. Also add a saveFileDialog control from Toolbox, then add following code in button's click event.
private void button1_Click(object sender, EventArgs e)
{
DialogResult Result = saveFileDialog1.ShowDialog();//Show the dialog to save the file.
//Test result and determine whether the user selected a file name from the saveFileDialog.
if ((Result == DialogResult.OK) && (saveFileDialog1.FileName.Length > 0))
{
//Save the contents of the richTextBox into the file.
richTextBox1.SaveFile(saveFileDialog1.FileName);
}
}

Categories

Resources