C# Windows窗体设计器,NumericUpDown不包含定义
所以我得到了这个错误C# Windows窗体设计器,NumericUpDown不包含定义,c#,windows-forms-designer,C#,Windows Forms Designer,所以我得到了这个错误 “dicegui.MainForm”不包含“DiceNumericValueChanged”的定义,并且找不到接受“dicegui.MainForm”类型的第一个参数的扩展方法“DiceNumericValueChanged”(是否缺少using指令或程序集引用?)(CS1061)-C:\Users\b\Documents\Projects\dicegui\dicegui\MainForm.Designer.cs:90,66 对于我试图从中获取值的两个数值。我不完全清楚为什
“dicegui.MainForm”不包含“DiceNumericValueChanged”的定义,并且找不到接受“dicegui.MainForm”类型的第一个参数的扩展方法“DiceNumericValueChanged”(是否缺少using指令或程序集引用?)(CS1061)-C:\Users\b\Documents\Projects\dicegui\dicegui\MainForm.Designer.cs:90,66
对于我试图从中获取值的两个数值。我不完全清楚为什么。希望有人能告诉我发生了什么。这是一个相当基本的windows窗体程序,我没有做太多更改
这是mymainform.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace dicegui
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
int maxDice;
int diceType;
Random _r =new Random();
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void RollButtonClick(object sender, EventArgs e)
{
maxDice = (int) diceNumeric.Value;
diceType = (int) sidesNumeric.Value;
DiceLoop();
}
public int RandomDice()
{
int n = _r.Next (1, diceType);
return n;
}
public void DiceLoop()
{
int result = 0;
for(int i = 0; i < maxDice; i++)
{
result += RandomDice();
}
string resultS = Convert.ToString(result);
//MessageBox.Show("You rolled " + result);
}
}
}
是的,我不知道这里发生了什么,我根本没有修改mainform.designer.cs文件中的任何内容,所以在我看来它应该可以工作。将
DiceNumericValueChanged
添加到主表单中
void DiceNumericValueChanged(object sender, EventArgs e)
{
// some logic about the dice changing
}
您正在此处调用该方法:
this.diceNumeric.ValueChanged += new System.EventHandler(this.DiceNumericValueChanged);
但是在“this”中没有提到它(“this”是mainform的所有部分)
如果您不想在骰子数值更改时执行任何操作,则可以从设计器中删除引用行。您的表单类是否包含带有预期签名的
DiceNumericValueChanged
的方法定义?此方法不在问题中包含的任何代码片段中。@M.Babcock是正确的,错误的意思是您正试图调用DiceNumericValueChanged
,但此名称的方法不存在于范围中。可能不存在。我不知道它会去哪里,也不知道为什么会丢失。我以前做过有numericupdowns的教程,不需要在中添加numericvaluechanged方法。我假设它通常由windowsformdesigner生成。我不知道为什么这个节目有什么不同。我想我会浏览mainform.designer.cs文件中的一个教程,并尝试找到该方法的示例。查看我的答案。如果您不关心骰子值的更改,那么也只需删除designer.cs文件中的行,而不是查找该方法的示例。。只要使用智能。如果在.ValueChanged
之后键入+=
,然后按tab键两次,它将为您生成所有内容。谢谢!!这完全是问题所在。windowsform设计器已经自动生成了该方法,我根本不需要它。我把它注释掉了,整个程序现在运行得很好。谢谢:)
this.diceNumeric.ValueChanged += new System.EventHandler(this.DiceNumericValueChanged);