自定义项模板向导按钮单击不触发?



我正是这样做的:

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

但无法使其发挥作用。当我尝试添加新项目时,表单会出现,但当我输入文本并单击按钮时,什么都不会发生。

为了子孙后代,这里是我的代码:

Wizard类中扩展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;
    }

用户输入表单代码:

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

这个按钮确实被命名为按钮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;

所以我对Windows窗体(做网络应用程序)没有太多经验,但我遵循MSDN上的指示,这是非常清晰的。有什么建议吗?其他人能让它发挥作用吗?

好吧,我想明白了。我不得不手动在表单的构造函数中添加事件处理程序:

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

为什么MSDN上的文档中没有这一点让我感到困惑。

如果您使用WinForms设计器模式从"工具箱"拖动按钮,然后在设计器视图中双击该按钮,它将为您添加事件处理程序并清除Click方法。仅供参考。

最新更新