c#怎样实现下面生成的txt中的内容自动换行???

2025-03-03 08:53:55
推荐回答(3个)
回答1:

在TextBox中实现换行的方法如下:

(1)在Visual Studio中添加一个“Windows 窗体应用程序”项目

(2)在Form上添加2个TextBox和1个Button

设置textBox1的属性

  • Multiline = True

  • ScrollBars = Both

(3)Form1窗体代码Form1.cs

using System;
using System.Windows.Forms;

namespace BaiduWinformApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button1.Text = "输入";
        }
        
        // 点击button1 “输入”按钮
        private void button1_Click(object sender, EventArgs e)
        {
            // 取出textBox2中的内容,去除前后的空格
            string line = textBox2.Text.Trim();
            
            // 如果输入的内容为空,则退出
            if (string.IsNullOrEmpty(line)) return;
            
            // 在textBox1中显示输入内容
            // 注意!使用 Environment.NewLine 实现换行
            textBox1.AppendText(line + Environment.NewLine);
            
            // 准备再次输入
            textBox1.Text = string.Empty;
        }
    }
}

(4)运行效果

回答2:

string hebe = @"C:\pathlog.txt";
File.AppendAllText(hebe, comboBox2.Text+"\r\n" );

回答3:

/n...你试试看,我记不太清楚了