none
Minimize code in MouseClick event using "sender" gives warning ??? RRS feed

  • Question

  • I have a form with 3 panels for the user to mouseclick on as a simple game.


    I turn the Tag property on each of them has randomly been given a value=1.


    If the user clicks on the Panel with the Tag-value =1 he wins the game.


    To minimize the code I want to use the "sender" to determine the Tag value of the clicked panel  and I made this handler:


            private void MouseClick(object sender, MouseEventArgs e) {
                if ((int)(sender as Panel).Tag == 1) {
                    MessageBox.Show("Ja det var her");
                    opdaterVundne();
                }
                else { MessageBox.Show("Forkert !"); }
                nytSpil();
            }


    and it Works nicely allthough I get this warning:


    Warning 1 'Jonatan_gætteleg.Form1.MouseClick(object, System.Windows.Forms.MouseEventArgs)' hides inherited member 'System.Windows.Forms.Control.MouseClick'. Use the new keyword if hiding was intended. C:\Users\ksor\Documents\01 Visual Studio 2008\Projects\Jonatan gætteleg\Jonatan gætteleg\Form1.cs 45 22


    What should I do to avoid this warning - it says something about using the "new keyword" - what/where... I don't get it !

    Sunday, November 26, 2017 3:58 PM

Answers

  • Hi,

    Your class Form1 inherits from System.Windows.Forms.Control class which also has MouseClick member.

    Newly created MouseClick method will 'hide' the inherited/original MouseClick and be used instead. If this was your intention add new keyword in method declaration:

    private new void MouseClick(object sender, MouseEventArgs e)

    Otherwise use different name.

    More information here.

    Sunday, November 26, 2017 6:12 PM