Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
after reading from a text file how can i make the cursor return to the beginning of text box

Code is below

C#
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(); 
 } 
}
Posted
Updated 20-Apr-11 17:06pm
v2
Comments
Mohammed Ahmed Gouda 20-Apr-11 5:41am    
the code :

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

Try:
myTextBox.SelectionStart = 0;
 
Share this answer
 
Comments
Mohammed Ahmed Gouda 20-Apr-11 5:32am    
it didnt work
OriginalGriff 20-Apr-11 5:35am    
What did happen? "It didn't work" is not very helpful!
Mohammed Ahmed Gouda 20-Apr-11 5:38am    
the curosr didnt return to the start of textbox
OriginalGriff 20-Apr-11 5:40am    
Did it go anywhere? Did it disappear? Remember we can't see your screen, so we can only work with what you tell us!
Mohammed Ahmed Gouda 20-Apr-11 5:42am    
the cursor remains after the string
try this:

C#
textBox1.SelectionStart = 0;
textBox1.ScrollToCaret();
this.ActiveControl = textBox1;
 
Share this answer
 
v2
Comments
Mohammed Ahmed Gouda 20-Apr-11 5:43am    
it didnt work also may be i wrote the code in wrong place
Michel [mjbohn] 20-Apr-11 5:56am    
I edited my solution. Probably you couldn't see the cursor because textBox was not active
Sergey Alexandrovich Kryukov 20-Apr-11 17:52pm    
My 5, but I must note the last line is redundant, only to convince OP :-)
--SA
Michel [mjbohn] 21-Apr-11 3:50am    
It was indeed just to convince
thanks :)
Mohammed Ahmed Gouda wrote:
how can i make the cursor return to the beginning of text box

TextBox1.SelectionStart = 0;


From your code in comment, I would say, don't change the text in TextChanged event, or remove handler before changing.
C#
textBox1.TextChanged -= textBox1_TextChanged;
textBox1.Text=textBox1.Text.Remove(0, 7);
textBox1.TextChanged += textBox1_TextChanged;
 
Share this answer
 
v5
Comments
Mohammed Ahmed Gouda 20-Apr-11 5:31am    
it didnt work
Prerak Patel 20-Apr-11 5:32am    
What happens? Share the code.
Prerak Patel 20-Apr-11 5:54am    
You shouldn't change the text in textchanged event. Still if you wish, you should unbind event, and then change, then finally bind it.
Mohammed Ahmed Gouda 20-Apr-11 5:58am    
how?
Prerak Patel 20-Apr-11 6:02am    
Check edited part in my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900