English 中文(简体)
有可能从用户控制处发现一个表格型磁力。
原标题:Is it possible to detect a Form mouseclick from a User Control

我建立了用户控制,希望在用户点击表格时能够发现。

我看到。 问题涉及但建议使用<代码> 离开活动总是不做我想要做的事情,因为当用户点击表格时,重点不一定会改变(我的控制可能是对形式的唯一控制,在这种情况下,重点仍然由我控制)。

任何想法?

我想能够在用户控制范围内做这样的事情:

Private Sub ParentForm_Click(sender As Object, e As System.EventArgs) _
    Handles Me.Parent.Click

End Sub
最佳回答

我将稍作改动:

Private _form As Control

Private Sub UserControl_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ParentChanged

    If _form IsNot Nothing Then
        RemoveHandler _form.Click, AddressOf ParentOnClick
    End If

    _form = Me.FindForm()

    If _form IsNot Nothing Then
        AddHandler _form.Click, AddressOf ParentOnClick
    End If

End Sub

Private Sub ParentOnClick(ByVal sender As Object, ByVal e As EventArgs)
     ...
End Sub

这给它带来了更微薄的 res力——如果它不是形式的直接子女,如果其父母发生变化的话。

问题回答

我对我感兴趣的任何人来说,是怎样做的:

Private _parentForm As Form

Private Sub UserControl_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    _parentForm = CType(Parent, Form)
    AddHandler _parentForm.Click, AddressOf ParentForm_Click
End Sub

Private Sub ParentForm_Click(sender As Object, e As System.EventArgs)
    debug.writeline("Parent form clicked")
End Sub




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签