IFlyHigh
IFlyHigh

Reputation: 546

Adding events to Form designer file

I created a new Event in my user control (SearchControl) like this:

//Event which is triggered on double click or Enter

public event EditRecordEventHandler EditRecord;
public delegate void EditRecordEventHandler(object sender, EventArgs e);

//Supressing the events

private bool _raiseEvents = true;
private void OnEditRecord(System.EventArgs e)
{
     if (_raiseEvents)
     {
        if (this.SearchResultGridView.FocusedRowHandle > -1)
         {
            if (EditRecord != null)
            {
                EditRecord(this, e);
             }
          }
      }
}

Now this Event is called when user double click a row in a grid. So from the properties window I selected the MouseDoubleClick Event of the grid view and called the above created EditRecord event.

private void SearchResultListGridControl_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // Check whether the user clicked on a real and not a header row or group row
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info = SearchResultGridView.CalcHitInfo(e.Location);
            if (info.InRow && !SearchResultGridView.IsGroupRow(info.RowHandle))
            {
                OnEditRecord(e);
            }

        }

Now the issue I am facing is every time I double click a row in grid view it calls the SearchResultListGridControl_MouseDoubleClick() which then calls OnEditRecord(), however the value of EditRecord is everytime null.

To solve this I checked the designer file of the Main Control which has SearchControl and could not find the EditRecord Event entry in this. So I manually created it like this:

this.MySearchControl.EditRecord += new performis.BA.Merkmalsleisten.Search.SearchControl.EditRecordEventHandle(this.MySearchControl_EditRecord);

Now the things are working fine, but my question is why it did not create it automatically at the first place? And as far I know it is not recommendable to add anything manually to the designer file..is there any other way I can do it?

Thanks

Upvotes: 1

Views: 269

Answers (2)

Sinatr
Sinatr

Reputation: 22008

When you create event it has to be used in the form designer similar to how you are using MouseDoubleClick for the Grid (so you need to find event in the Misc category, because you didn't define CategoryAttribute, double clicked there, etc).

If I understand it right you want to subscribe to event automatically, when form is created. You can do this in the control constructor (find parent form control.Parent or control.FindForm()) or perhaps in the special method, which you have to call from the form constructor, which in turn is basically similar to wiring event manually (which you did in the designer created file, but, instead, you can do in the form file, which is totally ok to edit) Up to you.

Upvotes: 1

Avi Turner
Avi Turner

Reputation: 10456

Sure. A better practice would be to add your binding line:
this.MySearchControl.EditRecord += new performis.BA.Merkmalsleisten.Search.SearchControl.EditRecordEventHandle(this.MySearchControl_EditRecord);

To the form's constructor. something like:

public MyForm() {

this.MySearchControl.EditRecord += new performis.BA.Merkmalsleisten.Search.SearchControl.EditRecordEventHandle(this.MySearchControl_EditRecord);
//The rest of your constructor.

}

Upvotes: 0

Related Questions