在C#窗口的文本框中更改光标位置

在C#窗口的文本框中更改光标位置,c#,winforms,cursor,C#,Winforms,Cursor,我有一个winform,它加载MDI子winform。子winform中的所有文本框都将光标停留在左侧,我无法将其移动到其他位置,除非我选择所有文本并重新输入。如何启用此选项,使光标可以使用鼠标停留在任何位置?在下面的示例中,光标将位于表单每个文本框中的第二个字符之后。焦点将集中在最后一个文本框上,但通过反复按TAB键,可以验证是否已为每个文本框设置了光标位置 using System; using System.Windows.Forms; public class Program {

我有一个winform,它加载MDI子winform。子winform中的所有文本框都将光标停留在左侧,我无法将其移动到其他位置,除非我选择所有文本并重新输入。如何启用此选项,使光标可以使用鼠标停留在任何位置?

在下面的示例中,光标将位于表单每个文本框中的第二个字符之后。焦点将集中在最后一个文本框上,但通过反复按TAB键,可以验证是否已为每个文本框设置了光标位置

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

//Windows窗体掩码文本框输入从右到左的testex,带“#######”掩码小数(6,4)

私有void maskedtextboxmaskrtfu KeyPress(对象发送方,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;           
    }
var maskedTextBox=(maskedTextBox)发送方;
var contLit=maskedTextBox.Text.Where(ch=>“,”.Contains(ch)).Count();
var value=maskedTextBox.Text.Replace(“.”,“”)。Replace(“,”)+e.KeyChar;
if(value.Length>=maskedTextBox.Mask.Length-contLit)
值=值。子字符串(1);
其他的
while(value.Length
试试这个,希望对你有所帮助;)


对于每个文本框,请尝试选择Start=i和SelectionLength=0,其中i是所需的光标位置。如何设置所有文本框?因为我有很多文本框:(使用循环:)表单有一个名为Controls的属性,这是一个ControlCollection。您可以使用
foreach(form.Controls中的var c)
对其元素进行迭代,使用
TextBox tb=c作为TextBox测试当前控件是否是一个TextBox;如果(tb!=null).
并为
tb
设置光标位置。这意味着无法进行设置。Ex是win形式的属性吗?我们必须手动执行:(我发布了一个示例,请参见下面的答案。kol,很好,但是如果您使用foreach:foreach(form.Controls中的文本框控件)执行以下操作,它会不会工作,那么您就不需要这个:var textBox=控件作为textBox;请注意,我还没有测试它,但我认为它应该可以工作。@PavelDonchev只有当所有控件都是textBox时,您的想法才会起作用。如果您在表单上还有一个按钮,则会出现异常,说明System.Windows.Forms.Button无法转换为System.Windows.Forms.textBox。我不太确定你是对的。我只是创建了一个带有按钮和文本框的表单,然后写了以下内容:foreach(TextBox tb in this.Controls){MessageBox.Show(tb.Name);}执行它并获取表单上文本框的名称没有问题,它只是跳过了按钮。如果它对您不起作用,您会非常感兴趣-为什么它不起作用。我在Windows窗体Visual Studio 2010、.NET Framework 4客户端配置文件上试过它(我的默认设置).PavelDonchev我用上面的代码测试了你的想法,做了两个修改:(1)添加了一个按钮(
var button=new button();form.Controls.Add(button);
),以及(2)将
表单更改为
foreach(form.Controls中的TextBox TextBox){TextBox.Focus();TextBox.SelectionStart=2;TextBox.SelectionLength=0;}
。后者引发了我上面提到的异常。你是对的。现在我注意到它为什么欺骗了我。在显示的事件处理程序中,进程被终止(如果正在调试,则会弹出Visual Studio,向您显示包含无效强制转换异常的行)。如果尝试将代码放入加载事件处理程序,您将注意到表单将正常加载(除非它无法按预期工作)。如果您将代码放入try..catch块,您仍然可以捕获InvalidCastException。非常奇怪的行为。
//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;