none
How to un-select a windows form control RRS feed

  • Question

  • By default the first control on a windows form is outlined in blue. If you click on a different control the new control is outlined in blue. Is there a way to make all my controls not have this blue outline? The project that I am working would look a lot better if I could make this blue outline go away.
    • Moved by CoolDadTx Friday, July 3, 2020 2:30 PM Winforms related
    Thursday, July 2, 2020 8:59 PM

Answers

  • Hi LavaCreeperKing,

    Thank you for posting here.

    You can also cancel the style when the control has focus in this way.

        public class NoFocusCueButton : Button
        {
            public NoFocusCueButton() : base()
            {
                this.SetStyle(ControlStyles.Selectable, false);
            }
    
            protected override bool ShowFocusCues
            {
                get
                {
                    return false;
                }
            }
        }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 3, 2020 1:54 AM

All replies

  • You can follow this:

    https://stackoverflow.com/questions/9399215/c-sharp-winforms-custom-button-unwanted-border-when-form-unselected

    However, please consider people who are more comfortable navigate the controls with tab button.


    • Edited by cheong00 Friday, July 3, 2020 1:42 AM
    Friday, July 3, 2020 1:41 AM
  • Hi LavaCreeperKing,

    Thank you for posting here.

    You can also cancel the style when the control has focus in this way.

        public class NoFocusCueButton : Button
        {
            public NoFocusCueButton() : base()
            {
                this.SetStyle(ControlStyles.Selectable, false);
            }
    
            protected override bool ShowFocusCues
            {
                get
                {
                    return false;
                }
            }
        }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 3, 2020 1:54 AM
  • Thanks this helped. Now is there a way to prevent a user from being able to click on a textbox?

    When I use the code you gave me on my custom textbox the textbox can still be selected when clicked on. I want to make a read only textbox that can't be interacted with, it will only display text. I did set the read only property on the textbox but even with that set you can still click on it and it gets selected and displays a text editing cursor even though you can't edit any text in it. 

    The project I am working on is a fun personal project I have no intention to make it public.

    Friday, July 3, 2020 6:15 PM
  • Thanks this helped. Now is there a way to prevent a user from being able to click on a textbox?

    When I use the code you gave me on my custom textbox the textbox can still be selected when clicked on. I want to make a read only textbox that can't be interacted with, it will only display text. I did set the read only property on the textbox but even with that set you can still click on it and it gets selected and displays a text editing cursor even though you can't edit any text in it. 

    The project I am working on is a fun personal project I have no intention to make it public.

    Why don't you use a Label control ?
    Friday, July 3, 2020 6:21 PM
  • Textbox inherits from control, so you can set the Enable property to false. But as mentioned in other comments you could also use a Label. Since you are not writing in the textbox
    Monday, July 6, 2020 3:25 AM
  • Textbox inherits from control, so you can set the Enable property to false. But as mentioned in other comments you could also use a Label. Since you are not writing in the textbox
    Not for What I want to do, I need a non editable text box. A label will not work for what I am wanting to create. I was able to make something work by adding a new control that sits off screen then setting the click event of the text box to set focus onto the off screen control.
    Monday, July 6, 2020 8:42 PM

  • Not for What I want to do, I need a non editable text box. A label will not work for what I am wanting to create.

    You wrote :

    "I want to make a read only textbox that can't be interacted with, it will only display text."

    this is the behaviour of a Label....

    Tuesday, July 7, 2020 6:15 AM
  • Except it doesn't look like textbox.

    Sometimes not only the functionality matters, appearance is important too.

    It reminds me about an incident where I have to implement a read-only textbox (on web) that, when clicked will popup some other page. You would argue it had better be replaced with link ("A" tag), but that's a matter of design.
    • Edited by cheong00 Tuesday, July 7, 2020 7:19 AM
    Tuesday, July 7, 2020 7:11 AM
  • Except it doesn't look like textbox.

    A TextBox without border is exactly like a Label (a Label is a Static control) =>

    Tuesday, July 7, 2020 7:31 AM
  • Refer to the uploaded image on the SO question that I linked to, or the animated image of Timon, in order to see the blue outline that the asker talks about.

    It's not the textbox border he wanted to remove.

    Tuesday, July 7, 2020 7:43 AM
  • The OP asked afterwards :

    Now is there a way to prevent a user from being able to click on a textbox?


    Tuesday, July 7, 2020 7:52 AM
  • Yes, and to do that you can just disabling it, right?
    Tuesday, July 7, 2020 7:55 AM
  • Yes, and to do that you can just disabling it, right?

    Yes, but he will have to change default colors, not needed for a Label

    But I don't see exactly what the OP wants....
    Tuesday, July 7, 2020 8:05 AM
  • This is what I want to make. I only want the text box to display text. I don't want to be able to interact with it.

    Wednesday, July 8, 2020 7:28 PM

  • This is what I want to make. I only want the text box to display text. I don't want to be able to interact with it.

    But why don't you want  to use a Label control, which does exactly that ?

    (just add a border, change color, ...)

    Wednesday, July 8, 2020 7:49 PM
  • One thing I could think of is scrollbar, but if need that function then you shouldn't disable Textbox, but use the code provided by Timon.

    To make the Textbox unselectable, maybe you'll want to handle .GetFocus event and set the .ActiveControl of the form to null. (The textbox will still have brief moment on focus when selected)


    • Edited by cheong00 Thursday, July 9, 2020 2:21 AM
    Thursday, July 9, 2020 2:20 AM

  • This is what I want to make. I only want the text box to display text. I don't want to be able to interact with it.

    But why don't you want  to use a Label control, which does exactly that ?

    (just add a border, change color, ...)

    Do Labels have multiline or work wrap support?
    Thursday, July 9, 2020 7:48 PM
  • The best solution I could find was to place a control off screen. I then modified the click event of the TextBox to send focus to the off screen control. 

    If any one wants to know. The feature that I want to replicate came from PyQt. As you can see from the image that I posted the title of the window says Musical GUI Window v3. The first two versions were written in python using PyQt. PyQt had a textbox control that was just like a regular textbox except if you clicked on it nothing would happen. You were only able to highlight text.



    Thursday, July 9, 2020 7:54 PM

  • Do Labels have multiline or work wrap support?

    Yes.

    You can also override OnPaint and draw anything inside



    • Edited by Castorix31 Thursday, July 9, 2020 7:58 PM
    Thursday, July 9, 2020 7:57 PM
  • The best solution I could find was to place a control off screen. I then modified the click event of the TextBox to send focus to the off screen control. 

    If any one wants to know. The feature that I want to replicate came from PyQt. As you can see from the image that I posted the title of the window says Musical GUI Window v3. The first two versions were written in python using PyQt. PyQt had a textbox control that was just like a regular textbox except if you clicked on it nothing would happen. You were only able to highlight text.

    You can also use a ReadOnly TextBox and call HideCaret

    (I just tested on Windows 10)

    Friday, July 10, 2020 7:08 AM