locked
Disable and turn text light gray for all controls, including DataGridView RRS feed

  • Question

  • I am trying to disable and turn text the light grey for all controls on a form.  I am running into two issues:

    1. The DataGridView control will not disable (I can still activate a ContextMenuStrip associated with the DataGridView control) and
    2. The ctl.ColumnHeadersDefaultCellStyle.ForeColor = Color.LightGray line throws an error saying it is not a member of System.Windows.Forms.Control

    My code is as follows:

            For Each ctl As Control In Controls

                If TypeOf ctl Is DataGridView Then

                    ctl.ColumnHeadersDefaultCellStyle.ForeColor = Color.LightGray
                    ctl.Enabled = False

                Else

                    ctl.ForeColor = Color.LightGray
                    ctl.Enabled = False

                End If

            Next

     

    Wednesday, June 25, 2014 4:32 PM

Answers

  • Well we can take it a tad bit farther

    Private Sub DisableDataGridView(ByVal sender As DataGridView)
        With sender
            .Enabled = False
            .ForeColor = Color.Gray
            .ReadOnly = True
            .BackgroundColor = SystemColors.Control
            .BorderStyle = BorderStyle.None
            .DefaultCellStyle.BackColor = SystemColors.Control
            .EnableHeadersVisualStyles = False
            .CurrentCell = Nothing
            .ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control
            For Each col As DataGridViewColumn In .Columns
                col.HeaderCell.Style.ForeColor = Color.Gray
            Next
        End With
    End Sub
     

    Or toggle properties where I would store the default values in a container rather than hard code value as I am here (don't have the time).

    Private Sub DisableDataGridView(ByVal sender As DataGridView)
        If sender.Enabled Then
            With sender
                .Enabled = False
                .ForeColor = Color.Gray
                .ReadOnly = True
                .BackgroundColor = SystemColors.Control
                .BorderStyle = BorderStyle.None
                .DefaultCellStyle.BackColor = SystemColors.Control
                .EnableHeadersVisualStyles = False
                .CurrentCell = Nothing
                .ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control
                For Each col As DataGridViewColumn In .Columns
                    col.HeaderCell.Style.ForeColor = Color.Gray
                Next
            End With
        Else
            With sender
                .Enabled = True
                .ForeColor = Color.Black
                .ReadOnly = False
                .BackgroundColor = SystemColors.AppWorkspace
                .BorderStyle = BorderStyle.FixedSingle
                .DefaultCellStyle.BackColor = Color.White
                .EnableHeadersVisualStyles = True
                .ColumnHeadersDefaultCellStyle.BackColor = Nothing
                For Each col As DataGridViewColumn In .Columns
                    col.HeaderCell.Style.ForeColor = Nothing
                Next
            End With
        End If
    
    End Sub
    Of course after pressing submit I remembered we can include AllowUserToAddRows to the above so the pesky * is not sticking out like a sore thumb.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


    Wednesday, June 25, 2014 7:21 PM
  • Hello,

    For the DataGridView that I setup a context menu which after running this code will not be accessible.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        DisableDataGridView(DataGridView1)
    End Sub
    Private Sub DisableDataGridView(ByVal sender As DataGridView)
        With sender
            .Enabled = False
            .ForeColor = Color.Gray
            For Each col As DataGridViewColumn In .Columns
                col.HeaderCell.Style.ForeColor = Color.Gray
            Next
        End With
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    But the control itself (the border frame and all) doesn't change to gray, or does it?

    Still lost in code, just at a little higher level.

    :-)

    • Marked as answer by Patrick D W Wednesday, June 25, 2014 8:45 PM
    Wednesday, June 25, 2014 6:01 PM

All replies

  • Hi,

     How about setting the Forms Enable property to false. It won`t change the DataGridViews text colors but, it will disable all the controls. However, you would not be able to move, minimize, or close the form until it the form is re-enabled. That could be done at the end of doing whatever you need to disable them for is done or maybe a timer or something. Just a thought.

    Me.Enabled = False


    If you say it can`t be done then i`ll try it


    • Edited by IronRazerz Wednesday, June 25, 2014 5:04 PM
    • Proposed as answer by Frank L. Smith Wednesday, June 25, 2014 5:27 PM
    Wednesday, June 25, 2014 5:03 PM
  • Patrick,

    Another way would be to put a container control (such as a panel) on the form and put all of your other controls in that panel. When you disable the panel, all of the child controls will automatically be disabled.

    The reason I think this is preferable to disabling the form is that this way the form itself is still enabled and works normally. If, as an example, you set the cursor to show the WaitCursor, if the form is disabled that's ignored, whereas if you disable the container control then you'll see the wait cursor.

    *****

    As for turning everything gray, that's not an option we have (short of making your own). The DGV being an example; they didn't set it to turn gray when .Enabled = False, so there's not a lot you can do about that part.


    Still lost in code, just at a little higher level.

    :-)


    • Edited by Frank L. Smith Wednesday, June 25, 2014 5:13 PM ...forgot a few words
    • Proposed as answer by IronRazerz Wednesday, June 25, 2014 5:17 PM
    Wednesday, June 25, 2014 5:12 PM
  • @ Frank,

     "Another way would be to put a container control (such as a panel)"

     Yes, i thought about that and forgot to mention it. That would be better than disabling the Form itself.   :)


    If you say it can`t be done then i`ll try it

    Wednesday, June 25, 2014 5:17 PM
  • @ Frank,

     "Another way would be to put a container control (such as a panel)"

     Yes, i thought about that and forgot to mention it. That would be better than disabling the Form itself.   :)


    If you say it can`t be done then i`ll try it

    I don't routinely show my own stuff here, but I think this is a good example of why:

    What you don't see is that behind all of that (in the Z-order) is a standard SplitContainer control so when I want to disable both left and right sections quickly - I just disable the SplitContainer and it's done. :)


    Still lost in code, just at a little higher level.

    :-)

    Wednesday, June 25, 2014 5:27 PM
  • Hello,

    For the DataGridView that I setup a context menu which after running this code will not be accessible.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        DisableDataGridView(DataGridView1)
    End Sub
    Private Sub DisableDataGridView(ByVal sender As DataGridView)
        With sender
            .Enabled = False
            .ForeColor = Color.Gray
            For Each col As DataGridViewColumn In .Columns
                col.HeaderCell.Style.ForeColor = Color.Gray
            Next
        End With
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Wednesday, June 25, 2014 5:58 PM
  • Hello,

    For the DataGridView that I setup a context menu which after running this code will not be accessible.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        DisableDataGridView(DataGridView1)
    End Sub
    Private Sub DisableDataGridView(ByVal sender As DataGridView)
        With sender
            .Enabled = False
            .ForeColor = Color.Gray
            For Each col As DataGridViewColumn In .Columns
                col.HeaderCell.Style.ForeColor = Color.Gray
            Next
        End With
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    But the control itself (the border frame and all) doesn't change to gray, or does it?

    Still lost in code, just at a little higher level.

    :-)

    • Marked as answer by Patrick D W Wednesday, June 25, 2014 8:45 PM
    Wednesday, June 25, 2014 6:01 PM
  • Well we can take it a tad bit farther

    Private Sub DisableDataGridView(ByVal sender As DataGridView)
        With sender
            .Enabled = False
            .ForeColor = Color.Gray
            .ReadOnly = True
            .BackgroundColor = SystemColors.Control
            .BorderStyle = BorderStyle.None
            .DefaultCellStyle.BackColor = SystemColors.Control
            .EnableHeadersVisualStyles = False
            .CurrentCell = Nothing
            .ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control
            For Each col As DataGridViewColumn In .Columns
                col.HeaderCell.Style.ForeColor = Color.Gray
            Next
        End With
    End Sub
     

    Or toggle properties where I would store the default values in a container rather than hard code value as I am here (don't have the time).

    Private Sub DisableDataGridView(ByVal sender As DataGridView)
        If sender.Enabled Then
            With sender
                .Enabled = False
                .ForeColor = Color.Gray
                .ReadOnly = True
                .BackgroundColor = SystemColors.Control
                .BorderStyle = BorderStyle.None
                .DefaultCellStyle.BackColor = SystemColors.Control
                .EnableHeadersVisualStyles = False
                .CurrentCell = Nothing
                .ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control
                For Each col As DataGridViewColumn In .Columns
                    col.HeaderCell.Style.ForeColor = Color.Gray
                Next
            End With
        Else
            With sender
                .Enabled = True
                .ForeColor = Color.Black
                .ReadOnly = False
                .BackgroundColor = SystemColors.AppWorkspace
                .BorderStyle = BorderStyle.FixedSingle
                .DefaultCellStyle.BackColor = Color.White
                .EnableHeadersVisualStyles = True
                .ColumnHeadersDefaultCellStyle.BackColor = Nothing
                For Each col As DataGridViewColumn In .Columns
                    col.HeaderCell.Style.ForeColor = Nothing
                Next
            End With
        End If
    
    End Sub
    Of course after pressing submit I remembered we can include AllowUserToAddRows to the above so the pesky * is not sticking out like a sore thumb.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


    Wednesday, June 25, 2014 7:21 PM
  • Kev,

    Good work!

    Thinking out loud here, maybe use a single method (sub) which has two parameters: The name of the DGV and an enum (Enable/Disable) and let it do all of that in one place.

    It's a thought. :)


    Still lost in code, just at a little higher level.

    :-)

    Wednesday, June 25, 2014 7:32 PM
  • So many great replies.  Thanks much.  I think for my purposes, the method proposed by Frank L. Smith and expanded upon by Kevininstructor works best.  This should help a lot of people.
    Wednesday, June 25, 2014 8:47 PM