I need to be able to pass along two objects to the method being fired when I click a button. How do I do this?
So far I've been looking at creating a changed EventArgs
:
public class CompArgs : System.EventArgs { private object metode; private Type typen; public CompArgs(object m, Type t) { this.metode = m; this.typen = t; } public object Metode() { return metode; } public Type Typen() { return typen; } }
But how would I use it? Is it possible to somehow override the click-event of the button to use a custom eventhandler
, which takes CompArgs
as a parameter?
private void button1_Click(object sender, EventArgs e) { Assembly assembly = Assembly.LoadFile(@"c:\components.dll"); int counter = 0; foreach (Type type in assembly.GetTypes()) { if (type.IsClass == true) { Button btn = new Button(); btn.Location = new Point(174 + (counter * 100),10); btn.Size = new Size(95, 23); btn.Name = type.Name; btn.Text = type.Name; btn.UseVisualStyleBackColor = true; this.Controls.Add(btn); object obj = Activator.CreateInstance(type); //I need to pass on obj and type to the btn_Click btn.Click += new eventHandler(btn_Click); counter++; } } }
And the event-method where I need it:
private void btn_Click(object sender, CompArgs ca) { MessageBox.Show((string)ca.Typen().InvokeMember("getMyName", BindingFlags.Default | BindingFlags.InvokeMethod, null, ca.Metode(), null)); }
You can't pass arguments to event handlers. You can assign handler by another way: var newIndex = index; // for avoiding closure pin. Clicked += async (s, e) => { await Navigation.
Parameters of the even handlers are defined by the "event arguments" class (derived from System. EventArgs ); and you cannot change the signature of the event handler. So, you should have asked, "can I pass additional information?". Of course you can.
EventArgs e is a parameter called e that contains the event data, see the EventArgs MSDN page for more information. Object Sender is a parameter called Sender that contains a reference to the control/object that raised the event.
Wow, you guys are making this entirely to difficult. No need for any custom classes or method overrides. In this example I just need to pass a tab index number. You can specify whatever you want, so long as your method is expecting that value type.
button.Click += (sender, EventArgs) => { buttonNext_Click(sender, EventArgs, item.NextTabIndex); }; void buttonNext_Click(object sender, EventArgs e, int index) { //your code }
Cant you just set a property or member variable on the form that hosts the button and access these from the button click event?
EDIT: custom button class suggestion after feedback comment (not the same suggestion as above)
class MyButton : Button { private Type m_TYpe; private object m_Object; public object Object { get { return m_Object; } set { m_Object = value; } } public Type TYpe { get { return m_TYpe; } set { m_TYpe = value; } } } Button1Click(object sender, EventArgs args) { MyButton mb = (sender as MyButton); //then you can access Mb.Type //and Mb.object }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With