Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Selection of text in textbox

Selection of text in textbox

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KrunalC
    wrote on last edited by
    #1

    In my project, I have created custom text box deriving it from text box control. I would like to select the text present when focus is received in the custom text box. Focus can be received in custom control either pressing tab or clicking into the textbox control. In my custom control I have overridden the getfocus behaviour and placed following code : protected override void OnGotFocus(EventArgs e) { if (!ReadOnly) { BackColor = _backHighLightColor; // just to change back color // to selected complete text in the textbox when focus received SelectAll(); } } With above line of code, text in the custom textbox control gets selected when I'm getting the foucs into the control using the tab but doesn't select the text when i click into the custom text box. I could write the above code in the click event of custom control by overriding the base implementation for click but i don't want to do that as click event of my custom control is intended to do some other functionality as well. Can anyone help me with this? regards, KC

    A 1 Reply Last reply
    0
    • K KrunalC

      In my project, I have created custom text box deriving it from text box control. I would like to select the text present when focus is received in the custom text box. Focus can be received in custom control either pressing tab or clicking into the textbox control. In my custom control I have overridden the getfocus behaviour and placed following code : protected override void OnGotFocus(EventArgs e) { if (!ReadOnly) { BackColor = _backHighLightColor; // just to change back color // to selected complete text in the textbox when focus received SelectAll(); } } With above line of code, text in the custom textbox control gets selected when I'm getting the foucs into the control using the tab but doesn't select the text when i click into the custom text box. I could write the above code in the click event of custom control by overriding the base implementation for click but i don't want to do that as click event of my custom control is intended to do some other functionality as well. Can anyone help me with this? regards, KC

      A Offline
      A Offline
      Andrew Lygin
      wrote on last edited by
      #2

      Hi, KC.

      KrunalC wrote:

      I have overridden the getfocus behaviour

      It's better to override OnEnter() method because you have a side effect with your code in OnGotFocus() (1. enter some text into your textbox; 2. go to an another form 3; go back to your form. After this your text will be fully selected. It's not a correct behaviour (although IE uses it in the address bar)).

      KrunalC wrote:

      I could write the above code in the click event of custom control by overriding the base implementation for click but i don't want to do that

      I think you have to do that. But override OnMouseDown() instead of OnClick(). It will be more common behaviour. And you'll have to add some logic there to prevent selection when control already has focus.

      K 1 Reply Last reply
      0
      • A Andrew Lygin

        Hi, KC.

        KrunalC wrote:

        I have overridden the getfocus behaviour

        It's better to override OnEnter() method because you have a side effect with your code in OnGotFocus() (1. enter some text into your textbox; 2. go to an another form 3; go back to your form. After this your text will be fully selected. It's not a correct behaviour (although IE uses it in the address bar)).

        KrunalC wrote:

        I could write the above code in the click event of custom control by overriding the base implementation for click but i don't want to do that

        I think you have to do that. But override OnMouseDown() instead of OnClick(). It will be more common behaviour. And you'll have to add some logic there to prevent selection when control already has focus.

        K Offline
        K Offline
        KrunalC
        wrote on last edited by
        #3

        Andrew, Thanks for your reply. I have gone through your reply but I'm afraid it will not serve the purpose for me. I got your argument on why I should override OnEnter() rather than OnGotFocus(). But still I will have to override the Click or Mousedown. Basically I want to avoid this. Also I would like to know why such behaviour is there. Why focus i received by clicking the control not giving the expected behaviour as got foucs. I would appreciate if someone can explain me the behaviour i.e. what is the difference when focus is received by pressing the tab key or when focus is received by clicking into the control. Thanks, KC

        A 1 Reply Last reply
        0
        • K KrunalC

          Andrew, Thanks for your reply. I have gone through your reply but I'm afraid it will not serve the purpose for me. I got your argument on why I should override OnEnter() rather than OnGotFocus(). But still I will have to override the Click or Mousedown. Basically I want to avoid this. Also I would like to know why such behaviour is there. Why focus i received by clicking the control not giving the expected behaviour as got foucs. I would appreciate if someone can explain me the behaviour i.e. what is the difference when focus is received by pressing the tab key or when focus is received by clicking into the control. Thanks, KC

          A Offline
          A Offline
          Andrew Lygin
          wrote on last edited by
          #4

          I think the difference between tab-focus and mouseclick-focus is: 1. When control receives focus by pressing tab the only one action occurs -- receiving focus. At this moment you select all the text and cursor automatically goes to the end of the text. 2. When control receives focus by clicking into the control there are two actions: At first you have behaviour as described at 1. But then control must move cursor to the position where you clicked within the control. And this movement clears the selection. (It looks like you select control with tab and then press arrow key to move cursor within the control.) I don't think you can prevent this behaviour for the common winform textbox without overriding mouse events.

          K 1 Reply Last reply
          0
          • A Andrew Lygin

            I think the difference between tab-focus and mouseclick-focus is: 1. When control receives focus by pressing tab the only one action occurs -- receiving focus. At this moment you select all the text and cursor automatically goes to the end of the text. 2. When control receives focus by clicking into the control there are two actions: At first you have behaviour as described at 1. But then control must move cursor to the position where you clicked within the control. And this movement clears the selection. (It looks like you select control with tab and then press arrow key to move cursor within the control.) I don't think you can prevent this behaviour for the common winform textbox without overriding mouse events.

            K Offline
            K Offline
            KrunalC
            wrote on last edited by
            #5

            Andrew, I'm getting what you are saying. I searched the web for this problem and my finding is also in line with what you have said in your reply. I think I will not be able to get the desired behaviour without overriding the mouseevent or click event. Thanks a lot for replying my question. regards, KC

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups