|
-
Sep 3rd, 2009, 10:43 AM
#1
Thread Starter
Hyperactive Member
[Resolved] Making ALL comboboxes on a form ReadOnly
Is there a simple way to make all comboboxes on a form to be readonly upon form load?
Rather than doing:
Code:
ComboBox1.ReadOnly = True
Combobox2.ReadOnly = True
And so on...
-
Sep 3rd, 2009, 10:51 AM
#2
Re: Making ALL comboboxes on a form ReadOnly
ComboBoxes don't have a ReadOnly property. But in general the answer to your question is that you loop through all the controls on the form and test if it's a combobox, if it is then you do whatever you want with it:
vb.net Code:
For Each ctl As Control In Me.Controls If TypeOf ctl Is ComboBox Then 'Do something with the combobox End If Next 'Or with LINQ Dim cbxes = (From ctl In Me.Controls _ Where TypeOf ctl Is ComboBox _ Select ctl).OfType(Of ComboBox)()
Note that this code doesn't check nested controls.
-
Sep 3rd, 2009, 10:54 AM
#3
Thread Starter
Hyperactive Member
Re: Making ALL comboboxes on a form ReadOnly
Woops i meant textbox.. i dont know what i was thinking and thanks! thats what i needed!
-
Sep 3rd, 2009, 10:58 AM
#4
Thread Starter
Hyperactive Member
Re: Making ALL comboboxes on a form ReadOnly
But what do i use in front of:
.ReadOnly = True
when i use that format?
-
Sep 3rd, 2009, 11:03 AM
#5
Re: Making ALL comboboxes on a form ReadOnly
Assuming you are using the For Next loop:
vb.net Code:
For Each ctl As Control In Me.Controls If TypeOf ctl Is TextBox Then DirectCast(ctl, TextBox).ReadOnly = True End If Next
-
Sep 3rd, 2009, 11:21 AM
#6
Re: Making ALL comboboxes on a form ReadOnly
I like to toggle things in case I want to do the opposite sometimes. Since you seem to have a reason not to set this in design, it stands to reason you might want to change your mind.
vb.net Code:
Public Class Form1
Private Sub MakeTBReadOnly(ByVal IsReadOnly As Boolean)
Dim ctrl As Control = Me.GetNextControl(Me, True)
Do
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).ReadOnly = IsReadOnly
End If
ctrl = Me.GetNextControl(ctrl, True)
Loop Until ctrl Is Nothing
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MakeTBReadOnly(True) 'make them readonly
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MakeTBReadOnly(False) 'make them read/write
End Sub
End Class
-
Sep 3rd, 2009, 11:29 AM
#7
Re: Making ALL comboboxes on a form ReadOnly
I would just like to add that the For loop ForumAccount uses will not visit nested controls (controls in a panel/groupbox/tabpage, whatever), as he mentioned already, whereas the Do loop used by Hack does.
Also, to both of you, isn't it better to use the following:
Code:
Dim tb As TextBox = TryCast(ctrl, TextBox)
If tb IsNot Nothing Then tb.ReadOnly = IsReadOnly
instead of using the TypeOf operator, and then the DirectCast function? As far as I know, both TypeOf and (obviously) DirectCast will perform a cast, so it will be cast twice, whereas the TryCast way will cast only once. If 'tb' isn't derived from a TextBox then it will be Nothing after the cast, so that is more or less equivalent to using the TypeOf operator, isn't it?
I'm sure it will be a minor difference, but perhaps if you have loads and loads of textboxes..?
Am I right about this, or does it not make a difference at all?
-
Sep 3rd, 2009, 11:32 AM
#8
Thread Starter
Hyperactive Member
Re: Making ALL comboboxes on a form ReadOnly
I just have admin rights as a boolean. If Boolean = true, then it lets you modify that data.. As long as boolean = false.. You can see it but not change it.
Thanks for the input guys! i think ill mark this as resolved now!
-
Sep 3rd, 2009, 12:06 PM
#9
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
I get: IsReadOnly is not declared
-
Sep 3rd, 2009, 12:07 PM
#10
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
You must have missed the parameter in the method declaration
-
Sep 3rd, 2009, 12:09 PM
#11
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
Btw i am also using telerik rad controls.. So they are RadTextBox
It works up until Me.Controls or Me.GetNextControl in the examples by Hack and ForumAccount. But every other mentioning of control, lets me change it to radcontrol with no problem.. But Me.Controls cant be changed to me.RadControls doesnt work.
-
Sep 3rd, 2009, 12:09 PM
#12
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
 Originally Posted by NickThissen
You must have missed the parameter in the method declaration 
what u mean?
-
Sep 3rd, 2009, 12:11 PM
#13
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
Ahh this:
vb Code:
Private Sub MakeTBReadOnly(ByVal IsReadOnly As Boolean)
-
Sep 3rd, 2009, 12:14 PM
#14
Re: Making ALL comboboxes on a form ReadOnly
 Originally Posted by NickThissen
I would just like to add that the For loop ForumAccount uses will not visit nested controls ( controls in a panel/groupbox/tabpage, whatever), as he mentioned already, whereas the Do loop used by Hack does.
Also, to both of you, isn't it better to use the following:
Code:
Dim tb As TextBox = TryCast(ctrl, TextBox)
If tb IsNot Nothing Then tb.ReadOnly = IsReadOnly
instead of using the TypeOf operator, and then the DirectCast function? As far as I know, both TypeOf and (obviously) DirectCast will perform a cast, so it will be cast twice, whereas the TryCast way will cast only once. If 'tb' isn't derived from a TextBox then it will be Nothing after the cast, so that is more or less equivalent to using the TypeOf operator, isn't it?
I'm sure it will be a minor difference, but perhaps if you have loads and loads of textboxes..?
Am I right about this, or does it not make a difference at all?
You're probably right about that, it seems to make sense. That's how the Windows programmers commonly do it.
-
Sep 3rd, 2009, 12:19 PM
#15
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
This is what ive been able to get:
vb Code:
Private Sub MakeTBReadOnly(ByVal IsReadOnly As Boolean)
Dim ctrl As RadControl
Do
If TypeOf ctrl Is RadTextBox Then
Dim tb As RadTextBox = TryCast(ctrl, RadTextBox)
If tb IsNot Nothing Then tb.ReadOnly = IsReadOnly
End If
ctrl = Nothing
Loop Until ctrl Is Nothing
End Sub
It wont let me use Me.GetNextControl(Me, True) because it is not a windows form control.
-
Sep 3rd, 2009, 12:21 PM
#16
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
That's not going to work. Actually I think you'll get an exception, what type of project is this? Or are you using a 3rd party form too?
-
Sep 3rd, 2009, 12:24 PM
#17
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
Surely a RadTextBox is derived from Control, too? If it's not, I'm stumped.
If it is, then what stops you from simply using what Hack told you, except replacing TextBox with RadTextBox..?
If it really doesn't derive from Control then I think your only option left is to use the For loop as ForumAccount showed you. Check if the RadTextBox controls are directly on the form however, and not in a Panel or any other container control. If they are all in one container control, you can replace "Me" with the name of that container. If they are scattered across different containers, you have to put them into a List(Of RadTextBox) manually (possibly in the constructor or Form_Load event) and loop through that instead.
-
Sep 3rd, 2009, 12:32 PM
#18
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
Im using a 3rd party tool.. Not a 3rd party form.
-
Sep 3rd, 2009, 12:34 PM
#19
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
Actually.. This shows no error underlining, but it doesnt do anything either,
Code:
Dim ctrl As RadControl = Me.GetNextControl(Me, True)
Do
If TypeOf ctrl Is RadTextBox Then
Dim tb As RadTextBox = TryCast(ctrl, RadTextBox)
If tb IsNot Nothing Then tb.ReadOnly = IsReadOnly
End If
ctrl = Nothing
Loop Until ctrl Is Nothing
End Sub
-
Sep 3rd, 2009, 12:34 PM
#20
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
This doesnt work for sure...
vb Code:
For Each ctl As Control In Me.Controls
-
Sep 3rd, 2009, 12:39 PM
#21
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
The way you have it now, it's going to test the first control in the tab order then exit the loop, you need to change the bolded line:
Code:
Dim ctrl As RadControl = Me.GetNextControl(Me, True)
Do
If TypeOf ctrl Is RadTextBox Then
Dim tb As RadTextBox = TryCast(ctrl, RadTextBox)
If tb IsNot Nothing Then tb.ReadOnly = IsReadOnly
End If
ctrl = Me.GetNextControl(ctl, True)
Loop Until ctrl Is Nothing
End Sub
-
Sep 3rd, 2009, 12:39 PM
#22
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
And why doesn't it work?
I have never heard about the RadTextBox, but I would be very surprised if it's a control that does not derive in some way or another from the Control class. I it does derive from Control then the code Hack posted should work when you change TextBox to RadTextBox.
You seem to have made a few other changes that break the code completely.
It should be like this:
Code:
Dim ctrl As Control = Me.GetNextControl(Me, True)
Do
Dim tb As RadTextBox = TryCast(ctrl, RadTextBox)
If tb IsNot Nothing Then tb.ReadOnly = IsReadOnly
ctrl = Me.GetNextControl(ctrl, True)
Loop Until ctrl Is Nothing
End Sub
Two changes I made are highlighted in bold, the third is that I removed the TypeOf check. It's not strictly wrong to use it, but you should either use TypeOf in combination with DirectCast, as Hack showed you, or use only TryCast, as I showed. A combination works too, but it defeats the point of using my method completely
-
Sep 3rd, 2009, 12:42 PM
#23
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
Hmm... seems I missed a couple things.
-
Sep 3rd, 2009, 12:45 PM
#24
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
Im using RadTextboxes... When u change it to RadTextbox from textbox it has no problem.. If i move my mouse over RadControl, which i changed Control to, It will say:
Represents a RadControl. RadControl is an Abstract Class and is base class for all Telerik Controls.
-
Sep 3rd, 2009, 12:50 PM
#25
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
Heres my Current Code:
vb Code:
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports Telerik.WinControls.Themes.Design
Imports Telerik.WinControls.UI
Imports Telerik.WinControls
Imports System.Data.SqlClient
Public Class frmuser
Private Sub RadMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadMenuItem3.Click
frmrole.Show()
End Sub
Private Sub frmuser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MakeTBReadOnly(True)
End Sub
Private Sub MakeTBReadOnly(ByVal IsReadOnly As Boolean)
Dim ctrl As Control = Me.GetNextControl(Me, True)
Do
Dim tb As RadTextBox = TryCast(ctrl, RadTextBox)
If tb IsNot Nothing Then tb.ReadOnly = IsReadOnly
ctrl = Me.GetNextControl(ctrl, True)
Loop Until ctrl Is Nothing
End Sub
It does not make the comboboxes ReadOnly like it should.
-
Sep 3rd, 2009, 01:12 PM
#26
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
Nvm it works! Though once i move MakeTBReadOnly(True) to an If statement it stops working.
Last edited by Monkz; Sep 3rd, 2009 at 01:33 PM.
-
Sep 4th, 2009, 08:25 AM
#27
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
This worked only when it was not in an if statement:
Code:
Dim ctrl As Control = Me.GetNextControl(Me, True)
Do
Dim tb As RadTextBox = TryCast(ctrl, RadTextBox)
If tb IsNot Nothing Then tb.ReadOnly = IsReadOnly
ctrl = Me.GetNextControl(ctrl, True)
Loop Until ctrl Is Nothing
After getting annoyed... I tried this.. Which also worked, but this works in the if statement, where the code above didn't.. Why would this be?
Code:
For Each control In Me.Controls
If TypeOf control Is RadTextBox Then
Dim t As RadTextBox = TryCast(control, RadTextBox)
t.ReadOnly = True
End If
Next
-
Sep 4th, 2009, 10:40 AM
#28
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
What If statement, can you show us? The if statement itself should not (and cannot) affect the code inside it, except of course if it prevents it from running in the first place. To see if your loop is running at all, you can place a breakpoint anywhere inside the loop (or just on the start of the loop). When the code execution reaches the line with the breakpoint, it will halt until you tell it to continue. If the execution never halts, then the breakpoint is never reached, and your loop doesn't execute at all.
The second code still has a slight issue, which isn't really important for it's functionality though. It's just a tiny bit 'odd' if you use the TryCast function after you have verified that the type is indeed a RadTextBox.
The TryCast function is actually just the same as the DirectCast function, except for one thing: if the object you are casting cannot be cast to the type you specify, then the DirectCast function will give an error. The TryCast function will not give an error, but it will simply return Nothing (null object).
So, the TryCast function can be used to try to cast an object, and you can see by checking if it's Nothing whether or not it succeeded. If you have already verified that it control is of type RadTextBox, then TryCast cannot fail (at least not for that reason) and so you could just use DirectCast instead.
Now, I don't think it matters whether you use DirectCast or TryCast in your example, but using TryCast enables you to get rid of the TypeOf check, which does an extra cast 'behind the scenes'. So, there is only one cast taking place when you use TryCast (the correct way), while there are two if you use DirectCast in combination with the TypeOf operator.
I realize this is a little off-topic as it has nothing to do with your problem, but you seem to be trying to use it correctly ever since I mentioned it, so I am just correcting you and telling you that it's still not 100% correct (although, as said, it will work equally well).
-
Sep 4th, 2009, 10:48 AM
#29
Thread Starter
Hyperactive Member
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
So would it be right if i change this:
Code:
For Each control In Me.Controls
If TypeOf control Is RadTextBox Then
Dim t As RadTextBox = TryCast(control, RadTextBox)
t.ReadOnly = True
End If
Next
To This:
Code:
For Each control In Me.Controls
Dim t As RadTextBox = TryCast(control, RadTextBox)
t.ReadOnly = True
End If
Next
-
Sep 4th, 2009, 11:39 AM
#30
Re: [Resolved] Making ALL comboboxes on a form ReadOnly
Nearly. You just need to check if t is not Nothing first, otherwise you cannot access its ReadOnly property. CHecking if t is not Nothing is the equivalent of using the TypeOf check in the first code. The only difference is that the TypeOf check does another cast, which can be bad for memory / speed if used a lot.
You are looking for this:
Code:
For Each control In Me.Controls
Dim t As RadTextBox = TryCast(control, RadTextBox)
If t IsNot Nothing Then
t.ReadOnly = True
End If
End If
Next
Please note again that you should use the Do loop in combination with GetNextControl to visit every control. The For loop like this will not visit nested controls.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|