C# method failing to function for no known reason

I'm currently taking a programming class and learning C#. One of my assignments was to have a program run with a graphical user interface that has a text box for input, a text box for output, and a button that copies whatever number was in the "input" text box and pastes it into the output text box. For some reason the method computeBtn_Click , which was the method to read the number in the input and put that in the output, was doing absolutely nothing for me, but by using the method name computeBtn_Click_1 it worked great. Any idea why?

Here is my code(without all the using.System stuff):

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

When you create event handlers Visual Studio assigns names automatically. So in your case you have a button named computeBtn . Then in the graphical designer you create an event handler for the Click event. VS automatically creates name for it: computeBtn_Click .

Now if in the graphical designer you decide to remove the event handler (eg in Properties tab for the button) nothing is hooked to the Click event but the method named computeBtn_Click remained in the class. So what will happen if you decide to create an event handler for the Click event? IDE will create a new method and since there is already something with the name computeBtn_Click it will modify name of the new method so it is still unique. Which in this case results in computeBtn_Click_1 .

To confirm that, you can check references of the method that does not work. It will show that there are no references in the code to the method. To check the references click the method's name and hit Ctrl+K, R or use context menu.


your EventHandler delegate is not set in your partial class to call computeBtn_Click by partial class I mean the Form1.Designer.cs which may look like this

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

        }

try this and your method will be called when you click the button

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

上一篇: 如何将我的导出格式化为microsoft.office.interop.excel中的excel工作簿?

下一篇: C#方法无法正常工作