如何使用一次移至右边的按钮移动光标 [英] how to move the cursor using a button going to the right one at a time

查看:74
本文介绍了如何使用一次移至右边的按钮移动光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用一次一次向左移一个字符的按钮移动光标..

how to move the cursor using a button going to the left one char at a time..

private void button6_Click(object sender, EventArgs e)
        {
            textBox1.SelectionLength = 0;
            for (int i = 0; i > textBox1.SelectedText.Length; i++ )
            {
                textBox1.SelectionStart = textBox1.Text.Length+1;
                textBox1.Focus();
                textBox1.ScrollToCaret();
            }
        }

推荐答案

现在,我不太确定您要做什么,但这是不行的. br/> 您有一个循环,除非文本框中的字符数为负数,否则不会四舍五入,但是即使yoiu确实绕过了循环,每次将选择"开始都设置在同一位置.

尝试更改循环:使用<"而不是>" -那么它至少会绕过它.

然后取出循环.这可能不是您想要的.

每次按下按钮,您实际上想做什么?


每次单击按钮时,文本光标也会移动.就像在键盘上一样..
例如,当您输入拼写错误的单词时,请按箭头键来编辑错误的字母."



然后,您在按钮单击处理程序中所需要做的就是每次将SelectionStart上下移动一个-显然要注意左端和右端.您将需要Focus调用才能将光标放回到文本框中,否则它将停留在按钮上.
Now, I''m not quite sure what you are trying to do, but that isn''t going to do it.
You have a loop, which you don''t go round unless there are a negative number of characters in the text box, but even if yoiu did go round it, each time you would set the Selection start to the same place.

Try changing your loop: use "<" instead of ">" - then it will at least go round it.

Then take out the loop. It probably isn''t what you want to do.

What are you actually trying to do, each time you press your button?


"everytime you click the button the text cursor also move..just like here in keyboard..
example when you type a word which is wrong spelled you press the arrow key to edit the wrong letter."



Then all you need to do in your button click handler is move the SelectionStart up or down by one each time - looking out for the left and right ends, obviously. You will need the Focus call to put the cursor back in the text box, otherwise it will stay on the button.
private void butLeft_Click(object sender, EventArgs e)
     {
     if (tbData.SelectionStart > 0)
         {
         tbData.SelectionStart--;
         tbData.Focus();
         }
    }

 private void butRight_Click(object sender, EventArgs e)
     {
     if (tbData.SelectionStart < tbData.Text.Length)
         {
         tbData.SelectionStart++;
         tbData.Focus();
         }
     }


您可能不了解Windows中的消息处理:实际上,循环执行的操作一次就完成了,直到最终位置.什么是一次"?实际上,您一次完成所有操作.您想为该动作设置动画吗?

看来您需要使用单独的线程并将光标移动到某个位置并稍有延迟(System.Threading.Thread.Sleep).

一个问题是:您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
You probably don''t understand message processing in Windows: what you do in a loop is actually done all at once to the final position. What''s "one at a time"? You do it "all at a time", in fact. Do you want to animate this motion?

It looks like you need to use a separate thread and move the cursor in one position with some delay (System.Threading.Thread.Sleep).

One problem is: you cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于如何使用一次移至右边的按钮移动光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆