简体   繁体   English

如何使用C#在按钮单击事件上的textBox中设置光标位置?

[英]How to set cursor position in textBox on button click event using c#?

I have two text boxes and a button in windows form app using c# . 我在windows form app使用c#有两个text boxes和一个按钮。

when i enter text in txtbox1 and send that text to txtbox2 on button click event , the cursor also goes to txtbox2 . 当我在txtbox1输入文本并将button click event上的文本发送到txtbox2时,光标也将移至txtbox2

My requirement is when i send data from textBox1 to textBox2 on buton click event the cursor should remain in textBox1 . 我的要求是,当在click事件上将数据从textBox1发送到textBox2时,光标应保留在textBox1

In the buttonClick event just add at the end: 在buttonClick事件中,只需在末尾添加:

textBox1.Focus();

How to: Set Focus in a TextBox Control 如何:在TextBox控件中设置焦点

Any reasons why dont set cursor position manually after the click event? 有什么原因导致单击事件后不手动设置光标位置?

 Cursor.Position = new Point(MyFirstTextbox.Location.X -5, MyFirstTextbox.Location.Y -5);

Also Control.Focus() helps you. Control.Focus()也可以帮助您。

On button click write at last: 在按钮上单击最后写入:

textbox1.focus()

focus is the method you need to use. focus是您需要使用的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM