简体   繁体   English

如何在文本框的第一行设置插入点?

[英]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.

相关问题 在插入点将“ FontWeightProperty”设置为BOLD - set “FontWeightProperty” to BOLD in the insertion point 禁用文本框的第一行 - Disable first line of textbox Word Interop:如何根据Find命令的结果设置插入点? - Word Interop: How do I set an insertion point based on the results of a Find command? 如何将WinForms TextBox的前几个字符设置为只读? - How to set the first few characters of a WinForms TextBox to Read-Only? 如何设置等于文本框的标签,每个字母都放在单独的行上? - How to set a label equal to a textbox, with every letter on a separate line? 如何将 DataGridView 文本框列设置为多行? - How to set DataGridView textbox column to multi-line? 在 Canvas WPF 上绘制一条线后,在 A 点和 B 点上显示文本框 - Show textbox on point A and point B after drawing a Line on Canvas WPF 如何将第二个文本框中的日期设置为第一个文本框的日期 + 1 - How can I set Date in second TextBox to first TextBox's day + 1 在C#中的数字文本框中,如何防止仅将小数点放在第一位? - In C#, in a numerical textbox, how do I prevent a decimal point being placed in as the first digit only? 如何根据asp.net中的第一个文本框日期在第二个文本框中设置日期 - how to set date in second textbox according to first textbox date in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM