Is there a simple way to make all comboboxes on a form to be readonly upon form load?
Rather than doing:
And so on...Code:ComboBox1.ReadOnly = True
Combobox2.ReadOnly = True
Printable View
Is there a simple way to make all comboboxes on a form to be readonly upon form load?
Rather than doing:
And so on...Code:ComboBox1.ReadOnly = True
Combobox2.ReadOnly = True
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.
Woops i meant textbox.. i dont know what i was thinking and thanks! thats what i needed!
But what do i use in front of:
.ReadOnly = True
when i use that format?
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
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
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:
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?Code:Dim tb As TextBox = TryCast(ctrl, TextBox)
If tb IsNot Nothing Then tb.ReadOnly = IsReadOnly
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?
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!
I get: IsReadOnly is not declared
You must have missed the parameter in the method declaration ;)
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.
Ahh this:
vb Code:
Private Sub MakeTBReadOnly(ByVal IsReadOnly As Boolean)
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.
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?
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.
Im using a 3rd party tool.. Not a 3rd party form.
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
This doesnt work for sure...
vb Code:
For Each ctl As Control In Me.Controls
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
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:
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 :)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
Hmm... seems I missed a couple things. :blush:
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.
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.
Nvm it works! Though once i move MakeTBReadOnly(True) to an If statement it stops working.
This worked only when it was not in an if statement:
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: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
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
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).
So would it be right if i change this:
To 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
Code:For Each control In Me.Controls
Dim t As RadTextBox = TryCast(control, RadTextBox)
t.ReadOnly = True
End If
Next
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:
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.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