Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change cursor position in textbox in C# Windows

I have a winform which load MDI child winform. All textboxes in child winform always have cursor stay at left side and I can't move it to another position, except I choose all text and re-input.

How can I enable this to make cursor can stay at any position by using mouse?

like image 201
vNext Avatar asked Nov 21 '11 02:11

vNext


People also ask

How to change cursor position in TextBox?

To position the cursor at the end of the contents of a TextBox control, call the Select method and specify the selection start position equal to the length of the text content, and a selection length of 0.

How to put cursor in TextBox c#?

Try textbox1. select() . It's the best approach to bring your cursor to your textbox. It also selects content of the texbox which makes it easier for the user to edit what's inside the textbox.

How do I get the cursor position in HTML?

If you ever had a specific case where you had to retrieve the position of a caret (your cursor's position) inside an HTML input field, you can do so using the selectionStart property of an input's value.


3 Answers

In the following example the cursor will be positioned after the second character in each textbox of the form. The focus will be on the last one, but by pressing the TAB key repeatedly, you can verify that the cursor position has been set for every textbox.

using System;
using System.Windows.Forms;

public class Program
{
  public static void Main()
  {
    var form = new Form();
    form.Text = "Cursor Positioning Test";
    form.Visible = true;
    form.Shown += delegate(object sender, EventArgs args) {
      foreach (var control in form.Controls)
      {
        var textBox = control as TextBox;
        if (textBox != null)
        {
          textBox.Focus();
          textBox.SelectionStart = 2;
          textBox.SelectionLength = 0;
        }
      }
    };

    var textBox1 = new TextBox();
    textBox1.Text = "hello";
    textBox1.Left = 10;
    textBox1.Top = 10;
    form.Controls.Add(textBox1);

    var textBox2 = new TextBox();
    textBox2.Text = "stack";
    textBox2.Left = 10;
    textBox2.Top = 10 + textBox1.Height + 10;
    form.Controls.Add(textBox2);

    var textBox3 = new TextBox();
    textBox3.Text = "overflow";
    textBox3.Left = 10;
    textBox3.Top = 10 + textBox1.Height + 10 + textBox2.Height + 10;
    form.Controls.Add(textBox3);

    Application.Run(form);
  }
}
like image 77
kol Avatar answered Oct 04 '22 17:10

kol


Try This , Hope Helps You ;)

//if you want put cusror at the end of text use this:
TextBox1.SelectionStart = TextBox1.Text.Length;
TextBox1.SelectionLength = 0;
//use this for custom location  int CustomIndex 
TextBox1.SelectionStart = CustomIndex;
TextBox1.SelectionLength = 0;
like image 31
Ali Gh Avatar answered Oct 04 '22 16:10

Ali Gh


//Windows forms maskedTextBox Input Right to Left testex with '##.####' mask decimal(6,4);

private void maskedTextBoxMaskRTF_KeyPress(object sender, KeyPressEventArgs e) {

        var maskedTextBox = (MaskedTextBox)sender;

        var contLit = maskedTextBox.Text.Where(ch => ".,".Contains(ch)).Count();

        var value = maskedTextBox.Text.Replace(".", "").Replace(",", "") + e.KeyChar;

        if (value.Length >= maskedTextBox.Mask.Length - contLit)
            value = value.Substring(1);
        else
            while (value.Length < maskedTextBox.Mask.Length - contLit)
                value = "_" + value;

        maskedTextBox.Text = value;
        maskedTextBox.SelectionStart = maskedTextBox.Mask.Length - 1;
        maskedTextBox.SelectionLength = 1;           
    }
like image 23
Rafael Salgado Avatar answered Oct 04 '22 17:10

Rafael Salgado