textbox / richtextbox光标移动到零位置

时间:2014-04-16 07:01:24

标签: c# winforms

我试图像键盘(键盘)那样编写一个字母数字手机。所以,我把字母放在文本框中,但每次按下我的一个按钮,文本框光标移动位置零并在那里写表格。

我尝试选择已更改的事件但由于文本框本身更改为零而无效,我的selection_changed代码从那里处理。

默认情况下,我不需要将光标位置更改为零。

继承人我的表格看起来像。

enter image description here

一键按MyCode:

private void button3_MouseClick(object sender, MouseEventArgs e)
{
    MyTimer.Start();

    if (e.Button == MouseButtons.Left)
    {
        clicked += e.Clicks;
    }

    switch (clicked % 2)
    {
        case 1:
        {
            if (clicked > 1)
            {
                if (selectionstart < textBox1.Text.Length)
                {
                    string myString = textBox1.Text.ToString();

                    StringBuilder sb = new StringBuilder();

                    string part1 = myString.Substring(0, selectionstart);

                    int length = myString.Length - selectionstart - 1;
                    string part2 = myString.Substring(selectionstart + 1, length);
                    sb.Append(part1);
                    sb.Append("a");
                    sb.Append(part2);
                    textBox1.Text = sb.ToString();
                    break;
                }
                else
                {
                    string myString = textBox1.Text.ToString();
                    myString = myString.Substring(0, myString.Length - 1);
                    textBox1.Text = myString;
                    textBox1.AppendText("a");
                    break;
                }
            }
            else
            {
                if (selectionstart < textBox1.Text.Length)
                {
                    string myString = textBox1.Text.ToString();

                    StringBuilder sb = new StringBuilder();

                    string part1 = myString.Substring(0, selectionstart);

                    int length = myString.Length - selectionstart;
                    string part2 = myString.Substring(selectionstart, length);
                    sb.Append(part1);
                    sb.Append("a");
                    sb.Append(part2);
                    textBox1.Text = sb.ToString();
                    break;
                }
                else
                {
                    string myString = textBox1.Text.ToString();
                    myString = myString.Substring(0, myString.Length - 1);
                    textBox1.Text = myString;
                    textBox1.AppendText("a");
                    break;
                }
            }
            break;
        }
        case 0:
        {
            if (clicked > 1)
            {
                if (selectionstart < textBox1.Text.Length)
                {
                    string myString = textBox1.Text.ToString();

                    StringBuilder sb = new StringBuilder();

                    string part1 = myString.Substring(0, selectionstart);

                    int length = myString.Length - selectionstart - 1;
                    string part2 = myString.Substring(selectionstart + 1, length);
                    sb.Append(part1);
                    sb.Append("b");
                    sb.Append(part2);
                    textBox1.Text = sb.ToString();
                    break;
                }
                else
                {
                    string myString = textBox1.Text.ToString();
                    myString = myString.Substring(0, myString.Length - 1);
                    textBox1.Text = myString;
                    textBox1.AppendText("b");
                    break;
                }
            }
            else
            {
                if (selectionstart < textBox1.Text.Length)
                {
                    string myString = textBox1.Text.ToString();

                    StringBuilder sb = new StringBuilder();

                    string part1 = myString.Substring(0, selectionstart);

                    int length = myString.Length - selectionstart;
                    string part2 = myString.Substring(selectionstart, length);
                    sb.Append(part1);
                    sb.Append("b");
                    sb.Append(part2);
                    textBox1.Text = sb.ToString();
                    break;
                }
                else
                {
                    string myString = textBox1.Text.ToString();
                    myString = myString.    Substring(0, myString.Length - 1);
                    textBox1.Text = myString;
                    textBox1.AppendText("b");
                    break;
                }
            }
            break;
        }
    }
}

0 个答案:

没有答案