开发者

Observe event created through an instance of a Winforms UserControl

In my winforms app, I have a UserControl that contains a DataGridView. I instantiate and load this UserControl when needed into a panel in my Main Form (frmMa开发者_C百科in). My problem is figuring out how to resond to or listen for events raised in my UC's DataGridView. For example, I want to handle the CellDoubleClick event of the DataGridView in my Main Form rather than through the UC.

Is this possible? I had thought of updating a property when the cell in the grid is double-clicked, and then let my Main form do whatever when that property changes - therefore I thought of using INotifyPropertyChanged. Im not heavily clued up on how to use it in m scenario however, and would deeply appreciate some help in this regard, or if anyone can suggest an alternate solution.

Much thanx!


Your user control must encapsulate some logic, so if you want to handle event of the DataGridView that is in your control the way you've described, you probably missing something in idea of user controls and encapsulation. Technically here two ways to do this:

  1. Make a public property in your user control of type DataGridView.
  2. Make an event wrapper. You will need to create an event in your user control that is raised when DataGridView CellDoubleClick (or any) is rased and in your calling code you will handle this event wrapper.

The second approach is more logical, cos internal logic of your control is incapsulated and you can provide end-user of you component with more logical and meaningful event then CellDoubleClidk or else.


thank u 4 your reply. Sorry for not responding earlier. I did manage to sort this issue out by creating a public event in my UC:

public event DataGridViewCellEventHandler GridRowDoubleClick {
    add { dgvTasks.CellDoubleClick += value; }
    remove { dgvTasks.CellDoubleClick -= value; }
}

and in my main form, after I instantiate and load the UC

_ucTask.GridRowDoubleClick += new DataGridViewCellEventHandler(TasksGrid_CellDoubleClick);

with the following attached event:

private void TasksGrid_CellDoubleClick( object sender, DataGridViewCellEventArgs e ) {
    // do work here!
}

This does work, although I don't know if any of u experts out there foresee a problem with this approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜