Buena
Buena

Reputation: 2491

How to play a beep sound clicking on a disabled control?

I want the following on the btn1.Click:

if (btn1.Enabled == false) System.Media.SystemSounds.Beep.Play();  

Is there a way to play the beep although btn1 is disabled ?

In fact, I need something like this to have by default:

foreach (controls c in Form1.Controls)
if (c is clicked && c.Enabled == false)
System.Media.SystemSounds.Beep.Play();

Upvotes: 0

Views: 2820

Answers (4)

Sani Huttunen
Sani Huttunen

Reputation: 24395

You can add a click event to the form and get the control that is at the position of the click:

private void Form1_Click(object sender, EventArgs e)
{
  var p = PointToClient(Cursor.Position);
  var c = GetChildAtPoint(p);
  if (c != null && c.Enabled == false)
    System.Media.SystemSounds.Beep.Play();
}

Upvotes: 5

Ozgur Dogus
Ozgur Dogus

Reputation: 921

If you are making controls disabled manually , instead of disabling the buttons you can just change the button's style to disabled. And on click event check the button's style. If you can explain why you want that functionality we can help more maybe

Upvotes: 1

David Brabant
David Brabant

Reputation: 43619

Can't you do this in the OnMouseClick event of your form (receiving a MouseEventArgs e):

Control control = GetChildAtPoint(e.Location);

if (control != null)
{
}

From that, you can do some dirty things to retrieve the control type and its state and beep if necessary. Anyway, I'd hate to use an application beeping all the time ;-)

Upvotes: 1

Alvin Wong
Alvin Wong

Reputation: 12440

If a Button is disabled, it won't receive Click events. So the only way is enable it and use a custom flag to store the state.

Upvotes: 1

Related Questions