8

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?

4
  • For each TextBox try SelectionStart = i and SelectionLength = 0, where i is the desired cursor position.
    – kol
    Nov 21, 2011 at 2:49
  • How can I set for all textboxs? Cause' I have a lot of textboxs :(
    – vNext
    Nov 21, 2011 at 2:57
  • 1
    Use a loop :) Forms have a property called Controls, which is a ControlCollection. You can iterate over its elements using foreach (var c in form.Controls), test whether the current control is a TextBox using TextBox tb = c as TextBox; if (tb != null) ... and set the cursor position for tb.
    – kol
    Nov 21, 2011 at 3:01
  • It means there are no way to set this. Ex a property of win form? We must do this manually :(
    – vNext
    Nov 21, 2011 at 3:11

3 Answers 3

12

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);
  }
}
5
  • kol, great, but won't it work if you do the following with the foreach: foreach (TextBox control in form.Controls), then you won't need this: var textBox = control as TextBox; Note that I haven't tested it yet, but I think it should work. Nov 21, 2011 at 4:43
  • @PavelDonchev Your idea works only if all controls are TextBoxes. If you also have a Button on the form, then you will get an exception saying System.Windows.Forms.Button cannot be converted to System.Windows.Forms.TextBox.
    – kol
    Nov 21, 2011 at 5:04
  • I am not quite sure you are right. I just created a form with a button and a text box, then wrote the following: foreach (TextBox tb in this.Controls) { MessageBox.Show(tb.Name); } And had no problems to execute it and get the name of the textbox on the form, it simply skipped the button. Very interested if it doesn't work for you - why it doesn't work. I tried it on a Windows Forms, Visual Studio 2010, .NET Framework 4 Client Profile (the default settings for me). Sorry for having to write code in the comments, I know it won't appear fine. Nov 21, 2011 at 7:06
  • @PavelDonchev I tested your idea with the above code, with two modifications: (1) added a button (var button = new Button(); form.Controls.Add(button);), and (2) changed the form.Shown event handler delegate to foreach (TextBox textBox in form.Controls) { textBox.Focus(); textBox.SelectionStart = 2; textBox.SelectionLength = 0; }. The latter throws the exception I mentioned above.
    – kol
    Nov 21, 2011 at 8:07
  • You are right. Now I noticed why did it trick me. In the Shown event handler the process gets killed (Visual Studio pops up if you are debugging, showing you the line with the Invalid Cast Exception). If you try to put the code in the Load event handler you will notice that the form will normally load (except it won't work as expected). If you put the code in try .. catch block you will still be able to catch the InvalidCastException. Pretty strange behavior. Nov 21, 2011 at 9:21
9

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;
1

//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;           
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.