Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I code for fun, and i am not a professional coder.
I make this custom event in a simple class that should be a Menu. But i can't make my custom click event like the traditional Button click event.
In form1, when i access the Menu button event, is working fine as it is, but without mouse (left,right) buttons functionality. I want that.
Here is how it looks now: void MenuButon_MausicaClick()
and how i want it to be: void MenuButon_MausicaClick(object sender, EventArgs e)
I've tried diferent permutations, guessing basicly where to add "(object sender, EventArgs e)" or "EventHandler" inside my custom class, but i get a ton of errors so i give up.

What I have tried:

C#
       //simple external Menu class with custom event
       public delegate void ButtonClickedika_delegate();
       public event ButtonClickedika_delegate MausicaClick;
       void button1_Click(object sender, EventArgs e)
       {
           if (MausicaClick != null) { MausicaClick(); }
       }

       //In Form1
       public Form1()
       {
           InitializeComponent();
MenuButon.MausicaClick += new Menu_dll.Top.ButtonClickedika_delegate(MenuButon_MausicaClick);
       }

       //should be: void MenuButon_MausicaClick(object sender, EventArgs e)
       void MenuButon_MausicaClick() //<<how to add "EventArgs e"
       {
               if (e.Button == System.Windows.Forms.MouseButtons.Left)
               {
               }
       }
Posted
Updated 1-Dec-19 5:52am

1 solution

change event type of your custom class to you dont need any custom delegate
public event System.EventHandler<System.EventArgs> MausicaClick

and when you will subscribe it it will give you
MenuButon_MausicaClick(object sender, EventArgs e)
 
Share this answer
 
v2
Comments
_Q12_ 1-Dec-19 11:58am    
if (MausicaClick != null) { MausicaClick(); }
//Error 1 The name 'MausicaClick' does not exist in the current context
[no name] 1-Dec-19 12:02pm    
public event System.EventHandler<system.eventargs> MausicaClick
remove the custom delegate as you are using inbuilt eventhandler
if(MausicaClick != null)
{
MausicaClick(null, null); //if you have something to send from source class then send sender as object
}
_Q12_ 1-Dec-19 12:10pm    
i failed to mention that i already have some code that must run inside this event from my Menu class.
I deleted my old delegate, added your new delegate, but I cant reference this new delegate with the button that is inside in this menu class, that already is doing some important stuff(in the background).

Make me the entire code, that is running some stuff inside the Menu class in a click event, and have the eventargs click functionality, and how is declared in Form1. Use my original code i posted, keep it simple, it should be a few lines of code for you, and not that much trouble. Thank you!

//this is how it looks exactly right now:
public event System.EventHandler<system.eventargs> ButtonClickedika_delegate;
public void button1_Click(object sender, EventArgs e)
{
isDC = true;
iswire = false;
isrezistor = false;
isled = false;
if (MausicaClick != null) { MausicaClick(); }
}
_Q12_ 1-Dec-19 12:18pm    
i just give you 5stars but ill accept the solution if the code is working in my favor :)
[no name] 1-Dec-19 23:04pm    
make sure your Menu class delegate also adopted i.e. ButtonClickedika_delegate from menu_dll module also have same type of delegate defined.
you can change either your delete too have eventargs as parameter.
public event EventHandler<eventargs> MausicaClick;
OR

public delegate void ButtonClickedika_delegate(object sender, EventArgs args);

If you can send me your all classes here then i may able to help you. but this is what my understanding by creating MenuClass.

public partial class Form1 : Form
{
public event EventHandler<eventargs> MausicaClick;
private MenuButton MenuButon = new MenuButton();
void button1_Click(object sender, EventArgs e)
{
MenuButon_MausicaClick(null, null);
}

//In Form1
public Form1()
{
InitializeComponent();
MenuButon.MausicaClick += MenuButon_MausicaClick;
}

private void MenuButon_MausicaClick(object sender, EventArgs args)
{
throw new NotImplementedException();
}
}

and some menuclass created for this, may be not the same but just for refernce.
public class MenuButton
{
public event EventHandler<eventargs> MausicaClick;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900