簡體   English   中英

如何在文本框的第一行設置插入點?

[英]How to set the insertion point at the first line of textbox?

如果為null值,按Enter鍵時如何在文本框的第一行設置插入點?

我用了

if (e.KeyChar == (char)Keys.Enter)
{
    //some text
    textBox1.Text = "";
    textBox1.Focus();
}

什么也沒發生。

失敗原因:您將Empty String分配給TextBox而不是Comparing

解決方案:您需要使用if Condition檢查是否為Empty String

嘗試這個:

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;
}

使其簡單實用

if (e.KeyChar == (char)Keys.Enter)
{

    textBox1.Text = "";
    textBox1.SelectionStart = 1;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM