TextBox selection : TextBox « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » TextBox 
23.17.8.TextBox selection
TextBox selection
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

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

    private void showButton_Click(object sender, EventArgs e)
    {
        if (inputBox.SelectionLength == 0)
        {
            MessageBox.Show("You need to select something inside the text box first");
        }
        else
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append(String.Format("The input box contains {0}\n", inputBox.Text));
            builder.Append(String.Format("You have selected {0} characters, starting at {1}\n", inputBox.SelectionLength, inputBox.SelectionStart));
            builder.Append(String.Format("The selection is {0}", inputBox.SelectedText));

            MessageBox.Show(builder.ToString());

        }
    }
}
partial class Form1
{
    private void InitializeComponent()
    {
        this.inputBox = new System.Windows.Forms.TextBox();
        this.showButton = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // inputBox
        // 
        this.inputBox.Location = new System.Drawing.Point(1615);
        this.inputBox.Name = "inputBox";
        this.inputBox.Size = new System.Drawing.Size(42122);
        this.inputBox.TabIndex = 0;
        // 
        // showButton
        // 
        this.showButton.Location = new System.Drawing.Point(31146);
        this.showButton.Name = "showButton";
        this.showButton.Size = new System.Drawing.Size(12627);
        this.showButton.TabIndex = 1;
        this.showButton.Text = "Show selection";
        this.showButton.Click += new System.EventHandler(this.showButton_Click);
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(615);
        this.ClientSize = new System.Drawing.Size(44987);
        this.Controls.Add(this.showButton);
        this.Controls.Add(this.inputBox);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private System.Windows.Forms.TextBox inputBox;
    private System.Windows.Forms.Button showButton;
}
public class TextBoxSelection
{
    [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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.