How to make a textbox non-selectable using C#

前端 未结 11 1725
轮回少年
轮回少年 2021-02-19 11:09

I\'m using C#/.NET for a Windows Forms application. I have a text box. How can I make the text box unselectable?

I don\'t want to disable the complete textbox.

11条回答
  •  不知归路
    2021-02-19 11:58

    @Mystere Man: You might want a text box that cannot be used all the time. For example, I allow the user to create text boxes on a canvas and drag them around. To prevent them from selecting and moving text when they are dragging I need to disallow user input, and text selection also needs to be disabled because it causes a delay which messes up my drag function. In my application the user can only edit a text box when he has double clicked on it, and must then click outside of the text box to be able to move it again.

    I basically have this code (where t is a TextBox):

    // Prevent text entry
    t.IsReadOnly = true;
    
    // Prevent text selection
    t.Focusable = false;
    

    This behaviour is preferable to disabling the whole control (t.Enabled = false), since that would also stop mousedown and doubleclick events, which would stop dragging and changing from edit to drag mode from working. Not to mention that the text box would go grey.

提交回复
热议问题