C# Learner <cs****@learner.here> wrote:
How can I "invoke" a method containing parameters?
Use the form of Invoke which takes two parameters - the first is the
delegate to invoke, the second is the parameter list (as an object
array). Here's a short but complete example:
using System;
using System.Drawing;
using System.Windows.Forms;
public class MainForm : Form
{
delegate void D(char c);
MainForm()
{
SuspendLayout();
Size = new Size (200, 80);
Location = new Point (100, 100);
Button b = new Button();
b.Location = new Point (50, 20);
b.Size = new Size(100, 20);
b.Text = "Click me";
b.Click += new EventHandler (ButtonClicked);
Controls.Add(b);
ResumeLayout();
}
void ButtonClicked (object sender, EventArgs e)
{
D d = new D(Invokee);
Invoke (d, new object[]{'x'});
}
static void Main()
{
Application.Run (new MainForm());
}
void Invokee(char c)
{
this.Text += c;
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too