Count lines in the TextBox and read text in TextBox line by line : TextBox « GUI Windows Forms « C# / CSharp Tutorial






Count lines in the TextBox and read text in TextBox line by line
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void showLinesButton_Click(object sender, EventArgs e)
    {
        StringBuilder lineInfo = new StringBuilder();
        lineInfo.Append("There are " + multiLineBox.Lines.Length.ToString() + " lines.\n");
        foreach (string line in multiLineBox.Lines)
        {
            lineInfo.Append(line + "\n");
        }

        MessageBox.Show(lineInfo.ToString());
    }
}
partial class Form1
{
    private void InitializeComponent()
    {
        this.multiLineBox = new System.Windows.Forms.TextBox();
        this.showLinesButton = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // multiLineBox
        // 
        this.multiLineBox.Location = new System.Drawing.Point(12, 12);
        this.multiLineBox.Multiline = true;
        this.multiLineBox.Name = "multiLineBox";
        this.multiLineBox.Size = new System.Drawing.Size(268, 213);
        this.multiLineBox.TabIndex = 0;
        // 
        // showLinesButton
        // 
        this.showLinesButton.Location = new System.Drawing.Point(205, 231);
        this.showLinesButton.Name = "showLinesButton";
        this.showLinesButton.Size = new System.Drawing.Size(75, 23);
        this.showLinesButton.TabIndex = 1;
        this.showLinesButton.Text = "Show Lines";
        this.showLinesButton.Click += new System.EventHandler(this.showLinesButton_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.showLinesButton);
        this.Controls.Add(this.multiLineBox);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private System.Windows.Forms.TextBox multiLineBox;
    private System.Windows.Forms.Button showLinesButton;
}
public class TextBoxLineCountText
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}








23.17.TextBox
23.17.1.Get input value from TextBox and display it vis a messagebox
23.17.2.TextBox key pressed event
23.17.3.TextBox Modified and TextChangedTextBox Modified and TextChanged
23.17.4.TextBox: Multiline and BorderStyleTextBox: Multiline and BorderStyle
23.17.5.TextBox Keyevent handerTextBox Keyevent hander
23.17.6.TextBox cancel eventTextBox cancel event
23.17.7.Count lines in the TextBox and read text in TextBox line by lineCount lines in the TextBox and read text in TextBox line by line
23.17.8.TextBox selectionTextBox selection
23.17.9.TextBox validationTextBox validation
23.17.10.Add ContextMenu to TextBoxAdd ContextMenu to TextBox
23.17.11.Use Regular Expression to Check the input for a TextBoxUse Regular Expression to Check the input for a TextBox
23.17.12.Multiline TextBoxMultiline TextBox
23.17.13.Simple Editor