0

我想要 btn1.Click 上的以下内容:

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

尽管 btn1 被禁用,有没有办法播放哔声?

实际上,默认情况下我需要这样的东西:

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

4 回答 4

5

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();
}
于 2012-06-20T11:02:57.593 回答
1

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

于 2012-06-20T11:00:55.317 回答
1

如果 Button 被禁用,它将不会接收 Click 事件。所以唯一的方法是启用它并使用自定义标志来存储状态。

于 2012-06-20T10:48:17.067 回答
1

您不能在表单的 OnMouseClick 事件中执行此操作(接收 MouseEventArgs e):

Control control = GetChildAtPoint(e.Location);

if (control != null)
{
}

从那里,您可以做一些肮脏的事情来检索控件类型及其状态,并在必要时发出哔哔声。无论如何,我讨厌使用一直发出哔哔声的应用程序 ;-)

于 2012-06-20T10:56:23.610 回答