C#方法无法正常工作

我目前正在上一门编程课并学习C#。 我的一项任务是让程序运行一个图形用户界面,该界面有一个输入文本框,一个输出文本框和一个按钮,用于复制“输入”文本框中的任何数字并将其粘贴到输出中文本框。 出于某种原因,方法computeBtn_Click (它是读取输入中的数字并将其放入输出中的方法)对我来说computeBtn_Click_1 ,但是通过使用方法名称computeBtn_Click_1它运行得非常好。 任何想法为什么?

这是我的代码(没有使用所有系统的东西):

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        //exitToolStripMenuItem1 method
        //Purpose: To close the window and terminate the application.
        //Parameters: The object generating the event and the event arguments.
        //Returns: None
        void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //aboutToolStripMenu method
        //Purpose: To give information about the application.
        //Parameters: The object generating the event and the event arguments.
        //Returns: None
        void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("NamenCS1400nLab #4");
        }

        private void inTxtBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void outTxtBox_TextChanged(object sender, EventArgs e)
        {

        }

        // For some reason this method wasn't working.
        // computeBtn_Click Method
        // Purpose: Get a value from the user and display it back again
        // Parameters: The sending object, and the event arguments
        // Returns: none
        /*private void computeBtn_Click(object sender, EventArgs e)
        {
            int num = int.Parse(inTxtBox.Text);
            string outStr = string.Format("{0:D}", num);
            outTxtBox.Text = outStr;
        }*/

        // computeBtn_Click_1 Method
        // Purpose: Get a value from the user and display it back again
        // Parameters: The sending object, and the event arguments
        // Returns: none
        private void computeBtn_Click_1(object sender, EventArgs e)
        {
            int num = int.Parse(inTxtBox.Text);
            string outStr = string.Format("{0:D}", num);
            outTxtBox.Text = outStr;
        }
    }
}

当您创建事件处理程序时,Visual Studio会自动分配名称。 所以在你的情况下,你有一个名为computeBtn的按钮。 然后在图形设计器中为Click事件创建一个事件处理程序。 VS自动为它创建名称: computeBtn_Click

现在,如果在图形设计器中决定删除事件处理程序(例如在按钮的“属性”选项卡中),则没有任何事件挂钩到Click事件,但名为computeBtn_Click的方法仍保留在该类中。 那么如果你决定为Click事件创建一个事件处理程序会发生什么? IDE将创建一个新方法,并且由于已经有名称为computeBtn_Click东西,它将修改新方法的名称,使其仍然是唯一的。 在这种情况下会导致computeBtn_Click_1

为了确认这一点,你可以检查不起作用的方法的参考。 它将显示代码中没有对该方法的引用。 要检查引用,请单击该方法的名称,然后按Ctrl + K,R或使用上下文菜单。


您的EventHandler委托没有设置在您的部分类中调用computeBtn_Click部分类我的意思是Form1.Designer.cs可能看起来像这样

 private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(105, 30);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(170, 74);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.computeBtn_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

试试这个,当你点击按钮时你的方法会被调用

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 this.button1.Click += new System.EventHandler(computeBtn_Click);
        }
链接地址: http://www.djcxy.com/p/6071.html

上一篇: C# method failing to function for no known reason

下一篇: C# Interop not closing Excel process after execution