Making a form read only

dpatfield66

Well-known member
Joined
Apr 6, 2006
Messages
136
Programming Experience
5-10
How can I make a form read only, but still allow for a close button?
 
Ok, I figured out that I can just as the control box back with minimum/maximum disabled, but I still don't know how (other than complex looping through controls) to make an entire form read only, if a record is locked, versus editable if the record is not locked.
 
through my looping code, that's effectively what I'm doing now (although some controls are read-only as opposed to disabled).

I'd prefer all controls to be read-only, so the user can see the original font and background colors, but just can't edit anything. I don't really want the user to see a greyish font.

One problem I have is that I cannot get the combo boxes to read only, only disabled. Any suggestions for this.

However, the MAIN question I had was...is there some way to just select a form property or method that puts all controls as read-only?

In Access, I used to be able to open a form as Read Only. I can't see where I can do that in .NET, however.
 
Textbox1.ReadOnly = True
Textbox1.BackColor = SystemColors.Window

if you're using a giant loop to go through the entire form's collection, once you've checked to see that it's a textbox just set it's ReadOnly property to True and it's BackColor to SystemColors.Window
 
Thanks for the reply, but the textboxes aren't the problem.
It's the comboboxes that don't have a read-only property.
I don't want to disable them, but rather just lock them. Not just the text box portion of the combo box, but the pull down as well.

And my numeric up downs are read only, but I can't keep the user from using the updown buttons. I can stop the updown button on the keyboard, but not the screen.
 
Yes it still allow user to enter text. So to avoid user enter text in combo box keypress event put e.Handled = True so what user input will not display out.
 
This sounds interesting. But will it just hide the input? That means that the value or Text of the comboBox will still transfer over to my SQL table, correct? Or show up on a report?

I want them to not be able to enter anything in the combobox, OR select anything. And if I can do that with the entire form instead of looping through controls, that would be great! There's gotta be a way to make a form read only (just like I could do in Access), right?
 
As far as the combo box goes, set the .DropDownStyle = ComboBoxStyle.DropDownList . Then no text can be entered and only the items listed can be selected.

To make it 'ReadOnly' change the .MaxDropDownItems = 1 and set the first item to whatever item should be ReadOnly. This way, when the combo drops down only one item is available and that's the item you want.

In my opinion, if you want to make a combobox 'ReadOnly' then dont use one at all. Instead, have a ReadOnly textbox or a label (with a white BackColor and border style of FixedSingle). If you use a textbox, set its location and size to that of the combobox and set the combobox to invisible. This is much better usability because otherwise you can confuse users and it looks unprofessional. The label is by far the best option unless you want to be able to highlight and copy text.

As far as making an entire form ReadOnly, that's alot of work (not difficult but god is it tedious!). But if you are trying to allow for ReadOnly as well as Editable then choose your controls and style wisely! How about having two panels which are the size of the form. One is for the editable and one is for the ReadOnly. Its alot easier than having to iterate through controls and changing their properties. Just change the visible and ur sorted - no muss no fuss.
 
Thanks, these are great ideas.
I'll probably try the maxdroppeddown items only because these forms have a gazillion comboboxes! I'm coming into a preexisting development, so I have to somewhat go with what's there.

I've already got all the items (except combos) on the form read only via looping code(textboxes, numeric updowns, etc...), but I was hoping you could just make a single form read-only or non-editable, like in Access.
 
I experimented with the maxdroppeddownitems, but got 2 issues:

1. It shows only the one value, but provides some kind of updown control that still allows you to select the other values.

2. What do you do when they initially didn't have a value in there? Now they can select the 1 item? Or can you do maxdropped down items = 0? I didn't try that one.

I like the textbox/label idea if I didn't have so many darned comboboxes.
It's amazing...they allow for a read-only textbox, you think there'd be code that doesn't allow for entering text AND selecting any values.

In MS Access, you could simply LOCK the control, not disable, and no one could select a value nor could they enter one in there.

I know with numericupdown, I set them to read only, and then have to disable the keyboard updown feature, in addition to setting the increment to 0 so the value doesn't move when you hit the updown controls.
 
This may not be the easiest way but I have your solution that works perfectly.....

Add a class to your project and title it CheckBox. Then Inherit the checkbox class and override the OnClick Method. Mark out the MyBase.OnClick so that it dosn't get called. The checkbox will be shown as normal but click events will not happen at all. To Implement the class do these simple steps.
Build your project.
Goto Toolbox and at the top you will see the control class you built. If you used the name Checkbox then it will read Checkbox but the simbol will be a gear icon.
Double click it to add it to the form. It will look and act like a real check box except it won't work. If you want to make it work later then add a procedure or Property to the Class you wrote to allow it to call the MyBase.OnClick

VB.NET:
[SIZE=2][COLOR=#0000ff]
Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] CheckBox
[/SIZE][SIZE=2][COLOR=#0000ff]Inherits[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.CheckBox
[/SIZE][SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Overrides[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] OnClick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs)
[/SIZE][SIZE=2][COLOR=#008000]' MyBase.OnClick(e)
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] InitializeComponent()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].SuspendLayout()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ResumeLayout([/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
That new class looks interesting. You can definately make a ComboBox ReadOnly by extending the class. For starters have:

Public Class ReadOnlyComboBox
Inherits System.windows.forms.ComboBox
Private ReadOnlyValue As Boolean = False
Public Property IsReadOnly() As Boolean
Get
Return Me.ReadOnlyValue
End Get
Set(ByVal value As Boolean)
Me.ReadOnlyValue = value
End Set
End Property
End Class

Then just override the methods which cause the dropdown menu to display. So If ReadOnlyValue Then fire event otherwise dont.

The number of MaxDropDownItems must be greater than 1. I see what you mean about selecting other items. I'm sure there must be a way to prevent that but I unfortunately I dont know how.
 
Back
Top