Prevent controls from falling out when it's disabled

In winforms.net elements, if we set the Enabled property to false, the control will be grayed out.

In this case, it will become unreadable for many color combinations (because I give options to change the color of the form for the user at runtime).

I can use the ReadOnly property, but it is only available for TextBox controls, and not for other controls such as ComboBox, DateTimePicker, etc.

I just wanted to know if there is any option available so that I can deny control over when it is disabled.

+4
source share
5 answers

This is a sad moment in most usability studies, seeing how the subject breaks away from the mouse and keyboard and does not understand why it does not work. But you can get it if you want. The trick is to put a picture frame in front of the block that shows the bitmap image of the controls in their previously enabled state. They will never understand that they click on the bitmap instead of the actual controls.

It’s best to make a panel so that you can easily disable controls as a group. Add a new class to your project and paste the code shown below. Compilation. Drop the new control on top of the toolbar onto your shape. And put in it the controls that should be disabled. Everything else is automatic, just set the Enabled property to false, and the user will not know what happened:

using System; using System.Drawing; using System.Windows.Forms; class FakeItPanel : Panel { private PictureBox mFakeIt; public new bool Enabled { get { return base.Enabled; } set { if (value) { if (mFakeIt != null) mFakeIt.Dispose(); mFakeIt = null; } else { mFakeIt = new PictureBox(); mFakeIt.Size = this.Size; mFakeIt.Location = this.Location; var bmp = new Bitmap(this.Width, this.Height); this.DrawToBitmap(bmp, new Rectangle(Point.Empty, this.Size)); mFakeIt.Image = bmp; this.Parent.Controls.Add(mFakeIt); this.Parent.Controls.SetChildIndex(mFakeIt, 0); } base.Enabled = value; } } protected override void Dispose(bool disposing) { if (disposing && mFakeIt != null) mFakeIt.Dispose(); base.Dispose(disposing); } } 
+7
source

Actually, you do not want to change the color behavior of the disable / enable controls. Hit Microsoft with updated UX Guides for Windows when you get a chance, as this will give you some layout / design guidelines, but if this is an absolutely must-have feature for your application, you need to either handle the paint control event yourself, or inherit and override his drawing event and paint his own colors on his own to fully control this aspect.

My personal recommendation was to find another possibility, as others mentioned that people expect programs to behave in a certain way, however, if you provide color scheme functions, perhaps limit the parts of the application that can be changed / personalized.

+2
source

I will go out on a limb and guess that you need to make certain fields read-only based on user access rights. This means that a user who has the right to edit certain information can see the combo box, while a user who respects this right will not be able to edit the information, but still can view it and possibly view it.

I would suggest that you choose the right control based on this requirement. If the user can change the selected value in the drop-down list, show the drop-down list. If the user is not allowed to change the value, display a read-only text box that contains the selected value.

One way to simplify the aforementioned solution is to develop some user controls that customize the way the data is displayed in accordance with the property, allowing you to call it the Editable control. That way, you can create a custom control that shows combobox if Editable is true and if Editable is false. And then the corresponding control for datatimepicker, etc.

+2
source

I know this is an old branch, but accidentally discovered an alternative simple solution.

Create a new transparent label control that inherits from the label:

 class TransparentLabel : Label { public TransparentLabel() { this.SetStyle(ControlStyles.Opaque, true); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false); } protected override CreateParams CreateParams { get { CreateParams parms = base.CreateParams; parms.ExStyle |= 0x20; // Turn on WS_EX_TRANSPARENT return parms; } } } 

(I do not claim to have invented this class, it came from somewhere else, probably Hans Passant :-))

In your designer, create a TransparentLabel and give it a name, say tLabel1 . Set the properties as follows:

  • AutoSize is False so you can then expand it to cover all the controls you want to disable (of course, this could be a whole form).
  • Text is empty.
  • Visible - False (or leave it true if you want to see it in the constructor, in this case move it to the end of the Z-sequence so that you can also see the controls that it will cover when the form starts).

In the form, execute this bit of code if you want to disable controls:

 tLabel1.Visible = true; tLabel1.BringToFront(); // if your designer has it at the back 

This works because when it is visible, TransparentLabel receives all mouse clicks and keystrokes, not the basic control, but it is transparent, so you still see the basic controls. Right-clicking does nothing because shortcuts do not have a context menu.

To enable the controls again, simply set the Visible property to false again and everything will come back to life.

+2
source

If you have a "read-only mode" in your program, check the boxes, checkboxes, lists, and text fields as labels.

For example, I did an online survey, which when entering values, its built-in checkboxes, etc., but when someone views a quiz, you see all the answers as tags with the selected value in the form of text. For me, this is the only way to do this without disturbing the normal way of thinking of the UI for both the user and the developer.

If you have a group of selectable options and you want the selection to be visible even in read-only mode, then write down all the options and mark the selected one so that it does not look like an option flag. och.

Here are my 5 cents in this area.

+1
source

All Articles