Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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));

        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
117 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...