Drop Down List and Enabled=false
I have a form with a bunch of text boxes, and drop down list boxes. Now, there are two access states to the page, read only and full access. On read only, I turn the txt box's properties to readOnly = true, instead of enable = false, because the light gray effect with enable=false isn't really eye friendly for "read only".
Now, there is no "read only" option for drop down list boxes, and enable=false makes the text on the DDL light gray and hard to read as well, you also can't see the options.
Is there a way to make the drop down list "read only" but not allow the user to change the option, but also allow the user to be able to read the text (black text just as normal...)???
Any help please? Thanks!
Re: Drop Down List and Enabled=false
The way I did it on a combobox was to inherit from it, add a readonly property and in SelectedIndexChanged, if Readonly = True, set the SelectedIndex back to the OldIndex value.
Re: Drop Down List and Enabled=false
Ok, I know the concept of what you are doing.. Back in the school days, we created classes, then inherited the class into another, changing it a little, to make another class... But, wow, that was a while ago, and was in C++. :) Guess this makes yet another learning experience on my part, no problem.
Thanks for your suggestion!
Re: Drop Down List and Enabled=false
I didn't test this, but I think it's corrected. Add it into a class module, and compile it. Then right-click on your toolbox... select add/remove items and browse to the location of the EXE or DLL you just created. It'll add it to your toolbox.
Public Class ReadonlyListBox
Inherits System.Windows.Forms.ListBox
Private RO As Boolean = False
Private OldIndex As Integer
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
#End Region
Public Property [Readonly]() As Boolean
Get
Return RO
End Get
Set(ByVal Value As Boolean)
RO = Value
End Set
End Property
Public Overrides Property SelectedIndex() As Integer
Get
Return MyBase.SelectedIndex
End Get
Set(ByVal Value As Integer)
OldIndex = Value
MyBase.SelectedIndex = Value
End Set
End Property
Protected Overrides Sub OnSelectedIndexChanged(ByVal e As System.EventArgs)
If Me.Readonly Then
Me.SelectedIndex = OldIndex
Else
MyBase.OnSelectedIndexChanged(e)
End If
End Sub
End Class
Re: Drop Down List and Enabled=false
"On read only, I turn the txt box's properties to readOnly = true, instead of enable = false, because the light gray effect with enable=false isn't really eye friendly for "read only"."
Since you want this to happen only under "read only" condition, why can't you just do the second part of DSJ's suggestion; i.e.,
"set the SelectedIndex back to the OldIndex value"
You already know when to trigger the "read only" condition. So isn't a modified listbox overdoing it?
Re: Drop Down List and Enabled=false
Ok, really this is for an ASP.Net web form, backed by VB.net, so I would really like to get around the selectedIndexChanged on the server, but I guess I didn't even think about javascript being the answer, doing what you guys suggest, to bounce back to the origonal index, when the index changes, if the user's security level is not high enough..
I'll do the server side selectedIndexChanged first, only because I'm not quite sure how to pass the security variable, to the javascript function..
Also, I'm not too sure how to, or where to initialize the drop down list as the new class 'ReadonlyListBox'.. I did try and change the protected Withevents to readonlyListBox, but got a parser error on the html part of the code.
Anyhow, it looks like a great opertunity for a learning experience is ahead of me! Thanks!
Re: Drop Down List and Enabled=false
Quote:
Originally Posted by Gizmo001
"On read only, I turn the txt box's properties to readOnly = true, instead of enable = false, because the light gray effect with enable=false isn't really eye friendly for "read only"."
Since you want this to happen only under "read only" condition, why can't you just do the second part of DSJ's suggestion; i.e.,
"set the SelectedIndex back to the OldIndex value"
You already know when to trigger the "read only" condition. So isn't a modified listbox overdoing it?
Well, I guess the question is whether this control is used by more than 1 page. If you want to re-use, it is way better to extend it. It is not really overdoing it since it is always good to encapsulate the inner working of an object behavior.
Re: Drop Down List and Enabled=false
Quote:
Originally Posted by bjswift
Ok, really this is for an ASP.Net web form, backed by VB.net, so I would really like to get around the selectedIndexChanged on the server, but I guess I didn't even think about javascript being the answer, doing what you guys suggest, to bounce back to the origonal index, when the index changes, if the user's security level is not high enough..
I'll do the server side selectedIndexChanged first, only because I'm not quite sure how to pass the security variable, to the javascript function..
Also, I'm not too sure how to, or where to initialize the drop down list as the new class 'ReadonlyListBox'.. I did try and change the protected Withevents to readonlyListBox, but got a parser error on the html part of the code.
Anyhow, it looks like a great opertunity for a learning experience is ahead of me! Thanks!
Sorry, my code was for a windows form listbox! :(
Re: Drop Down List and Enabled=false
Quote:
Originally Posted by Gizmo001
[I]You already know when to trigger the "read only" condition. So isn't a modified listbox overdoing it?
Not if you've got more than one. In my case I had about 35 in one project, and it has come in handy on others as well.
Re: Drop Down List and Enabled=false
Ok.. I don't need to re-use the control, but I do have quite a bit of drop down lists... I'm not thinking this through properly...
How do I capture the OldIndexValue? I can only think of capturing the default of all of the drop downs, and storing them in a global, but it would be ideal to just get the origonal value when the user clicks the drop down list, using it when they finish their choice (selected index change), but the nature of selected index change doesn't work like that, leaning me towards a new class???
Well. As times goes on, an answer will come along.
:)
Re: Drop Down List and Enabled=false
OK, how about this. Since it's readonly, why not use a multiline textbox instead? It would look the same.
Re: Drop Down List and Enabled=false
"How do I capture the OldIndexValue? I can only think of capturing the default of all of the drop downs, and storing them in a global, but it would be ideal to just get the origonal value when the user clicks the drop down list, using it when they finish their choice (selected index change), but the nature of selected index change doesn't work like that, leaning me towards a new class???"
Define a global integer for a dropdownlist box i (Dim OldIndexBoxi).
When the SelectedIndexChanged is triggered (when "not" under "read only" condition) in the SelectedIndexChanged sub, reset the old value:
oldindexboxi = dropddownlistboxi.selectedindex
When in "read only mode,"
do the reverse:
dropddownlistboxi.selectedindex = oldindexboxi
DSJ: This should work even for a large number of dropdownlistboxes by using array of integers for oldindexes and declariing the dropdownlistboxes as arrays and matching the two indices.
Re: Drop Down List and Enabled=false
Questions:
1. Is this an ASP.NET app?
2. Who set the default value for the dropdownlist?
Re: Drop Down List and Enabled=false
My suggestion was for Windows not ASP app.
Re: Drop Down List and Enabled=false
Quote:
Originally Posted by Gizmo001
[I]
DSJ: This should work even for a large number of dropdownlistboxes by using array of integers for oldindexes and declariing the dropdownlistboxes as arrays and matching the two indices.
My OOP solution works just fine, thanks... no need for arrays or any matching logic. I prefer KISS.
Re: Drop Down List and Enabled=false
DSJ:
I wasn't suggesting you change from your OOP - yours' is indeed the solution I would use if I had to use a large # of such dropdownlistboxes or if I had to use such boxes often in multiple apps.
Re: Drop Down List and Enabled=false
Okay, here is what I decided, with the help of my colleagues. Basically I was just going to disable the "Save Changes" button for users who have read only status, but I didn't want users to spend all day making changes, only to find out they can't make changes. So, taking care of the txt boxes is no problem, setting the read only. I was spending all of my time working on the darn ddl boxes, but really, the most simple answer is to leave them alone, because if the users can't save changes, and can't spend all day making box changes, there is no problem. They are still able to read the txt boxes, and see the options that are in the DDLists.
All of your input was helpful, and insightful, but it was my own stubbern-ness that led me to trying to make the read-only state available for the DDLs.
Thanks for your help!!