Question

I am following this exactly:

http://msdn.microsoft.com/en-us/library/ms185301.aspx

but can't get it to work. The form appears when I try and add my new item, but when I input text and click the button, nothing happens.

For posterity's sake here is my code:

The non-empty methods in the Wizard class which extends IWizard

 public void RunStarted(object automationObject,
        Dictionary<string, string> replacementsDictionary,
        WizardRunKind runKind, object[] customParams)
    {
        try
        {
            // Display a form to the user. The form collects 
            // input for the custom message.
            inputForm = new UserInputForm();
            inputForm.ShowDialog();

            customMessage = inputForm.get_CustomMessage();

            // Add custom parameters.
            replacementsDictionary.Add("$custommessage$",
                customMessage);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    // This method is only called for item templates,
    // not for project templates.
    public bool ShouldAddProjectItem(string filePath)
    {
        return true;
    }

The user input form code:

 public partial class UserInputForm : Form
{
    private string customMessage;

    public UserInputForm()
    {
        InitializeComponent();
    }

    public string get_CustomMessage()
    {
        return customMessage;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        customMessage = textBox1.Text;

        this.Dispose();
    }

}

And the button is indeed named button 1:

 this.button1.Location = new System.Drawing.Point(200, 180);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(100, 40);
        this.button1.TabIndex = 0;
        this.button1.Text = "Click Me";
        this.button1.UseVisualStyleBackColor = true;

So I don't have much experience with Windows Forms (do web apps), but I am following the directions on MSDN and it's pretty clear cut. Any suggestions? Can anyone else get this to work?

Was it helpful?

Solution

Okay I figured it out. I had to add the event handler in the form's constructor manually:

 public UserInputForm()
    {
        InitializeComponent();
        button1.Click += button1_Click;
    }

Why this isn't in the documentation on MSDN boggles my mind.

OTHER TIPS

If you use the WinForms designer mode to drag your button from the Toolbox, and then double-clicked the button in the designer view, it would have added the event handler and stubbed that Click method for you. Just FYI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top