C#中,对用TextBox读取数据的时候要求读取的数据能自动换行的代码怎样写?TextBOX命令为txtContent

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#中,对用TextBox读取数据的时候要求读取的数据能自动换行的代码怎样写?TextBOX命令为txtContent相关的知识,希望对你有一定的参考价值。

从TextBox读出的文本内容是无法换行的,用Replace函数将文本内容中的某个内容(具体的忘记了,你可以调试看下那个特殊字符)换为"\\r\\n"就可以了。 参考技术A <asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine"></asp:TextBox>

StringReader sr = new StringReader(txtContent.Text);
string str = string.Empty;
do

str += sr.ReadLine() + "*";
while (sr.Peek() != -1);
参考技术B winform的话你用个richtextbox就可以自动换行啊。。
web的话用textarea
参考技术C 把textbox.textMode 改成 多行 把wrap 改成true 是可以自动换行的,记得这样应该可以 参考技术D 是读取文件么?
txtContent.Text=sr.ReadToEnd();
第5个回答  2011-01-07 - -!你到底想搞啥???
需要换行就加上“/r/n”就好了嘛

C# 读取写入文件

读取文件

File.ReadAllText(textBox1.Text,Encoding.ASCII);

Form

 1 namespace ReadWriteText
 2 {
 3     public partial class Form1 : Form
 4     {
 5         private readonly OpenFileDialog chooseOpenFileDialog = new OpenFileDialog();
 6         private string chosenFile;
 7 
 8         public Form1()
 9         {
10             InitializeComponent();
11 
12             menuFileOpen.Click += OnFileOpen;
13             chooseOpenFileDialog.FileOk += OnOpenFileDialogOK;
14 
15             menuFileSave.Click += OnFileSave;
16         }
17 
18         private void OnFileOpen(object Sender, EventArgs e)
19         {
20             chooseOpenFileDialog.ShowDialog();
21         }
22 
23         private void OnFileSave(object Sender, EventArgs e)
24         {
25             SaveFile();
26         }
27 
28         private void OnOpenFileDialogOK(object Sender, CancelEventArgs e)
29         {
30             chosenFile = chooseOpenFileDialog.FileName;
31             Text = Path.GetFileName(chosenFile);
32             DisplayFile();
33         }
34 
35         private void SaveFile()
36         {
37             StreamWriter sw = new StreamWriter(chosenFile, false,
38                                                Encoding.Unicode);
39             foreach (string line in textBoxContents.Lines)
40                 sw.WriteLine(line);
41             sw.Close();
42         }
43 
44         private void DisplayFile()
45         {
46             StringCollection linesCollection = ReadFileIntoStringCollection();
47             string[] linesArray = new string[linesCollection.Count];
48             linesCollection.CopyTo(linesArray, 0);
49             textBoxContents.Lines = linesArray;
50         }
51 
52         private StringCollection ReadFileIntoStringCollection()
53         {
54             const int MaxBytes = 65536;
55             StreamReader sr = new StreamReader(chosenFile);
56             StringCollection result = new StringCollection();
57             int nBytesRead = 0;
58             string nextLine;
59             while ((nextLine = sr.ReadLine()) != null)
60             {
61                 nBytesRead += nextLine.Length;
62                 if (nBytesRead > MaxBytes)
63                     break;
64                 result.Add(nextLine);
65             }
66             sr.Close();
67             return result;
68         }
69     }
70 }
View Code

Designer

 1 namespace ReadWriteText
 2 {
 3     partial class Form1
 4     {
 5         /// <summary>
 6         /// Required designer variable.
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9 
10         /// <summary>
11         /// Clean up any resources being used.
12         /// </summary>
13         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22 
23         #region Windows Form Designer generated code
24 
25         /// <summary>
26         /// Required method for Designer support - do not modify
27         /// the contents of this method with the code editor.
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.components = new System.ComponentModel.Container();
32             this.menuFileOpen = new System.Windows.Forms.MenuItem();
33             this.textBoxContents = new System.Windows.Forms.TextBox();
34             this.menuItem1 = new System.Windows.Forms.MenuItem();
35             this.menuFileSave = new System.Windows.Forms.MenuItem();
36             this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
37             this.SuspendLayout();
38             // 
39             // menuFileOpen
40             // 
41             this.menuFileOpen.Index = 0;
42             this.menuFileOpen.Text = "&Open";
43             // 
44             // textBoxContents
45             // 
46             this.textBoxContents.Dock = System.Windows.Forms.DockStyle.Fill;
47             this.textBoxContents.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
48             this.textBoxContents.Location = new System.Drawing.Point(0, 0);
49             this.textBoxContents.Multiline = true;
50             this.textBoxContents.Name = "textBoxContents";
51             this.textBoxContents.ScrollBars = System.Windows.Forms.ScrollBars.Both;
52             this.textBoxContents.Size = new System.Drawing.Size(284, 245);
53             this.textBoxContents.TabIndex = 1;
54             // 
55             // menuItem1
56             // 
57             this.menuItem1.Index = 0;
58             this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
59             this.menuFileOpen,
60             this.menuFileSave});
61             this.menuItem1.Text = "&File";
62             // 
63             // menuFileSave
64             // 
65             this.menuFileSave.Index = 1;
66             this.menuFileSave.Text = "&Save";
67             // 
68             // mainMenu1
69             // 
70             this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
71             this.menuItem1});
72             // 
73             // Form1
74             // 
75             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
76             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
77             this.ClientSize = new System.Drawing.Size(284, 245);
78             this.Controls.Add(this.textBoxContents);
79             this.Menu = this.mainMenu1;
80             this.Name = "Form1";
81             this.Text = "Form1";
82             this.ResumeLayout(false);
83             this.PerformLayout();
84 
85         }
86 
87         #endregion
88 
89         private System.Windows.Forms.MenuItem menuFileOpen;
90         private System.Windows.Forms.TextBox textBoxContents;
91         private System.Windows.Forms.MenuItem menuItem1;
92         private System.Windows.Forms.MenuItem menuFileSave;
93         private System.Windows.Forms.MainMenu mainMenu1;
94     }
95 }
View Code

 

以上是关于C#中,对用TextBox读取数据的时候要求读取的数据能自动换行的代码怎样写?TextBOX命令为txtContent的主要内容,如果未能解决你的问题,请参考以下文章

c# winform中读取字符串?

C# 关于写入和读取的问题

C#中如何在一个窗体中调用另一个窗体控件中的数据?

c#程序,textbox如何设置文本居中显示?如何按行读取文本文件内容?

C# 怎么保存一个矩阵?

c#中 Double num1 = Convert.ToDouble(this.textBox1.Text.ToString())是啥意思,里面的()是啥意思