c# - 设计者必须创建一个...不能的实例,因为该类型被声明为抽象的

标签 c# .net winforms abstract

Visual Studio 提示:警告 1 设计者必须创建类型“RentalEase.CustomBindingNavForm”的实例,但它不能创建,因为该类型被声明为抽象。

Visual Studio 不允许我访问窗体的设计器。该类已经实现了 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();
                }
            }
        }
    }

最佳答案

我还没有在 urban potato 看到内容(下)但是我和 Smelch想出了一个解决方案。 Form 本身继承自一个抽象类,所以他们没有告诉你的是它只有第一级继承不能抽象,第二级继承羽绒服。

从那里开始,只需在中间有一个空类并在表单声明周围包装一个 #if debug 就可以了。只要确保在 Release模式下发布并在 Debug模式下设计(这是非常典型的)。

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

The full explanation and answer is here

关于c# - 设计者必须创建一个...不能的实例,因为该类型被声明为抽象的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/481305/

相关文章:

c# - WinForms 中的进度条

c# - MYSQL 连接到 C# 窗口应用程序

c# - 如何在 WinForm 的 DataGrid 中创建可编辑的数据绑定(bind)组合框?

c# - 如何通过 roslyn 检查两种方法是否匹配签名?

c# - 仅当文件大时,使用 PostAsync 的 Web API 才会抛出异常

c# - ListView.SelectedIndices.Count 多选时为零

c# - HTTPWebResponse + StreamReader 非常慢

c# - PHP、Javascript 或 C# 中的微分或无限集成?

c# - 添加具有多个属性的产品变体

c# - 无法使用 WebResponse c# 访问此页面