本文介绍了有关游标的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从文本文件读取后,如何使光标返回到文本框的开头

代码在下面

after reading from a text file how can i make the cursor return to the beginning of text box

Code is below

private void textBox1_TextChanged(object sender, EventArgs e)
{
string sql = "insert into items values('" + textBox1.Text + "')";
cmd = new SqlCommand(sql, con);
 if (textBox1.Text.Length > 7)
 {
  textBox1.Text=textBox1.Text.Remove(0, 7);
  if (textBox1.Text.Length == 7)
  {
   try
    {
     con.Open();
     cmd.ExecuteNonQuery();
    }
   catch (SqlException ex)
   {
    MessageBox.Show(ex.ToString());
   }
  con.Close();
  textBox1.SelectionStart = 0;
  textBox1.ScrollToCaret();
 }
}

推荐答案

myTextBox.SelectionStart = 0;


textBox1.SelectionStart = 0;
textBox1.ScrollToCaret();
this.ActiveControl = textBox1;


穆罕默德·艾哈迈德·古达(Mohammed Ahmed Gouda)写道:
Mohammed Ahmed Gouda wrote:

我如何使光标返回到文本框的开头

how can i make the cursor return to the beginning of text box


TextBox1.SelectionStart = 0;



我要从注释中的代码中说,不要更改TextChanged事件中的文本,也不要在更改之前删除处理程序.



From your code in comment, I would say, don''t change the text in TextChanged event, or remove handler before changing.

textBox1.TextChanged -= textBox1_TextChanged;
textBox1.Text=textBox1.Text.Remove(0, 7);
textBox1.TextChanged += textBox1_TextChanged;


这篇关于有关游标的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:53