vb.net to c# eventhandler conversion help

  • Thread starter Thread starter c#2006user
  • Start date Start date
C

c#2006user

Hi everyone, i have a vb.net program ive converted most of the code but
i know events dont translate well, the original vb.net code uses
withevents and .handles and i can't get the events in c# to work 100%

they seem to hook up ok but when the eventhandler is called from the
method its null and fails.
or maybe im reading it wrong?

anyway its quite a bit of code so i wont post but if any would help i
am willing to pay if thats ok on these boards?

regards
bryan
 
c#2006user said:
Hi everyone, i have a vb.net program ive converted most of the code but
i know events dont translate well, the original vb.net code uses
withevents and .handles and i can't get the events in c# to work 100%

they seem to hook up ok but when the eventhandler is called from the
method its null and fails.

That means they're *not* hooking up ok. In my experience, most
applications only need to hook up events when you create forms to start
with.
or maybe im reading it wrong?

anyway its quite a bit of code so i wont post but if any would help i
am willing to pay if thats ok on these boards?

Don't post your whole code, but reduce it down to a minimal program
that still shows the problem. If you still don't understand why it
doesn't work, post *that* code.

See http://www.pobox.com/~skeet/csharp/complete.html for more details.
 
thanks jon,

ill try.

protected ListControl mainList;
this.mainList.LeaveControl += new
dmControls.ListControl.LeaveControlEventHandler(mainList_LeaveControl);
this.mainList.Selected += new
dmControls.ListControl.SelectedEventHandler(mainList_Selected);
this.mainList.SelectionChanged += new
dmControls.ListControl.SelectionChangedEventHandler(mainList_SelectionChanged);

protected delegate void
MainListLeaveControlEventHandler(dmControls.Direction direction);
private MainListLeaveControlEventHandler MainListLeaveControlEvent;
protected event MainListLeaveControlEventHandler MainListLeaveControl
{
add { MainListLeaveControlEvent =
(MainListLeaveControlEventHandler)System.Delegate.Combine(MainListLeaveControlEvent,
value); }
remove { MainListLeaveControlEvent =
(MainListLeaveControlEventHandler)System.Delegate.Remove(MainListLeaveControlEvent,
value); }
}

protected delegate void MainListSelectedEventHandler(string sSelected,
object Data);
protected MainListSelectedEventHandler MainListSelectedEvent;
protected event MainListSelectedEventHandler MainListSelected
{
add { MainListSelectedEvent =
(MainListSelectedEventHandler)System.Delegate.Combine(MainListSelectedEvent,
value); }
remove { MainListSelectedEvent =
(MainListSelectedEventHandler)System.Delegate.Remove(MainListSelectedEvent,
value); }
}

protected delegate void MainListSelectionChangedEventHandler(string
sSelected, object Item);
private MainListSelectionChangedEventHandler
MainListSelectionChangedEvent;
protected event MainListSelectionChangedEventHandler
MainListSelectionChanged
{
add { MainListSelectionChangedEvent =
(MainListSelectionChangedEventHandler)System.Delegate.Combine(MainListSelectionChangedEvent,
value); }
remove { MainListSelectionChangedEvent =
(MainListSelectionChangedEventHandler)System.Delegate.Remove(MainListSelectionChangedEvent,
value); }
}


private void mainList_LeaveControl(Direction direction)
{
..........................
}

private void mainList_Selected(string sSelected, object Data)
{
if (MainListSelectedEvent != null)
MainListSelectedEvent(sSelected, Data);
}
 
thanks jon,

ill try.

public abstract class Page : IDisposable
{
protected ListControl mainList;

public virtual void DisplayPage(ref frmMain myForm)
{
this.mainList.LeaveControl += new
dmControls.ListControl.LeaveControlEventHandler(mainList_LeaveControl);

this.mainList.Selected += new
dmControls.ListControl.SelectedEventHandler(mainList_Selected);
this.mainList.SelectionChanged += new
dmControls.ListControl.SelectionChangedEventHandler(mainList_SelectionChang­ed);

}

protected delegate void
MainListLeaveControlEventHandler(dmControls.Direction direction);
private MainListLeaveControlEventHandler MainListLeaveControlEvent;
protected event MainListLeaveControlEventHandler MainListLeaveControl
{
add { MainListLeaveControlEvent =
(MainListLeaveControlEventHandler)System.Delegate.Combine(MainListLeaveCont­rolEvent,

value); }
remove { MainListLeaveControlEvent =
(MainListLeaveControlEventHandler)System.Delegate.Remove(MainListLeaveContr­olEvent,

value); }



}


protected delegate void MainListSelectedEventHandler(string sSelected,
object Data);
protected MainListSelectedEventHandler MainListSelectedEvent;
protected event MainListSelectedEventHandler MainListSelected
{
add { MainListSelectedEvent =
(MainListSelectedEventHandler)System.Delegate.Combine(MainListSelectedEvent­,

value); }
remove { MainListSelectedEvent =
(MainListSelectedEventHandler)System.Delegate.Remove(MainListSelectedEvent,

value); }


}


protected delegate void MainListSelectionChangedEventHandler(string
sSelected, object Item);
private MainListSelectionChangedEventHandler
MainListSelectionChangedEvent;
protected event MainListSelectionChangedEventHandler
MainListSelectionChanged
{
add { MainListSelectionChangedEvent =
(MainListSelectionChangedEventHandler)System.Delegate.Combine(MainListSelec­tionChangedEvent,

value); }
remove { MainListSelectionChangedEvent =
(MainListSelectionChangedEventHandler)System.Delegate.Remove(MainListSelect­ionChangedEvent,

value); }


}


private void mainList_LeaveControl(Direction direction)
{
..........................


}


private void mainList_Selected(string sSelected, object Data)
{
if (MainListSelectedEvent != null)
MainListSelectedEvent(sSelected, Data);
}

}

DisplayPage is then overridden and called from different type of pages
that inherit from page.

regards
bryan
 
oh ya its the MainListSelectedEvent thats null when i try and call it.

any help really appreciated!

thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top