How to add "OnFocus" event in Visual Studio 2005
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: How to add "OnFocus" event in Visual Studio 2005

  1. #1
    Join Date
    Dec 2009
    Posts
    11

    Question How to add "OnFocus" event in Visual Studio 2005

    Quick question about Visual Studio 2005 IDE.

    When I double-click a control in the design window, the IDE automatically creates a method for the default control event. But what about tons of the other events, how can I catch them?

    For instance, I want to create a method for the event "OnFocus". How can I do that? I remember a pull-down menu in which all of the events where listed, but not sure of it...

    Thanks in advance.

  2. #2
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    422

    Re: How to add "OnFocus" event in Visual Studio 2005

    instead of double-clicking just select the control and right click and select 'properties' - there you might the event lists in the datagrid control property

  3. #3
    Join Date
    Dec 2009
    Posts
    11

    Question Re: How to add "OnFocus" event in Visual Studio 2005

    Quote Originally Posted by vcdebugger View Post
    instead of double-clicking just select the control and right click and select 'properties' - there you might the event lists in the datagrid control property
    Rather than, I found this solution and it works just fine:

    http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx
    Title in page: To create an event handler in the Properties window

    Using this article I created the following method:
    Code:
    private void txtIpB_OnFocus(object sender, EventArgs e)
    {
    	txtIpB.SelectionStart = 0;
    	txtIpB.SelectionLength = txtIpB.Text.Length;
    }
    However, this doesn't work.

    And neither this one works:
    Code:
    private void txtIpB_OnFocus(object sender, EventArgs e)
    {
    	txtIpB.SelectAll();
    }
    My purpose is to select all the text in a TextBox when the control catches focus.
    How can I do that?

  4. #4
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    422

    Re: How to add "OnFocus" event in Visual Studio 2005

    hoe do you know whether it is working or not ? just put a break point that function and see if the control comes there... and assign the text in that control to some string and see if the text present in that control gets updated to that string...

    Code:
     
      string text =  txtIpB.Text;

  5. #5
    Join Date
    Dec 2009
    Posts
    11

    Thumbs up Re: How to add "OnFocus" event in Visual Studio 2005

    Quote Originally Posted by vcdebugger View Post
    hoe do you know whether it is working or not ? just put a break point that function and see if the control comes there... and assign the text in that control to some string and see if the text present in that control gets updated to that string...

    Code:
     
      string text =  txtIpB.Text;
    Thank you for your reply.

    I put a break point inside txtIpB_OnFocus() to see that this method is indeed called when I focus on the control. And saw that the "txtIpB.Text" contains the string control has as it was expected.

    So, how can I select the text in this edit control?

  6. #6
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    422

    Re: How to add "OnFocus" event in Visual Studio 2005

    there is no need of selecting , the property txtIpB.Text itself contains the text you want to select...

  7. #7
    Join Date
    Dec 2009
    Posts
    11

    Resolved Re: How to add "OnFocus" event in Visual Studio 2005

    Quote Originally Posted by vcdebugger View Post
    there is no need of selecting , the property txtIpB.Text itself contains the text you want to select...
    I want to make it selected, so that when user clicks on it he can immediately start typing new data without bothering to delete the old one, like in the web forms implemented by JavaScript.

    Thanks in advance.

  8. #8
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    422

    Re: How to add "OnFocus" event in Visual Studio 2005

    by default when the focus comes to that control automatically all the text inside that should get selected with what I have observed some years back in vc6.. not sure of C#..

    anyways there should be some property for edit box which makes it EDITABLE so that all the text get selected and user can start typing so that old text gets erased automatically... right click on text box and check the properties referring to option - *EDITABLE ** NON-EDITABLE

  9. #9
    Join Date
    Dec 2009
    Posts
    11

    Re: How to add "OnFocus" event in Visual Studio 2005

    The entire text in the control is selected only if you focus the control by means of pressing the TAB key. The text is not selected if the control catches focus by mouse click.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center