In top of form1 i did:
StreamWriter w;
In the constructor i did:
w = new StreamWriter(#"d:\test.txt");
Now i have a checkbox that enable a button:
if (checkBox2.Checked)
{
if (marklightnings == true)
{
myTrackPanelss1.panel1.Enabled = true;
myTrackPanelss1.panel1.Visible = true;
button1.Enabled = true;
}
}
Now in the button1 click event i write to the text file:
private void button1_Click_1(object sender, EventArgs e)
{
w.WriteLine("test");
w.Close();
}
What i want to do is that each time i check the checkbox it will open the text file for writing but not to create a new text file but to use the same exist text file and just add a text to it.
Its not a settings file but it is a text file i need later to read the text from it.
What i need is that when the button1 is enabled i will be able to click on it ant it will add text to the text file each click.
So i did w.Close();
But then it cant be add more text to be written. If i will make new instance for w it will create new empty text file.
Try:
w = new StreamWriter(#"d:\test.txt", true);
Or:
private void button1_Click_1(object sender, EventArgs e)
{
using (StreamWriter w = File.AppendText(#"d:\test.txt"))
{
w.WriteLine("test");
}
}
Related
I have a program that enables a user to search text files in an open file dialog. The user is then able to open an existing text file they choose and edit it. However, my problem is that when they file opens it appears blank. What am I missing?
private void Open_Click(object sender, RoutedEventArgs e)
{
TextBox openText = new TextBox();
var OpenFile = new Microsoft.Win32.OpenFileDialog();
Nullable<bool> Success = OpenFile.ShowDialog();
OpenFile.DefaultExt = ".txt";
OpenFile.Filter = "Text documents (.txt)|*.txt";
if (Success.HasValue && Success.Value)
{
openText.Text = OpenFile.FileName;
}
else
{
//cannot open file
}
}
Replace this:
openText.Text = OpenFile.FileName;
with this:
openText.Text = System.IO.File.ReadAllText(OpenFile.FileName);
Use File.ReadAllText()
openText.Text = File.ReadAllText(OpenFile.FileName);
on the first form I've a load button that load the file and call the second form. In the second form I've a richTextBox that has to show me text from the opened file, but it doesn't show nothing, here is what i've tried (I made richTextBox1 public to have access to it)
private void btnLoad_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FormEditor f2 = new FormEditor();
f2.ShowDialog();
using (System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName))
{
f2.richTextBox1.Text = sr.ReadToEnd();
}
}
}
If I try the same code putting richTextBox in the first form it works.
When you open f2 (f2.ShowDialog()), the code for filling richtextbox has not been executed, so you get an empty textbox on f2 (The code after ShowDialog() , will execute as soon as you close f2). Try:
FormEditor f2 = new FormEditor();
using (System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName))
{
f2.richTextBox1.Text = sr.ReadToEnd();
}
f2.ShowDialog();
FormEditor should be reponsible of showing the text, not the current form.
Write constructor with parameter for FormEditor and pass text to it, then save it in a variable and show it in richtextbox on form load.
Your FormEditor class should look like this:
private string textForEdit{get;set;}
public FormEditor(string txt)
{
textForEdit = txt;
}
private void FormEditor_load(object sender, EventArgs e)
{
richTextBox1.Text = textForEdit;
}
Then, change inside of your if block to this:
using (System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName))
{
FormEditor f2 = new FormEditor(sr.ReadToEnd());
f2.ShowDialog();
}
I have a Textbox and a button in form1, and when i write in the textbox i can save it to a file on the computer using my button. This is placed inside my button
public void button1_Click(object sender, EventArgs e)
{
string FileName = "C:\\sample\\sample.txt";
System.IO.StreamWriter WriteToFile;
WriteToFile = new System.IO.StreamWriter(FileName);
WriteToFile.Write(textBox1.Text);
WriteToFile.Close();
MessageBox.Show("Succeded, written to file");
But anyway, I want to move everything to do with streamWriter to their own class (Class1) and call it from my main form, inside the button.
If i move all the content insde the Button and move it to class1 inside a method, it claims that Textbox1 doesn't exist, obvious.
Do you have any tips or links on what i should read more about?
Best Regards
DB
You can do it in the class like this:
public class MyClass {
public static bool WriteToFile(string text){
string FileName = "C:\\sample\\sample.txt";
try {
using(System.IO.StreamWriter WriteToFile = new System.IO.StreamWriter(FileName)){
WriteToFile.Write(text);
WriteToFile.Close();
}
return true;
}
catch {
return false;
}
}
}
And in your button event:
public void button1_Click(object sender, EventArgs e){
if(MyClass.WriteToFile(textBox1.Text))
MessageBox.Show("Succeded, written to file");
else
MessageBox.Show("Failer, nothing written to file");
}
I set to true the AllowDrop implemented the DragOver and DragDrop events RichTextBox. On DragDrop event I load the dropped text files' contents on the RTB but it does add the icon of the file in RTB I'd to remove it:
Edit: Here's my code:
void msg_setup_dragDrop()
{
msg_textBox.AllowDrop = true;
msg_textBox.EnableAutoDragDrop = true;
msg_textBox.DragEnter += new DragEventHandler(msg_DragEnter);
msg_textBox.DragDrop += new DragEventHandler(msg_DragDrop);
}
void msg_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
void msg_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
StringBuilder buffer = new StringBuilder();
foreach (string filename in files)
{
try
{
string text = File.ReadAllText(filename);
buffer.Append(text);
}
catch (Exception ex)
{
string errMsg = string.Format("cannot read the file\"{0}\" error: {1}", filename, ex.Message);
MessageBox.Show(errMsg, "Reading file error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
msg_textBox.Text = buffer.ToString();
}
Somewhere you have set msg_textBox.EnableAutoDragDrop = true, either in your designer window or your code. You need to set this to false. You do still need to set AllowDrop = true.
When set to true, the winforms RichTextBox provides standard behaviors for drag-and-drop events, to which your custom handlers are added. If you don't want the standard behavior, you have to completely roll your own handlers. (The standard behavior for a dropped text file is OLE embedding. If you double click on the icon, notepad launches.)
I know this is an old post, but after trying to find a solution for the same problem, I ended up with a work around/solution
The DragDrop Method should be:
private void RTB_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
String[] filename = (String[])e.Data.GetData(DataFormats.FileDrop);
RTB.Text = fileName[0]; //this is where the text is
}
Now, if you have a RTB TextChanged method defined (RTB is rich text box), get the RTB.Text, then set the RTB.Text.
This seems to erase the unwated icons in the RTB
Example:
private void RTB_TextChanged(object sender, EventArgs e)
{
string currentText = RTB.Text.ToString();
RTB.Text = currenText;
}
Note: You have to go to the RTB properties and click on the lightning bolt icon (Events) and point the TextChanged Event to the RTB_TextChanged method you defined. Or just double click on the EventName and it'll create a new method for you to complete the method.
Icon placement; Occurs when the Msg_DragDrop method ends, depending on the e.Effect property.
Add this code to the end of the method=>
E.Effect = DragDropEffects.None;
void msg_DragDrop(object sender, DragEventArgs e)
{
//your code here
e.Effect = DragDropEffects.None;
}
I am using webBrowser in Windows Forms to edit a text file.
I can save the file via the dialog, but I don't know how to tell the webBrowser control to unload the document?
So that if I press button again it will open the saved file again.
private void EditDates_Click(object sender, EventArgs e)
{
if (EditDates.Text.StartsWith("Close"))
{
webBrowser1.ShowSaveAsDialog();
EditDates.Text = "Edit Dates";
webBrowser1.Document.CLOSE????
}
else
{
EditDates.Text = "Close Dates";
webBrowser1.Url = new Uri(#"H:\EOD\ProductionDates.txt");
webBrowser1.Document.ExecCommand("EditMode", false, null);
}
}
if (EditDates.Text.StartsWith("Close"))
{
webBrowser1.ShowSaveAsDialog();
EditDates.Text = "Edit Dates";
webBrowser1.Url = new Uri("about:blank");
}