textBox Multiline=true 读取每行的内容

本文介绍了三种使用C#从文本框中读取并处理文本的方法:通过Split方法按行分割字符串;利用StringReader类逐行读取;直接通过TextBox的Lines属性获取各行文本。

方法1,(可能存在问题)

            string[] str1 = textBox1.Text.Split('\n');

方法2,

string strLineData;
  using ...(StringReader sr = new StringReader(textbox1.text.Trim()))
...{
    //读取第一行
    strLineData = sr.ReadLine();

    while (!String.IsNullOrEmpty(strLineData))
    ...{
        //程序逻辑部分
        //...

        //读取下一行
        strLineData = sr.ReadLine();
    }
}

方法3,

string[] aryString = (!String.IsNullOrEmpty(textBox1.Text.Trim())) ? textBox1.Lines : null;

 

转载于:https://www.cnblogs.com/mountain2011/archive/2011/05/29/2061861.html

using HslCommunication.ModBus; using HslCommunication; namespace WinFormsApp2 { 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.Size = new System.Drawing.Size(800, 600); this.Text = "展威PLC数据采集工具"; // 创建UI控件 //lblRegisterType = new Label { Text = "寄存器类型:", Location = new System.Drawing.Point(20, 20), Size = new System.Drawing.Size(120, 30) }; //cboRegisterType = new ComboBox { Location = new System.Drawing.Point(150, 20), Size = new System.Drawing.Size(250, 80) }; //cboRegisterType.Items.AddRange(new object[] { "M (辅助继电器)", "X (输入继电器)", "Y (输出继电器)", "D (数据寄存器)" }); //cboRegisterType.SelectedIndex = 0; lblStartAddress = new Label { Text = "ip:", Location = new System.Drawing.Point(20, 60), Size = new System.Drawing.Size(60, 30) }; txtStartAddress = new TextBox { Location = new System.Drawing.Point(80, 60), Size = new System.Drawing.Size(500, 20), Text = "192.168.1.88:502,192.168.1.89:502" }; //lblLength = new Label { Text = "读取长度:", Location = new System.Drawing.Point(20, 100), Size = new System.Drawing.Size(80, 30) }; //txtLength = new TextBox { Location = new System.Drawing.Point(150, 100), Size = new System.Drawing.Size(150, 30), Text = "10" }; // 添加十六进制格式选项 //chkHexFormat = new CheckBox { Text = "十六进制显示", Location = new System.Drawing.Point(270, 50), Size = new System.Drawing.Size(120, 30) }; //btnConnect = new Button { Text = "连接PLC", Location = new System.Drawing.Point(20, 140), Size = new System.Drawing.Size(120, 40) }; btnRead = new Button { Text = "读取数据", Location = new System.Drawing.Point(20, 140), Size = new System.Drawing.Size(120, 40) }; btnDisconnect = new Button { Text = "断开连接", Location = new System.Drawing.Point(130, 140), Size = new System.Drawing.Size(120, 40) }; txtResult = new TextBox { Location = new System.Drawing.Point(20, 180), Multiline = true, Size = new System.Drawing.Size(740, 380), ScrollBars = ScrollBars.Both, Font = new System.Drawing.Font("Consolas", 10) }; // 添加控件到窗体 Controls.Add(lblRegisterType); Controls.Add(cboRegisterType); Controls.Add(lblStartAddress); Controls.Add(txtStartAddress); Controls.Add(lblLength); Controls.Add(txtLength); Controls.Add(chkHexFormat); Controls.Add(btnConnect); Controls.Add(btnRead); Controls.Add(btnDisconnect); Controls.Add(txtResult); } #endregion } } Form1.Designer.cs 代码
06-22
using System; using System.Windows.Forms; namespace KnowledgeTreeEditor { public partial class MainForm : Form { private ComboBox comboBoxSubjects; private TextBox textBoxName, textBoxContent; private Button buttonSave; private TreeNode selectedNode; public MainForm() { InitializeComponent(); InitializeCustomControls(); InitializeKnowledgeTree(); } private void InitializeCustomControls() { // 设置 TreeView treeView1.Dock = DockStyle.Left; treeView1.Width = 250; treeView1.AfterSelect += TreeView1_AfterSelect; // 布局 Panel Panel panel = new Panel(); panel.Dock = DockStyle.Fill; this.Controls.Add(panel); // 知识点名称 Label labelName = new Label() { Text = "知识点名称:" }; textBoxName = new TextBox() { Dock = DockStyle.Top, Width = 200 }; // 知识点内容 Label labelContent = new Label() { Text = "知识点内容:" }; textBoxContent = new TextBox() { Dock = DockStyle.Top, Multiline = true, Height = 100 }; // 所属学科 Label labelSubject = new Label() { Text = "所属学科:" }; comboBoxSubjects = new ComboBox() { Dock = DockStyle.Top }; comboBoxSubjects.Items.AddRange(new string[] { "计算机科学", "数学", "物理", "化学" }); // 保存按钮 buttonSave = new Button() { Text = "保存", Dock = DockStyle.Top }; buttonSave.Click += ButtonSave_Click; // 添加控件到 Panel panel.Controls.Add(buttonSave); panel.Controls.Add(comboBoxSubjects); panel.Controls.Add(labelSubject); panel.Controls.Add(textBoxContent); panel.Controls.Add(labelContent); panel.Controls.Add(textBoxName); panel.Controls.Add(labelName); } private void InitializeKnowledgeTree() { TreeNode root = new TreeNode("知识体系"); TreeNode csNode = new TreeNode("编程语言"); csNode.Nodes.Add(new TreeNode("C#")); csNode.Nodes.Add(new TreeNode("Python")); TreeNode mathNode = new TreeNode("数据结构"); mathNode.Nodes.Add(new TreeNode("数组")); mathNode.Nodes.Add(new TreeNode("链表")); root.Nodes.Add(csNode); root.Nodes.Add(mathNode); treeView1.Nodes.Add(root); } private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e) { selectedNode = e.Node; // 自动填充表单字段 textBoxName.Text = e.Node.Text; textBoxContent.Text = e.Node.Tag is string tag ? tag : ""; comboBoxSubjects.SelectedItem = e.Node.Name; } private void ButtonSave_Click(object sender, EventArgs e) { if (selectedNode != null) { selectedNode.Text = textBoxName.Text; selectedNode.Tag = textBoxContent.Text; selectedNode.Name = comboBoxSubjects.SelectedItem?.ToString(); MessageBox.Show("保存成功!"); } else { MessageBox.Show("请先选择一个知识点节点。"); } } } }优化代码
最新发布
07-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值