Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I might appear a simple questions but it is not as. I want to capture a click event in the textbox in winforms. After click I let my mouse stuck there but move position of the curosr to the start of the textbox programmatically. I have tried click and Mouse click, enter mouse enter event of textbox but no help. plz guide.
Posted
Comments
touseef4pk 6-Jul-12 7:10am    
Nobody has its answer ?
[no name] 6-Jul-12 7:18am    
Good grief. Have some patience. Do you realize that half the world is asleep? Volunteers at this site are not at your beck and call.
But since I am here... "but no help" is not an acceptable description of any sort of a problem. No such word as "plz". Try GotFocus or OnFocus I forget which.
touseef4pk 6-Jul-12 7:29am    
if there is no acceptable solution then what is point in saying no help :( There is no onfocus or gotfocus event in winforms C#.
[no name] 6-Jul-12 7:37am    
The event is called GotFocus in winforms. I know this because at 630am I ran Visual Studio and created a brand new Winforms project, added a textbox and then went through the list of events that the textbox has. Kind of new at this research thing aren't you?
Shemeer NS 6-Jul-12 7:56am    
gotFocus and Lost Focus are not available in the property window ... just add the handler then it will work for you... I have added a solution for you...please check

Hey angry young man...

sorry for late reply..


C#
public Form1()
{
    InitializeComponent();
    textBox1.Text = "shemeer"; // setting a default value
    textBox1.GotFocus += new EventHandler(textBox1_GotFocus); // add the eventhandler for GotFocus..this is not available in the property/event window
}

private void textBox1_GotFocus(object sender, EventArgs e)
{
    textBox1.Select(0, 0); //setting cursor to the begining just after getting focus
}

private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
    textBox1.Select(0, 0);//setting cursor to the begining  on a mouse click

}



I hope this will resolve your problem.... thanks
 
Share this answer
 
The event is called "Enter".
Somewhere early in your code, subscribe for the event:
C#
myTextBox.Enter+=new EventHandler(myTextBox_Enter);

In the event handler, do e.g:
C#
TextBox aTextBox = sender as TextBox;
aTextBox.SelectionStart=0;
aTextBox.SelectionLength=0;
 
Share this answer
 
Hi,

In Click event of text box, write:

TextBox1.Focus();

Regards,
Raghu...
 
Share this 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