| re: Help Needed! Really Stuck. C# dvents & null delegates
- You never attach a handler to the event MainListSelected.
- I see no reason for all the ref parameters.
You only need ref for a form if you want to write use myForm as an
L-value NOT when you are just calling its members or properties.
- You've gone to a lot of trouble to write code that the compiler will do
for you - All you need is:
public event MainListSelectedEventHandler MainListSelected;
public void mainList_Selected(string sSelected, object Data)
{
//........
if (MainListSelectedEvent != null)
MainListSelectedEvent(.....);
}
The compiler creates the delegate and add and remove methods.
- You have commented out an initialization of your delegate which would have
assigned it the method that actually calls it - this would have been
infinitely recursive - I have no idea what you really intended.
- IMHO the whole thing is a bit of a mess because of your naming - You have
something that isn't an event called MainListSelectedEvent; You have an
event called Selected on something called mainList and an event called
MainListSelected on the page.
"c#2006user" <htpc_2006@hotmail.com> wrote in message
news:1142820289.523006.119370@g10g2000cwb.googlegr oups.com...[color=blue]
> Hi everyone.
>
> i know there is a lot of code and ive reduced it as much as i could,
> its a bit much to ask but i am really stuck!
> i've been at this for a week! this is converted from vb to c# and
> something has gone wrong with the events as the original code used
> "WithEvents" and "Handles" keywords.
>
> basically when i debug and get to the mainList_Selected(.............)
> method the MainListSelectedEvent is null and i think this is where the
> problem is.
>
> if anyone could help id be really greatful!!
>
> This is the main class and this is the method that is called
>
> private void displayHomePage()
> {
> try
> {
> Page pg;
> while (history.Count > 0)
> {
> pg = (Page) history.Pop();
> pg.Dispose();
> }
> if (currentPage != null)
> {
> currentPage.Dispose();
> currentPage = null;
> }
> HomePage newPage = new HomePage();
> currentPage = newPage;
> MusicXP.frmMain X = this;
> newPage.DisplayPage(ref X);
> }
> catch (System.Exception ex)
> {
> ErrorHandler.GenericHandler(ex);
> }
>
> }
>
> ----------------------------------------------------------------------------------------------------------------------------------
>
> public abstract class Page : IDisposable
> {
> protected dmControls.imageList albumList;
> protected dmControls.detailImageList detailList;
> protected dmControls.ListControl mainList;
> protected dmControls.ListControl sideList;
> protected dmControls.VisKeys softKeyboard;
> protected frmMain theForm;
> protected int albumListSelectedIndex;
> protected int detailListSelectedIndex;
> protected int mainListSelectedIndex;
> protected int sideListSelectedIndex;
> protected string hasFocus;
> protected string softKeyboardText;
>
> //I took out the main events and delegates that were created in
> the conversion, this i think is where the problem
> //The MainListSelectedEvent is Null When it is called in the
> mainList_Selected.
>
> public delegate void MainListSelectedEventHandler(string
> sSelected, object Data);
> public MainListSelectedEventHandler MainListSelectedEvent; //=
> new MainListSelectedEventHandler(mainList_Selected);
> public event MainListSelectedEventHandler MainListSelected
> {
> add { MainListSelectedEvent =
> (MainListSelectedEventHandler)System.Delegate.Comb ine(MainListSelectedEvent,
> value); }
> remove { MainListSelectedEvent =
> (MainListSelectedEventHandler)System.Delegate.Remo ve(MainListSelectedEvent,
> value); }
> }
>
> public void mainList_Selected(string sSelected, object Data)
> {
> // object tmpObject = new object();
> //Data = tmpObject;
> Sounds.PlaySelectSound();
> if (MainListSelectedEvent != null)
> MainListSelectedEvent(sSelected, Data);
> }
>
> "The Rest of the Delegates are in here"
> "The Rest of the Event are in here"
>
> public virtual void DisplayPage(ref frmMain myForm)
> {
> myForm.currentPage = this;
> this.albumList = myForm.albumList;
> this.detailList = myForm.detailList;
> this.mainList = myForm.listNavigation;
> this.sideList = myForm.sideNavigation;
> this.softKeyboard = myForm.softKeyboard;
> this.theForm = myForm;
>
>
> this.mainList.LeaveControl += new
> dmControls.ListControl.LeaveControlEventHandler(ma inList_LeaveControl);
> this.mainList.Selected += new
> dmControls.ListControl.SelectedEventHandler(mainLi st_Selected);
> this.mainList.SelectionChanged += new
> dmControls.ListControl.SelectionChangedEventHandle r(mainList_SelectionChanged);
>
>
> "the Rest of the assignment are in here.............."
>
>
> }
>
> public Page()
> {
>
> }
>
> public void Dispose()
> {
> this.Dispose();
> }
> }
>
>
>
> ---------------------------------------------------------------------------------------------------------------------------------------------------
>
> class HomePage : NavigationPage
> {
>
> public HomePage()
> {
>
> string[] navArray = new string[6];
> navArray[0] = "Recent Albums";
> navArray[1] = "Browse All Albums";
> navArray[2] = "Browse Artists";
> navArray[3] = "Playlists";
> navArray[4] = "Scan Music Files";
> navArray[5] = "Settings";
>
> base.navArray = navArray;
> base.itemCount = navArray.GetLength(0);
> //base.mainList.Selected += new
> dmControls.ListControl.SelectedEventHandler(mainLi st_Selected);
>
>
> }
>
> private void HomePage_MainListSelected(string sSelected, object
> Data)
> {
> stuff in here............................
> }
>
> public override void SavePageState(ref frmMain myForm)
> {
> base.SavePageState(ref myForm);
> }
>
> public override void DisplayPage(ref frmMain myForm)
> {
> base.DisplayPage(ref myForm);
> myForm.wmp.Visible = true;
> myForm.SetUpHomeView();
> }
>
> }
> }
>[/color] |