Results 1 to 30 of 30

Thread: [Resolved] Making ALL comboboxes on a form ReadOnly

  1. #1

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    [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...

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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:
    1. For Each ctl As Control In Me.Controls
    2.      If TypeOf ctl Is ComboBox Then
    3.           'Do something with the combobox
    4.      End If
    5. Next
    6. 'Or with LINQ
    7. Dim cbxes = (From ctl In Me.Controls _
    8.              Where TypeOf ctl Is ComboBox _
    9.              Select ctl).OfType(Of ComboBox)()

    Note that this code doesn't check nested controls.

  3. #3

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    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!

  4. #4

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: Making ALL comboboxes on a form ReadOnly

    But what do i use in front of:

    .ReadOnly = True

    when i use that format?

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Making ALL comboboxes on a form ReadOnly

    Assuming you are using the For Next loop:
    vb.net Code:
    1. For Each ctl As Control In Me.Controls
    2.      If TypeOf ctl Is TextBox Then
    3.           DirectCast(ctl, TextBox).ReadOnly = True
    4.      End If
    5. Next

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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:
    1. Public Class Form1
    2.     Private Sub MakeTBReadOnly(ByVal IsReadOnly As Boolean)
    3.         Dim ctrl As Control = Me.GetNextControl(Me, True)
    4.         Do
    5.             If TypeOf ctrl Is TextBox Then
    6.                 CType(ctrl, TextBox).ReadOnly = IsReadOnly
    7.             End If
    8.             ctrl = Me.GetNextControl(ctrl, True)
    9.         Loop Until ctrl Is Nothing
    10.     End Sub
    11.  
    12.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    13.         MakeTBReadOnly(True)  'make them readonly
    14.     End Sub
    15.  
    16.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    17.         MakeTBReadOnly(False)  'make them read/write
    18.     End Sub
    19. End Class

  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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?

  8. #8

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    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!

  9. #9

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: [Resolved] Making ALL comboboxes on a form ReadOnly

    I get: IsReadOnly is not declared

  10. #10

  11. #11

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    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.

  12. #12

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: [Resolved] Making ALL comboboxes on a form ReadOnly

    Quote Originally Posted by NickThissen View Post
    You must have missed the parameter in the method declaration
    what u mean?

  13. #13

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: [Resolved] Making ALL comboboxes on a form ReadOnly

    Ahh this:
    vb Code:
    1. Private Sub MakeTBReadOnly(ByVal IsReadOnly As Boolean)

  14. #14
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Making ALL comboboxes on a form ReadOnly

    Quote Originally Posted by NickThissen View Post
    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.

  15. #15

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: [Resolved] Making ALL comboboxes on a form ReadOnly

    This is what ive been able to get:

    vb Code:
    1. Private Sub MakeTBReadOnly(ByVal IsReadOnly As Boolean)
    2.  
    3.         Dim ctrl As RadControl
    4.         Do
    5.  
    6.             If TypeOf ctrl Is RadTextBox Then
    7.                 Dim tb As RadTextBox = TryCast(ctrl, RadTextBox)
    8.                 If tb IsNot Nothing Then tb.ReadOnly = IsReadOnly
    9.             End If
    10.  
    11.             ctrl = Nothing
    12.         Loop Until ctrl Is Nothing
    13.     End Sub

    It wont let me use Me.GetNextControl(Me, True) because it is not a windows form control.

  16. #16
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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?

  17. #17
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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.

  18. #18

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: [Resolved] Making ALL comboboxes on a form ReadOnly

    Im using a 3rd party tool.. Not a 3rd party form.

  19. #19

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    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

  20. #20

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: [Resolved] Making ALL comboboxes on a form ReadOnly

    This doesnt work for sure...

    vb Code:
    1. For Each ctl As Control In Me.Controls

  21. #21
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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

  22. #22
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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

  23. #23
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [Resolved] Making ALL comboboxes on a form ReadOnly

    Hmm... seems I missed a couple things.

  24. #24

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    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.

  25. #25

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: [Resolved] Making ALL comboboxes on a form ReadOnly

    Heres my Current Code:
    vb Code:
    1. Imports Microsoft.VisualBasic
    2. Imports System
    3. Imports System.Collections.Generic
    4. Imports System.ComponentModel
    5. Imports System.Data
    6. Imports System.Drawing
    7. Imports System.Text
    8. Imports System.Windows.Forms
    9. Imports Telerik.WinControls.Themes.Design
    10. Imports Telerik.WinControls.UI
    11. Imports Telerik.WinControls
    12. Imports System.Data.SqlClient
    13.  
    14. Public Class frmuser
    15.  
    16.     Private Sub RadMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadMenuItem3.Click
    17.         frmrole.Show()
    18.     End Sub
    19.     Private Sub frmuser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    20.         MakeTBReadOnly(True)
    21.     End Sub
    22.     Private Sub MakeTBReadOnly(ByVal IsReadOnly As Boolean)
    23.  
    24.         Dim ctrl As Control = Me.GetNextControl(Me, True)
    25.         Do
    26.  
    27.             Dim tb As RadTextBox = TryCast(ctrl, RadTextBox)
    28.             If tb IsNot Nothing Then tb.ReadOnly = IsReadOnly
    29.  
    30.             ctrl = Me.GetNextControl(ctrl, True)
    31.         Loop Until ctrl Is Nothing
    32.     End Sub

    It does not make the comboboxes ReadOnly like it should.

  26. #26

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    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.

  27. #27

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    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

  28. #28
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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).

  29. #29

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    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

  30. #30
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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
  •  



Click Here to Expand Forum to Full Width