[英]How to set the insertion point at the first line of textbox?
How to set the insertion point at the first line of text box when I press enter if null value? 如果为null值,按Enter键时如何在文本框的第一行设置插入点?
I used 我用了
if (e.KeyChar == (char)Keys.Enter)
{
//some text
textBox1.Text = "";
textBox1.Focus();
}
Nothing happens though. 什么也没发生。
Reason for failure: you are assigning Empty
String
into TextBox
instead of Comparing 失败原因:您将
Empty
String
分配给TextBox
而不是Comparing
Solution : You need to check for Empty String
using if
Condition. 解决方案:您需要使用
if
Condition检查是否为Empty String
。
Try This: 尝试这个:
if (e.KeyChar == (char)Keys.Enter)
{
//some text
if(textBox1.Text.Trim().Equals(""))
textBox1.Focus();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true;
if (textBox1.Text.Trim() == "")
{
MessageBox.Show("Empty");
textBox1.Focus();
}
else
textBox2.Focus();
}
}
if(e.KeyChar == (char)Keys.Enter)
{
if(string.IsNullOrEmpty(textBox1.Text))
textBox1.Focus();
else
textBox1.Text = string.Empty;
}
Make it simple and useful 使其简单实用
if (e.KeyChar == (char)Keys.Enter)
{
textBox1.Text = "";
textBox1.SelectionStart = 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.