设计师必须创建...的一个实例,因为该类型被声明为抽象的(The designer must create an instance of…cannot because the type is decl

编程入门 行业动态 更新时间:2024-10-23 15:17:12
设计师必须创建...的一个实例,因为该类型被声明为抽象的(The designer must create an instance of…cannot because the type is declared abstract)

Visual Studio抱怨: 警告1设计人员必须创建一个类型为'RentalEase.CustomBindingNavForm'的实例,但它不能,因为该类型被声明为抽象类型。

Visual Studio不会让我访问窗体的Designer。 该类已经实现了CustomBindingNavForm中的所有抽象方法。 CustomBindingNavForm提供了一些具体和抽象的函数。

有没有解决的办法?

这是班级:

public abstract class CustomBindingNavForm : SingleInstanceForm { //Flags for managing BindingSource protected bool isNew = false; protected bool isUpdating = false; /// <summary> /// This is so that when a new item is added, it sets isNew and firstPass to true. The Position Changed Event will look for /// firstPass and if it is true set it to false. Then on the next pass, it will see it's false and set isNew to false. /// This is needed because the Position Changed Event will fire when a new item is added. /// </summary> protected bool firstPass = false; protected abstract bool validateInput(); protected abstract void saveToDatabase(); //manipulating binding protected abstract void bindingSourceCancelResetCurrent(); protected abstract void bindingSourceRemoveCurrent(); protected abstract void bindingSourceMoveFirst(); protected abstract void bindingSourceMoveNext(); protected abstract void bindingSourceMoveLast(); protected abstract void bindingSourceMovePrevious(); protected abstract void bindingSourceAddNew(); public void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e) { if (validateInput()) { bindingSourceMovePrevious(); } else { DialogResult cont = MessageBox.Show(null, "There are errors in your data. Click Cancel to go back and fix them, or ok to continue. If you continue, changes will not be saved.", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (cont == DialogResult.OK) { if (isNew) { bindingSourceRemoveCurrent(); isNew = false; } else { bindingSourceCancelResetCurrent(); bindingSourceMovePrevious(); } } } } public void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) { if (validateInput()) { saveToDatabase(); bool temp = isUpdating; isUpdating = true; bindingSourceAddNew(); isUpdating = temp; isNew = true; firstPass = true; } else { DialogResult cont = MessageBox.Show(null, "There are errors in your data. Click Cancel to go back and fix them, or ok to continue. If you continue, changes will not be saved.", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (cont == DialogResult.OK) { if (isNew) { bindingSourceRemoveCurrent(); isNew = false; } else { bindingSourceCancelResetCurrent(); } bool temp = isUpdating; isUpdating = true; bindingSourceAddNew(); isUpdating = temp; isNew = true; firstPass = true; } } } public void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e) { if (validateInput()) { bindingSourceMoveFirst(); } else { DialogResult cont = MessageBox.Show(null, "There are errors in your data. Click Cancel to go back and fix them, or ok to continue. If you continue, changes will not be saved.", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (cont == DialogResult.OK) { if (isNew) { bindingSourceRemoveCurrent(); isNew = false; } else { bindingSourceCancelResetCurrent(); } bindingSourceMoveFirst(); } } } public void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e) { if (validateInput()) { bindingSourceMoveNext(); } else { DialogResult cont = MessageBox.Show(null, "There are errors in your data. Click Cancel to go back and fix them, or ok to continue. If you continue, changes will not be saved.", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (cont == DialogResult.OK) { if (isNew) { bindingSourceRemoveCurrent(); isNew = false; } else { bindingSourceCancelResetCurrent(); } bindingSourceMoveNext(); } } } }

Visual Studio complains: Warning 1 The designer must create an instance of type 'RentalEase.CustomBindingNavForm' but it cannot because the type is declared as abstract.

Visual Studio won't let me access the Designer for the form. The class already implements all abstract methods from the CustomBindingNavForm. CustomBindingNavForm provides some functions concrete and abstract.

Is there a way around this?

Here is the class:

public abstract class CustomBindingNavForm : SingleInstanceForm { //Flags for managing BindingSource protected bool isNew = false; protected bool isUpdating = false; /// <summary> /// This is so that when a new item is added, it sets isNew and firstPass to true. The Position Changed Event will look for /// firstPass and if it is true set it to false. Then on the next pass, it will see it's false and set isNew to false. /// This is needed because the Position Changed Event will fire when a new item is added. /// </summary> protected bool firstPass = false; protected abstract bool validateInput(); protected abstract void saveToDatabase(); //manipulating binding protected abstract void bindingSourceCancelResetCurrent(); protected abstract void bindingSourceRemoveCurrent(); protected abstract void bindingSourceMoveFirst(); protected abstract void bindingSourceMoveNext(); protected abstract void bindingSourceMoveLast(); protected abstract void bindingSourceMovePrevious(); protected abstract void bindingSourceAddNew(); public void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e) { if (validateInput()) { bindingSourceMovePrevious(); } else { DialogResult cont = MessageBox.Show(null, "There are errors in your data. Click Cancel to go back and fix them, or ok to continue. If you continue, changes will not be saved.", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (cont == DialogResult.OK) { if (isNew) { bindingSourceRemoveCurrent(); isNew = false; } else { bindingSourceCancelResetCurrent(); bindingSourceMovePrevious(); } } } } public void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) { if (validateInput()) { saveToDatabase(); bool temp = isUpdating; isUpdating = true; bindingSourceAddNew(); isUpdating = temp; isNew = true; firstPass = true; } else { DialogResult cont = MessageBox.Show(null, "There are errors in your data. Click Cancel to go back and fix them, or ok to continue. If you continue, changes will not be saved.", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (cont == DialogResult.OK) { if (isNew) { bindingSourceRemoveCurrent(); isNew = false; } else { bindingSourceCancelResetCurrent(); } bool temp = isUpdating; isUpdating = true; bindingSourceAddNew(); isUpdating = temp; isNew = true; firstPass = true; } } } public void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e) { if (validateInput()) { bindingSourceMoveFirst(); } else { DialogResult cont = MessageBox.Show(null, "There are errors in your data. Click Cancel to go back and fix them, or ok to continue. If you continue, changes will not be saved.", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (cont == DialogResult.OK) { if (isNew) { bindingSourceRemoveCurrent(); isNew = false; } else { bindingSourceCancelResetCurrent(); } bindingSourceMoveFirst(); } } } public void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e) { if (validateInput()) { bindingSourceMoveNext(); } else { DialogResult cont = MessageBox.Show(null, "There are errors in your data. Click Cancel to go back and fix them, or ok to continue. If you continue, changes will not be saved.", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (cont == DialogResult.OK) { if (isNew) { bindingSourceRemoveCurrent(); isNew = false; } else { bindingSourceCancelResetCurrent(); } bindingSourceMoveNext(); } } } }

最满意答案

我还没有看到城市土豆的内容(倒下),但是我和Smelch想出了一个解决方案。 Form本身是从抽象类继承的,所以他们没有告诉你的是, 它只有第一级的继承不能抽象,第二级可以。

从那里只需要在中间有一个空的类,然后在窗体声明中包装一个#if debug ,你就很好。 只要确保在发布模式和调试模式下进行设计(这是非常典型的)。

您将在设计(调试)和构建(发布)时间获得完整的设计器支持和真正的抽象基类,因为每次最终都会使用抽象基类。

完整的解释和答案在这里

I haven't seen the content at urban potato (its down) but Me and Smelch came up with a solution. Form itself inherits from an abstract class, so what they dont tell you is that its only the 1st level of inheritance that can't be abstract, the 2nd on down can.

From there its simply a matter of having an empty class in the middle and wrapping an #if debug around the forms declaration and you're good to go. Just be sure to release in release mode and design in debug mode (which is very typical).

You'll get full designer support and a real abstract base class at design (debug) and build (release) time because each time it ends up using your abstract base class.

The full explanation and answer is here

更多推荐

本文发布于:2023-08-06 15:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450369.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抽象   实例   设计师   声明   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!