1
2小白求教大神该如何去解决这个问题

解决方案 »

  1.   

    贴完整代码。你的程序中有2个SelectedShape的定义。
      

  2.   

    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;
    // For the binary formatter.
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
    namespace shiyan04
    {
        public partial class MyPaintProgram : Form
        {
            public MyPaintProgram()
            {
                InitializeComponent();
            }        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }        // 需要呈现的当前形状/颜色。
            private SelectedShape currentShape;
            private Color currentColor = Color.DarkBlue;        // 维护每个ShapeData
            private List<ShapeData> shapes = new List<ShapeData>();        private void MainWindow_Paint(object sender, PaintEventArgs e)
            {
                // Get the Graphics type for this window.
                Graphics g = e.Graphics;            // Render each shape in the selected color. 
                foreach (ShapeData s in shapes)
                {
                    // Render a rectangle or circle 20 x 20 pixels in size
                    // using the correct color. 
                    if (s.ShapeType == SelectedShape.Rectangle)
                        g.FillRectangle(new SolidBrush(s.Color), s.UpperLeftPoint.X, s.UpperLeftPoint.Y, 20, 20);
                    else
                        g.FillEllipse(new SolidBrush(s.Color), s.UpperLeftPoint.X, s.UpperLeftPoint.Y, 20, 20);
                }        }        private void MainWindow_MouseClick(object sender, MouseEventArgs e)
            {
                // Make a ShapeData type based on current user
                // selections.
                ShapeData sd = new ShapeData();
                sd.ShapeType = currentShape;
                sd.Color = currentColor;
                sd.UpperLeftPoint = new Point(e.X, e.Y);            // Add to the List<T> and forse the form to repaint itself. 
                shapes.Add(sd);
                Invalidate();        }        private void pickShapeToolStripMenuItem_Click(object sender, EventArgs e)
            {
                ShapePickerDialog dlg = new ShapePickerDialog();
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    currentShape = dlg.SelectedShape;
                }        }        private void pickColorToolStripMenuItem_Click(object sender, EventArgs e)
            {
                ColorDialog dlg = new ColorDialog();            if (dlg.ShowDialog() == DialogResult.OK)
                {
                    currentColor = dlg.Color;
                }        }        private void clearSurfaceToolStripMenuItem_Click(object sender, EventArgs e)
            {
                shapes.Clear();            // This will fire the paint event.
                Invalidate();        }        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
            {
                using (SaveFileDialog saveDlg = new SaveFileDialog())
                {
                    // 配置Save对话框的外观
                    saveDlg.InitialDirectory = ".";
                    saveDlg.Filter = "Shape files (*.shapes)|*.shapes";
                    saveDlg.RestoreDirectory = true;
                    saveDlg.FileName = "MyShapes";                // 如果点击了OK按钮,就会打开新的文件并且序列化List<T>        
                    if (saveDlg.ShowDialog() == DialogResult.OK)
                    {
                        Stream myStream = saveDlg.OpenFile();
                        if ((myStream != null))
                        {
                            // 保存所有的形状!
                            BinaryFormatter myBinaryFormat = new BinaryFormatter();
                            myBinaryFormat.Serialize(myStream, shapes);
                            myStream.Close();
                        }
                    }
                }        }        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
            {
                using (OpenFileDialog openDlg = new OpenFileDialog())
                {
                    openDlg.InitialDirectory = ".";
                    openDlg.Filter = "Shape files (*.shapes)|*.shapes";
                    openDlg.RestoreDirectory = true;
                    openDlg.FileName = "MyShapes";                if (openDlg.ShowDialog() == DialogResult.OK)
                    {
                        Stream myStream = openDlg.OpenFile();
                        if ((myStream != null))
                        {
                            // 获取所有形状!
                            BinaryFormatter myBinaryFormat = new BinaryFormatter();
                            shapes = (List<ShapeData>)myBinaryFormat.Deserialize(myStream);
                            myStream.Close();
                            Invalidate();
                        }
                    }
                }        }        
        }
    }
      

  3.   

    请将各类之间的关系贴出来,否则,无法判断是否能转换。
    如currentshape 
    s.shapetype
      

  4.   

    ShapeData类的定义
    SelectShape类的定义
      

  5.   

    private SelectedShape currentShape;=>private shiyan04.SelectedShape currentShape;