C# method failing to function for no known reason
我现在正在上编程课,学习C。我的一个任务是运行一个程序,它有一个图形用户界面,其中有一个文本框用于输入,一个文本框用于输出,以及一个按钮,可以复制"输入"文本框中的任何数字,并将其粘贴到输出文本框中。出于某种原因,方法
这是我的代码(不包括所有使用.system的东西):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 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("Name CS1400 Lab #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会自动分配名称。因此,在您的例子中,有一个名为
现在,如果在图形设计器中决定删除事件处理程序(例如,在按钮的"属性"选项卡中),则不会将任何内容挂接到单击事件,但名为
要确认这一点,您可以检查不起作用的方法的引用。它将显示代码中没有对该方法的引用。要检查引用,请单击该方法的名称,然后单击ctrl+kbkbd、r或使用上下文菜单。
另外两个答案很好,但我想我可以添加另一个简单的方法来避免这个问题。在设计器中选择按钮后,转到"属性"窗口,您将在那里找到一个名为"事件"的选项卡。在"单击"下,可以选择要用于按钮的单击事件的方法。
您的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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); } |
尝试此操作,单击按钮时将调用您的方法
1 2 3 4 5 6 7 | public partial class Form1 : Form { public Form1() { InitializeComponent(); this.button1.Click += new System.EventHandler(computeBtn_Click); } |