[英]Saving TextBox content in a text file C#
I created this form that allows me to open a txt file and puts the content in a TextBox.我创建了这个表单,它允许我打开一个 txt 文件并将内容放在一个 TextBox 中。 I want to be able to modify the content writing in the textbox and then save it using the SaveFileDialog.我希望能够修改文本框中写入的内容,然后使用 SaveFileDialog 保存它。 Here's my code这是我的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1(){
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
if (openFileDialog1.ShowDialog() == DialogResult.OK){
System.IO.StreamReader input = new
System.IO.StreamReader(openFileDialog1.FileName);
TextBox_stampa_contenuto.AppendText(input.ReadToEnd());
input.Close();
}
}
private void salva_file_Click(object sender, EventArgs e){
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e){
string name = saveFileDialog1.FileName;
File.WriteAllText(name, TextBox_stampa_contenuto.Text);
}
}
}
When I run it works perfectly opening the file, but after I modify it and try to save it doesn't work.当我运行它时,它可以完美地打开文件,但是在我修改它并尝试保存它之后它不起作用。 The content stays the same.内容保持不变。 There's a way to fix it?有办法解决吗? And also how can I put the text in the textbox in write mode and not in append mode.以及如何将文本以写入模式而不是 append 模式放入文本框中。 Thanks.谢谢。
Try This: 尝试这个:
Solution 1: if you want to Save the contents of Textbox into TextFile you need to check the DialogResult
return type of the SaveDialog
. 解决方案1:如果要将文本框的内容保存到TextFile中,则需要检查SaveDialog
的DialogResult
返回类型。
private void salva_file_Click(object sender, EventArgs e)
{
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string name = saveFileDialog1.FileName;
File.WriteAllText(name, TextBox_stampa_contenuto.Text);
}
}
Solution 2: if you want to Insert the File Text into Textbox whithout appending you need to Assign the File String into TextBox Text
property. 解决方案2:如果要在不附加的情况下将文件文本插入文本框,则需要将文件字符串分配给文本框Text
属性。
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
TextBox_stampa_contenuto.Text=System.IO.File.ReadAllText(openFileDialog1.FileName);
}
using System.CodeDom.Compiler;
using System.Windows.Forms;
namespace Text_Editor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ladenToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "*.txt|*.txt";
openFileDialog.ShowDialog(richTextBox1);
string test = File.ReadAllText(openFileDialog.FileName);
richTextBox1.Text = test;
}
private void speichernToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string name = saveFileDialog1.FileName;
File.WriteAllText(name, Tex.Text);
}
/*Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.
myStream.Close();
}
}
*/
}
private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
namespace Text_Editor
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.dateiToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ladenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.speichernToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.beendenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.dateiToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(800, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// dateiToolStripMenuItem
//
this.dateiToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ladenToolStripMenuItem,
this.speichernToolStripMenuItem,
this.beendenToolStripMenuItem});
this.dateiToolStripMenuItem.Name = "dateiToolStripMenuItem";
this.dateiToolStripMenuItem.Size = new System.Drawing.Size(46, 20);
this.dateiToolStripMenuItem.Text = "Datei";
//
// ladenToolStripMenuItem
//
this.ladenToolStripMenuItem.Name = "ladenToolStripMenuItem";
this.ladenToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.ladenToolStripMenuItem.Text = "Laden";
this.ladenToolStripMenuItem.Click += new System.EventHandler(this.ladenToolStripMenuItem_Click);
//
// speichernToolStripMenuItem
//
this.speichernToolStripMenuItem.Name = "speichernToolStripMenuItem";
this.speichernToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.speichernToolStripMenuItem.Text = "Speichern";
this.speichernToolStripMenuItem.Click += new System.EventHandler(this.speichernToolStripMenuItem_Click);
//
// beendenToolStripMenuItem
//
this.beendenToolStripMenuItem.Name = "beendenToolStripMenuItem";
this.beendenToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.beendenToolStripMenuItem.Text = "Beenden";
this.beendenToolStripMenuItem.Click += new System.EventHandler(this.beendenToolStripMenuItem_Click);
//
// richTextBox1
//
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Location = new System.Drawing.Point(0, 24);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(800, 426);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private RichTextBox richTextBox1;
private ToolStripMenuItem dateiToolStripMenuItem;
private ToolStripMenuItem ladenToolStripMenuItem;
private ToolStripMenuItem speichernToolStripMenuItem;
private ToolStripMenuItem beendenToolStripMenuItem;
private MenuStrip menuStrip1;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.