Results 1 to 8 of 8

Thread: How do I make a Checkbox readonly? [resolved]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Posts
    164

    How do I make a Checkbox readonly? [resolved]

    I want to keep the checkbox enabled (to keep the white bg/black fg checkmark symbol), but I want it to be readonly. If I set enable=false, then everything gets grayed out.

    Any ideas how I can set all the checkboxes on my form to readonly, but maintain the enable state to preserve the same look as normal checkboxes?
    Last edited by Shurijo; Aug 28th, 2003 at 01:35 PM.
    -Shurijo

  2. #2
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    when the user click you can just do a code to but back the value of it.

    if chkBox.value = true then chkbox.value = true

    Something like that

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Posts
    164
    I thought about that but I'd have to store the value (checked or unchecked) for each checkbox on my form (dozens of them) and then use the same event handler for all checkbox_clicks, etc. I was wondering if there was some easier way.
    -Shurijo

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Posts
    164
    I ended up doing it that way:

    PHP Code:

    ...
    'loop through all checkboxes
    '
    ckb checkbox object
    ckb
    .Tag LCase(CStr(ckb.Checked))
    AddHandler ckb.CheckedChanged, New System.EventHandler(AddressOf checkbox_CheckedChanged)

    ....

    Private 
    Sub checkbox_CheckedChanged(ByVal sender As System.ObjectByVal e As System.EventArgs)
       
    Dim ckbox As CheckBox
       ckbox 
    CType(senderCheckBox)
       
    ckbox.Checked IIf(LCase(ckbox.Tag) = "true"TrueFalse)
    End Sub 
    -Shurijo

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Set them to Checked [True] and put this code into your form .
    VB Code:
    1. Private Sub CheckBox1_CheckedChanged(ByVal sender As
    2. System.Object, ByVal e As System.EventArgs) Handles
    3. CheckBox1.CheckedChanged, CheckBox2.CheckedChanged,
    4. CheckBox3.CheckedChanged
    5.  
    6.         CheckBox1.CheckState = CheckState.Checked
    7.         CheckBox2.CheckState = CheckState.Checked
    8.         CheckBox3.CheckState = CheckState.Checked
    9.  
    10.     End Sub

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    You can define a subclass of checkbox and declare a readonlycheckstate property for it, and in checkstate changed event set it back to original state. In case you need to change the checkstate all you have to do is changing that property.
    BTW, why you need lots of readonly check boxes? are they bound to a datasource or something that you dont want to be edited? if so you can disable edit of the underlying datasource. Clear enough?
    VB Code:
    1. Public Class ReadOnlyCheckBox
    2.         Inherits CheckBox
    3.         Dim c_state As CheckState
    4.         Public Property ReadOnlyState() As CheckState
    5.             Get
    6.                 Return c_state
    7.             End Get
    8.             Set(ByVal Value As CheckState)
    9.                 c_state = Value
    10.                 MyBase.CheckState = Value
    11.             End Set
    12.         End Property
    13.         Public Sub New(ByVal state As CheckState)
    14.             c_state = state
    15.             MyBase.CheckState = state
    16.         End Sub
    17.         Private Sub ReadOnlyCheckBox_CheckStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.CheckStateChanged
    18.             MyBase.CheckState = c_state
    19.         End Sub
    20.     End Class
    Last edited by Lunatic3; Aug 28th, 2003 at 02:20 PM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Posts
    164
    BTW, why you need lots of readonly check boxes? are they bound to a datasource or something that you dont want to be edited? if so you can disable edit of the underlying datasource. Clear enough?
    Editing is disabled from the underlying datasource, but the client wants white checkboxes rather than grayed out checkboxes.
    -Shurijo

  8. #8
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    The checkboxes wont be grayed out if bound to a readonly datasource, will they?
    Anyway, i think the idea of custom control will be a good aproach. You can bind the ReadOnlyState property to the datasource.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

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