10

単純なプロパティが不足しているように感じますが、カーソルをテキストボックスの行末に設定できますか?

private void txtNumbersOnly_KeyPress(object sender, KeyPressEventArgs e)
{
   if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == '.' || e.KeyChar == '-')
   {
      TextBox t = (TextBox)sender;
      bool bHandled = false;
      _sCurrentTemp += e.KeyChar;

      if (_sCurrentTemp.Length > 0 && e.KeyChar == '-')
      {
         // '-' only allowed as first char
         bHandled = true;
      }

      if (_sCurrentTemp.StartsWith(Convert.ToString('.')))
      {
         // add '0' in front of decimal point
         t.Text = string.Empty;
         t.Text = '0' + _sCurrentTemp;
         _sCurrentTemp = t.Text; 
         bHandled  = true;
      }

      e.Handled = bHandled;
   }

「。」のテスト後 最初の文字として、カーソルは追加されるテキストの前に移動します。したがって、結果は「0.123」ではなく「1230」になります。自分でカーソルを動かさずに。

これが重複した質問である場合もお詫び申し上げます。

4

5 に答える 5

22
t.SelectionStart = t.Text.Length;
于 2010-05-04T17:06:49.057 に答える
3

WPFでは、次を使用する必要があります:

textBox.Select(textBox.Text.Length,0);

ここで、0 は選択された文字数です

于 2013-06-27T13:30:06.353 に答える
2

テキストボックスにプロパティを設定するSelectionStartと、カーソル位置が制御されます。

于 2010-05-04T17:00:49.517 に答える
2

WPFではなくWinFormsを使用していると仮定します...

void SetToEndOfLine(TextBox tb, int line)
{
   int loc = 0;
   for (int x = 0; x < tb.Lines.Length && tb <= line; x++)
   {
      loc += tb.Lines[x].Length;
   }
   tb.SelectionStart = loc;
}
于 2010-05-04T17:02:07.570 に答える
1

これは役に立ちます。

        private void textBox_TextChanged(object sender, EventArgs e)
    {
        string c = "";
        string d = "0123456789.";
        foreach (char a in textBox.Text)
        {
            if (d.Contains(a))
                c += a;
        }
        textBox.Text = c;
        textBox.SelectionStart = textBox.Text.Length;
    }
于 2015-10-21T16:50:28.710 に答える