none
遇到个c#问题请高人指引“若要在加载设计器前避免可能发生的数据丢失,必须纠正以下错误: ” RRS feed

  • 问题

  •   若要在加载设计器前避免可能发生的数据丢失,必须纠正以下错误:  
     
     
         
     
     
        1 个错误  
     
      为什么会看到此页?  
     
     
     
     
      文件中的类都不能进行设计,因此未能为该文件显示设计器。设计器检查出文件中有以下类: ContriveForm --- 无法加载基类“BillSystem.BaseForm”。请确保已引用该程序集并已生成所有项目。
     
     
     
       
     
    此错误的实例(1)
     
    1。  
    在 System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)
    在 System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
    在 Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
    在 System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)
     
    有关此错误的帮助
     
     
     
    有关此错误的论坛文章
     
     
    我写的代码没有错,我是一个窗体继承另一个窗体就出现这个问题。。。。。
    • 已移动 Sheng Jiang 蒋晟Moderator 2009年9月27日 20:04 Windows表单类库设计器问题 (发件人:一般性问题讨论区)
    2009年9月17日 16:15

答案

  • 你好!
         重新生成以后,把ContriveForm那个设计器关闭,然后在重新打开就好了!
    周雪峰
    2009年9月18日 10:31
    版主

全部回复

  • winform吧 这个错误最常见 贴出你的代码

    2009年9月18日 3:35
  • 你好!
         当 Visual Studio 无法找到满足设计器要求的基类时,就会出现此错误。
         窗体和控件必须从支持设计器的基类派生。如果从正在执行的项目中的某些对象(如继承的窗体或控件)派生,则需确保通过选择“生成”菜单中的“生成解决方案”来生成此项目。
    周雪峰
    2009年9月18日 4:33
    版主
  • 我生成好几遍了没有用。。

    2009年9月18日 8:09
  • using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
    namespace BillSystem
    {
        /// <summary>
        /// 票据设计模块
        /// </summary>
        public partial class ContriveForm : BaseForm
        {
            public ContriveForm()
            {
                InitializeComponent();
            }
            public Control cl = null;
            List<Point> locations = new List<Point>();//存储坐标
            List<String> content = new List<string>();//存内容
            String txt = "";
            int x, y;
            bool MouseD = false;
            public MainForm mf = null;
            //load事件
            private void BaseForm_Load(object sender, EventArgs e)
            {
                Model.Click += new EventHandler(Controls_Click);
            }
            //设置控件
            public void SetControl(Control cl)
            {
                Type type = cl.GetType();
                if (type.Name == "TextBox")
                {
                    TextBox temp = (TextBox)cl;
                    temp.Multiline = true;
                }
                cl.Click += new EventHandler(Controls_Click);//注册事件
                cl.MouseDown += new MouseEventHandler(tb_MouseDown);
                cl.MouseMove += new MouseEventHandler(tb_MouseMove);
                cl.MouseUp += new MouseEventHandler(tb_MouseUp);
                Model.Controls.Add(cl);
            }
            //控件单击事件
            private void Controls_Click(object sender, EventArgs e)
            {
                cl = (Control)sender;
                txtName.Text = cl.Name;
                numWidth.Value = cl.Width;
                numHeight.Value = cl.Height;
            }
            //位置调整
            private void btn_Click(object sender, EventArgs e)
            {
                txt = ((Button)sender).Text;
                if (cl != null)
                {
                    setLocation();
                }
            }
            //鼠标按下
            private void btn_MouseDown(object sender, MouseEventArgs e)
            {
                txt = ((Button)sender).Text;
                if (cl != null)
                {
                    MoveTimer.Enabled = true;
                }
            }
            //鼠标放开
            private void btn_MouseUp(object sender, MouseEventArgs e)
            {
                MoveTimer.Enabled = false;
            }
            //快速位置移动
            private void MoveTimer_Tick(object sender, EventArgs e)
            {
                      setLocation();
            }
            //位置调整
            private void setLocation()
            {
                switch (txt)
                {
                    case "上":
                        cl.Location = new Point(cl.Location.X, cl.Location.Y - 1);
                        break;
                    case "下":
                        cl.Location = new Point(cl.Location.X, cl.Location.Y + 1);
                        break;
                    case "左":
                        cl.Location = new Point(cl.Location.X - 1, cl.Location.Y);
                        break;
                    case "右":
                        cl.Location = new Point(cl.Location.X + 1, cl.Location.Y);
                        break;
                }
            }
            //打印预览
            private void btnYl_Click(object sender, EventArgs e)
            {
                mf.PrintPreview(this);
            }
            //打印
            private void btnPrint_Click(object sender, EventArgs e)
            {
                mf.Print(this);
            }
            //调整宽
            private void numWidth_ValueChanged(object sender, EventArgs e)
            {
                cl.Width = Convert.ToInt32(numWidth.Value);
            }
            //调整高
            private void numHeight_ValueChanged(object sender, EventArgs e)
            {
                cl.Height = Convert.ToInt32(numHeight.Value);
            }
            //鼠标按下
            private void tb_MouseDown(object sender, MouseEventArgs e)
            {
                //计算位差
                x = Cursor.Position.X - ((Control)sender).Location.X;
                y = Cursor.Position.Y - ((Control)sender).Location.Y;
                MouseD = true;
            }
            //移动
            private void tb_MouseMove(object sender, MouseEventArgs e)
            {
                if (MouseD)
                    ((Control)sender).Location = new Point(Cursor.Position.X - x, Cursor.Position.Y - y);
            }
            //放开
            private void tb_MouseUp(object sender, MouseEventArgs e)
            {
                MouseD = false;
            }
            //选择文件
            private void btnSetBg_Click(object sender, EventArgs e)
            {
                try
                {
                    ofd.FileName = "";
                    ofd.ShowDialog();
                    String url = ofd.FileName;
                    if (url == "")
                    {
                        return;
                    }
                    Image img = Image.FromFile(url);

                    Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen;


                    if (img.Height < 150 || img.Width < 150 || img.Width > rect.Width || img.Height > rect.Height)
                    {
                        DialogResult isok = MessageBox.Show("你选择的图片太大或太小了,这样会导致无法编辑。\n你确定要导入吗?",
                            "提示", MessageBoxButtons.YesNo);
                        if (isok != DialogResult.Yes)
                            return;
                    }
                    setBgImage(img);

                }
                catch
                {
                    MessageBox.Show("设置背景错误,请确定文件格式正确.", "警告");
                }
            }
            //设置背景图片
            private void setBgImage(Image img)
            {
                this.Width = img.Width;
                this.Height = img.Height + panelMenu.Height + 30;
                Model.Width = img.Width - 20;
                Model.Height = img.Height - 20;
                Model.Location = new Point(10, 10);
                this.BackgroundImage = img;
            }
            //添加组件
            private void btnAdd_Click(object sender, EventArgs e)
            {
                NewControlForm ncf = new NewControlForm();
                ncf.cf = this;
                ncf.Show();
            }
            //修改组件名
            private void btnUpdate_Click(object sender, EventArgs e)
            {
                if (txtName.Text == "")
                {
                    MessageBox.Show("名称不能为空", "提示");
                    return;
                }
                if (cl == Model)
                {
                    MessageBox.Show("主模板不允许修改", "提示");
                    return;
                }
                cl.Name = txtName.Text;

                MessageBox.Show("修改成功", "提示");

            }
            //删除
            private void btnDel_Click(object sender, EventArgs e)
            {
                if (cl == null)
                {
                    MessageBox.Show("请先选中要删除的组件", "提示");
                    return;
                }
                else
                {
                    if (cl == Model)
                        MessageBox.Show("主面板不能删除", "警告");
                    else Model.Controls.Remove(cl);
                }
            }
            //打开
            private void benOpen_Click(object sender, EventArgs e)
            {
                if (Model.Controls.Count > 0)
                {
                 DialogResult dr=MessageBox.Show("建议您先保存当前的数据,要继续吗?", "提示", MessageBoxButtons.YesNo);
                 if (dr != DialogResult.Yes)
                     return;
                }
                try
                {
                    ofd.FileName = "";
                    ofd.ShowDialog();
                    String url = ofd.FileName;
                    if (url == "")
                    {
                        return;
                    }
                    Model.Controls.Clear();
                    FileStream fs = new FileStream(url, FileMode.Open);
                    BinaryFormatter bf = new BinaryFormatter();
                   Save data = (Save)bf.Deserialize(fs);
                   fs.Close();
                   setBgImage(data.img);
                   Model.Location = data.ModelPoint;
                   Model.Size = data.ModelSize;
                   for (int i = 0; i < data.clName.Count; i++)
                   {
                       Control c = getControl(data.clType[i], data.clName[i]);
                       c.Location = new Point(data.clPoint[i].X, data.clPoint[i].Y);
                       c.Size = data.clSize[i];
                       c.TabIndex = i+1;
                       SetControl(c);

                   }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("打开文件失败,原因:" + ex.Message, "提示");
                }
            }
            //保存
            private void btnSave_Click(object sender, EventArgs e)
            {
                saveFD.ShowDialog();
                if (saveFD.FileName == "") return;
                Save sa = new Save();
                sa.save(this);
            }
            //创建控件
            public Control getControl(String type,String name)
            {
                switch (type)
                {
                    case "TextBox":
                        TextBox tb = new TextBox();
                        if (name == "年") tb.Text = DateTime.Today.Year.ToString();
                        if (name == "月") tb.Text = DateTime.Today.Month.ToString();
                        if (name == "日") tb.Text = DateTime.Today.Day.ToString();
                        tb.Name = name;
                        return tb;
                    case "CheckBox":
                        CheckBox cb = new CheckBox();
                        cb.UseVisualStyleBackColor = true;
                        cb.AutoSize = true;
                        cb.Name = name;
                        return cb;
                }
                return null;
                       
            }

        }
    }
    下面是继承的
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace BillSystem
    {
        public partial class BaseForm : Form
        {
            public BaseForm()
            {
                InitializeComponent();
            }
            Point p;
            private void BaseForm_Load(object sender, EventArgs e)
            {
                p = pnMenu.Location;
            }

            private void BaseForm_Scroll(object sender, ScrollEventArgs e)
            {
                pnMenu.Location = p;
            }
        }
    }

    2009年9月18日 8:14
  • 周前辈问题是有时候可以继承有时候又不可以。。。我很郁闷

    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
    namespace BillSystem
    {
        /// <summary>
        /// 票据设计模块
        /// </summary>
        public partial class ContriveForm : BaseForm
        {
            public ContriveForm()
            {
                InitializeComponent();
            }
            public Control cl = null;
            List<Point> locations = new List<Point>();//存储坐标
            List<String> content = new List<string>();//存内容
            String txt = "";
            int x, y;
            bool MouseD = false;
            public MainForm mf = null;
            //load事件
            private void BaseForm_Load(object sender, EventArgs e)
            {
                Model.Click += new EventHandler(Controls_Click);
            }
            //设置控件
            public void SetControl(Control cl)
            {
                Type type = cl.GetType();
                if (type.Name == "TextBox")
                {
                    TextBox temp = (TextBox)cl;
                    temp.Multiline = true;
                }
                cl.Click += new EventHandler(Controls_Click);//注册事件
                cl.MouseDown += new MouseEventHandler(tb_MouseDown);
                cl.MouseMove += new MouseEventHandler(tb_MouseMove);
                cl.MouseUp += new MouseEventHandler(tb_MouseUp);
                Model.Controls.Add(cl);
            }
            //控件单击事件
            private void Controls_Click(object sender, EventArgs e)
            {
                cl = (Control)sender;
                txtName.Text = cl.Name;
                numWidth.Value = cl.Width;
                numHeight.Value = cl.Height;
            }
            //位置调整
            private void btn_Click(object sender, EventArgs e)
            {
                txt = ((Button)sender).Text;
                if (cl != null)
                {
                    setLocation();
                }
            }
            //鼠标按下
            private void btn_MouseDown(object sender, MouseEventArgs e)
            {
                txt = ((Button)sender).Text;
                if (cl != null)
                {
                    MoveTimer.Enabled = true;
                }
            }
            //鼠标放开
            private void btn_MouseUp(object sender, MouseEventArgs e)
            {
                MoveTimer.Enabled = false;
            }
            //快速位置移动
            private void MoveTimer_Tick(object sender, EventArgs e)
            {
                      setLocation();
            }
            //位置调整
            private void setLocation()
            {
                switch (txt)
                {
                    case "上":
                        cl.Location = new Point(cl.Location.X, cl.Location.Y - 1);
                        break;
                    case "下":
                        cl.Location = new Point(cl.Location.X, cl.Location.Y + 1);
                        break;
                    case "左":
                        cl.Location = new Point(cl.Location.X - 1, cl.Location.Y);
                        break;
                    case "右":
                        cl.Location = new Point(cl.Location.X + 1, cl.Location.Y);
                        break;
                }
            }
            //打印预览
            private void btnYl_Click(object sender, EventArgs e)
            {
                mf.PrintPreview(this);
            }
            //打印
            private void btnPrint_Click(object sender, EventArgs e)
            {
                mf.Print(this);
            }
            //调整宽
            private void numWidth_ValueChanged(object sender, EventArgs e)
            {
                cl.Width = Convert.ToInt32(numWidth.Value);
            }
            //调整高
            private void numHeight_ValueChanged(object sender, EventArgs e)
            {
                cl.Height = Convert.ToInt32(numHeight.Value);
            }
            //鼠标按下
            private void tb_MouseDown(object sender, MouseEventArgs e)
            {
                //计算位差
                x = Cursor.Position.X - ((Control)sender).Location.X;
                y = Cursor.Position.Y - ((Control)sender).Location.Y;
                MouseD = true;
            }
            //移动
            private void tb_MouseMove(object sender, MouseEventArgs e)
            {
                if (MouseD)
                    ((Control)sender).Location = new Point(Cursor.Position.X - x, Cursor.Position.Y - y);
            }
            //放开
            private void tb_MouseUp(object sender, MouseEventArgs e)
            {
                MouseD = false;
            }
            //选择文件
            private void btnSetBg_Click(object sender, EventArgs e)
            {
                try
                {
                    ofd.FileName = "";
                    ofd.ShowDialog();
                    String url = ofd.FileName;
                    if (url == "")
                    {
                        return;
                    }
                    Image img = Image.FromFile(url);

                    Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen;


                    if (img.Height < 150 || img.Width < 150 || img.Width > rect.Width || img.Height > rect.Height)
                    {
                        DialogResult isok = MessageBox.Show("你选择的图片太大或太小了,这样会导致无法编辑。\n你确定要导入吗?",
                            "提示", MessageBoxButtons.YesNo);
                        if (isok != DialogResult.Yes)
                            return;
                    }
                    setBgImage(img);

                }
                catch
                {
                    MessageBox.Show("设置背景错误,请确定文件格式正确.", "警告");
                }
            }
            //设置背景图片
            private void setBgImage(Image img)
            {
                this.Width = img.Width;
                this.Height = img.Height + panelMenu.Height + 30;
                Model.Width = img.Width - 20;
                Model.Height = img.Height - 20;
                Model.Location = new Point(10, 10);
                this.BackgroundImage = img;
            }
            //添加组件
            private void btnAdd_Click(object sender, EventArgs e)
            {
                NewControlForm ncf = new NewControlForm();
                ncf.cf = this;
                ncf.Show();
            }
            //修改组件名
            private void btnUpdate_Click(object sender, EventArgs e)
            {
                if (txtName.Text == "")
                {
                    MessageBox.Show("名称不能为空", "提示");
                    return;
                }
                if (cl == Model)
                {
                    MessageBox.Show("主模板不允许修改", "提示");
                    return;
                }
                cl.Name = txtName.Text;

                MessageBox.Show("修改成功", "提示");

            }
            //删除
            private void btnDel_Click(object sender, EventArgs e)
            {
                if (cl == null)
                {
                    MessageBox.Show("请先选中要删除的组件", "提示");
                    return;
                }
                else
                {
                    if (cl == Model)
                        MessageBox.Show("主面板不能删除", "警告");
                    else Model.Controls.Remove(cl);
                }
            }
            //打开
            private void benOpen_Click(object sender, EventArgs e)
            {
                if (Model.Controls.Count > 0)
                {
                 DialogResult dr=MessageBox.Show("建议您先保存当前的数据,要继续吗?", "提示", MessageBoxButtons.YesNo);
                 if (dr != DialogResult.Yes)
                     return;
                }
                try
                {
                    ofd.FileName = "";
                    ofd.ShowDialog();
                    String url = ofd.FileName;
                    if (url == "")
                    {
                        return;
                    }
                    Model.Controls.Clear();
                    FileStream fs = new FileStream(url, FileMode.Open);
                    BinaryFormatter bf = new BinaryFormatter();
                   Save data = (Save)bf.Deserialize(fs);
                   fs.Close();
                   setBgImage(data.img);
                   Model.Location = data.ModelPoint;
                   Model.Size = data.ModelSize;
                   for (int i = 0; i < data.clName.Count; i++)
                   {
                       Control c = getControl(data.clType[i], data.clName[i]);
                       c.Location = new Point(data.clPoint[i].X, data.clPoint[i].Y);
                       c.Size = data.clSize[i];
                       c.TabIndex = i+1;
                       SetControl(c);

                   }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("打开文件失败,原因:" + ex.Message, "提示");
                }
            }
            //保存
            private void btnSave_Click(object sender, EventArgs e)
            {
                saveFD.ShowDialog();
                if (saveFD.FileName == "") return;
                Save sa = new Save();
                sa.save(this);
            }
            //创建控件
            public Control getControl(String type,String name)
            {
                switch (type)
                {
                    case "TextBox":
                        TextBox tb = new TextBox();
                        if (name == "年") tb.Text = DateTime.Today.Year.ToString();
                        if (name == "月") tb.Text = DateTime.Today.Month.ToString();
                        if (name == "日") tb.Text = DateTime.Today.Day.ToString();
                        tb.Name = name;
                        return tb;
                    case "CheckBox":
                        CheckBox cb = new CheckBox();
                        cb.UseVisualStyleBackColor = true;
                        cb.AutoSize = true;
                        cb.Name = name;
                        return cb;
                }
                return null;
                       
            }

        }
    }
    下面是继承的
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace BillSystem
    {
        public partial class BaseForm : Form
        {
            public BaseForm()
            {
                InitializeComponent();
            }
            Point p;
            private void BaseForm_Load(object sender, EventArgs e)
            {
                p = pnMenu.Location;
            }

            private void BaseForm_Scroll(object sender, ScrollEventArgs e)
            {
                pnMenu.Location = p;
            }
        }
    }

    2009年9月18日 8:16
  • 你好!
         重新生成以后,把ContriveForm那个设计器关闭,然后在重新打开就好了!
    周雪峰
    2009年9月18日 10:31
    版主
  • 前辈谢谢在此解决问题了
    2009年9月18日 10:48
  • 不用这么客气了啊!
    周雪峰
    2009年9月18日 12:01
    版主