开发者

My RichTextBox change backgroundcolor when Enabled is set to false

How can I set the backgroundcolor and fontcolor to开发者_如何学Python be "normal" on a disabled (i.e. Enabled = false) RichTextBox?

Thanks


Windows User Interface guidelines demand that a control that is disabled appears disabled. With the obvious benefit that the user can tell that it won't make sense to keep banging the mouse on the control, trying to set the focus to it. Like all controls in the toolbox, RichTextBox implements this guideline as well. Overriding its painting behavior is not practical. Consider the ReadOnly property.


I would create a new control that inherits from RichTextBox. You could the override the BackColor property to always return something like white for example. Something similar could be done with the font color. Off the top of my head I think you could do something such as:

class CustomRichTextBox : System.Windows.Forms.RichTextBox {
     public override System.Drawing.Color BackColor {
          get { return System.Drawing.Color.White; }
          set { base.BackColor = System.Drawing.Color.White; }
     }
}

Though that may not work because you would probably have to override the OnPaint method to get around default greyed out behavior.

Another option would be to simply use the readonly property instead. ReadOnly is almost the same as enabled = false, except that you can actually still click in the text box (you just can't edit it). When it is readonly, you still have control over the normal color properties without having to override anything.

If you wanted to be even more creative, you could add a delegate to the Enter event of the RichTextBox that set the focus to some other control to prevent the user from even clicking in the box (which enabled doesn't let you do)


Emulate the property of being disabled. Implement a property that when set to false the control won't get focus or all key strokes are ignored. Pretty bizarre in my opinion but the programmer wants what the programmer wants! ;-]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜