How to enable a disabled control on click
Asked Answered
H

3

6

I have a DevExpress grid, which is disabled on screen. When I click the control, I want it to become enabled. Right now I have a click event set up for the grid:

        private void gridPSR_Click(object sender, EventArgs e)
        {
            gridPSR.Enabled = true;
        }

This isn't working. How should I be going about this?

Hoes answered 22/8, 2014 at 19:30 Comment(0)
H
10

Disabled controls do not receive windows messages, so you will never get the click message on that control. Assuming this is Winforms, you can listen for the click on the form (or whatever control is hosting this grid) and check if the click location is in the rectangle of the disabled control and then enable the control accordingly:

void Form1_MouseClick(object sender, MouseEventArgs e)
{
    if (gridPSR.ClientRectangle.Contains(e.Location))
    {
        gridPSR.Enabled = true;
    }
}
Housman answered 22/8, 2014 at 19:55 Comment(1)
Good answer, didn't even think about that option.Rolo
B
6

I know it's an old post but for me bounds worked instead of ClientRectangle

    private void OnPanelMouseClick(object sender, MouseEventArgs e)
    {
        if ((e.Button == MouseButtons.Left) &&
             myControl.Bounds.Contains(e.Location) &&
             !myControl.Enabled)
        {
            myControl.Enabled = true;
        }
    }

Where myControl is member variable of your control instance. OnPanelMouseClick handler should be linked with MouseClick event of form or container that holds control.

Butterfingers answered 24/5, 2016 at 9:28 Comment(0)
W
0

In this code I am setting an event on a disabled TextBox control called 'txtNumLabels'. I tested this code with the text box both on a Form and also with having it within a GroupBox container.

Set an event in the constructor after the 'InitializeComponent();'

this.txtNumLabels.Parent.MouseClick += new System.Windows.Forms.MouseEventHandler(this.txtNumLabels_Parent_MouseClick);

Here is the event handler -

private void txtNumLabels_Parent_MouseClick(object sender, MouseEventArgs mouseEvent)
{            
    // The Bounds property of a control returns a rectangle of its Location and Size within its parent control
    Rectangle rect = txtNumLabels.Bounds;

    // Other method that gets the same rectangle -
    // Point t = txtNumLabels.Location;
    // Size ts = txtNumLabels.Size;
    // Rectangle rect = new Rectangle(t, ts);

    if (rect.Contains(mouseEvent.Location))
    {
        txtNumLabels.Enabled = true;
    }
}
Waziristan answered 15/2, 2022 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.