Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

c# - How do I pass variables to a buttons event method?

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

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

1 Reply

0 votes
by (71.8m points)

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
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...