Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass variables to a buttons event method?

Tags:

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));          } 
like image 251
Bildsoe Avatar asked Jan 27 '11 11:01

Bildsoe


People also ask

How do you pass parameters in button click event xamarin forms?

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.

Is it possible to pass an additional parameter to an event handler?

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.

What is the use of object sender and EventArgs e parameters?

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.


2 Answers

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     } 
like image 62
Rudy Hinojosa Avatar answered Sep 20 '22 18:09

Rudy Hinojosa


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 } 
like image 34
WraithNath Avatar answered Sep 19 '22 18:09

WraithNath