代码都在,同一个类(主窗体)里是保存和读取成功的,但把 SaveInfo LoadInfo 方法放到另一个类中的时候运行读取不到数据,不知道是没有保存到还是读取的问题。但在一个窗体中又正常我晕了。到底哪里错了。        private void 大话游戏币5173_FormClosing(object sender, FormClosingEventArgs e)
        {
            InfoManage InfoManager = new InfoManage(double.Parse(txtLookPrice1.Text), double.Parse(txtLookPrice2.Text), double.Parse(txtLookPrice3.Text), double.Parse(txtLookPrice4.Text));
            infoManage.Save();
            //SaveInfo();
        }        public aaaaaa()
        {
            InitializeComponent();
            if (File.Exists("User.Game"))
            {
                infoManage.Load();
                //LoadInfo();
                txtLookPrice1.Text = saveInfo.LookPrice1.ToString();
                txtLookPrice2.Text = saveInfo.LookPrice2.ToString();
                txtLookPrice3.Text = saveInfo.LookPrice3.ToString();
                txtLookPrice4.Text = saveInfo.LookPrice4.ToString();
            }
        }       ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////        public void SaveInfo()
        {
            SaveInfo SaveInfo = new SaveInfo(double.Parse(txtLookPrice1.Text), double.Parse(txtLookPrice2.Text), double.Parse(txtLookPrice3.Text), double.Parse(txtLookPrice4.Text));
            try
            {
                //定义一个文件流
                FileStream fs = new FileStream("User.Game", FileMode.Create);
                //实例化二进制保存方式
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, saveInfo);
                //关闭流文件
                fs.Close();
            }
            catch (Exception Ex) { MessageBox.Show(Ex.ToString()); }
        }
        public void LoadInfo()
        {
            try
            {
                //定义一个文件流
                FileStream fs = new FileStream("User.Game", FileMode.Open);
                //实例化二进制保存方式
                BinaryFormatter bf = new BinaryFormatter();
                saveInfo = (SaveInfo)bf.Deserialize(fs);
                fs.Close();
            }
            catch (Exception Ex) { MessageBox.Show(Ex.ToString()); }
        }在另一个类中的代码是:
    class InfoManage
    {
       public double txtLookPrice1;
       public double txtLookPrice2;
       public double txtLookPrice3;
       public double txtLookPrice4;
       public InfoManage() { }
       public InfoManage(double lookPrice1, double lookPrice2, double lookPrice3, double lookPrice4)
        {
            this.txtLookPrice1 = lookPrice1;
            this.txtLookPrice2 = lookPrice2;
            this.txtLookPrice3 = lookPrice3;
            this.txtLookPrice4 = lookPrice4;
        }       SaveInfo saveInfo;
        public void Save()
        {
            saveInfo = new SaveInfo(txtLookPrice1, txtLookPrice2, txtLookPrice3, txtLookPrice4);
            try
            {
                //定义一个文件流
                FileStream fs = new FileStream("User.Game", FileMode.Create);
                //实例化二进制保存方式
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, saveInfo);
                //关闭流文件
                fs.Close();
            }
            catch (Exception Ex) { MessageBox.Show(Ex.ToString()); }
        }
        public void Load()
        {
            try
            {
                //定义一个文件流
                FileStream fs = new FileStream("User.Game", FileMode.Open);
                //实例化二进制保存方式
                BinaryFormatter bf = new BinaryFormatter();
                saveInfo = (SaveInfo)bf.Deserialize(fs);
                fs.Close();
            }
            catch (Exception Ex) { MessageBox.Show(Ex.ToString()); }
        }
    }

解决方案 »

  1.   

    因为窗口中的控件都是私有成员,当你把那两个方法放在这个窗口的代码中时,是可以在代码中调用这样控件对象的,而放到窗口外单独类时,无法读取那些类。初学C#.这是我的理解。
      

  2.   

    窗口中的控件我已经设置为公有的了,我再一步步调试的时候也看到类里的值和控件的值是一样的。但就不成功。不知道是保存的时候出问题还是读取的时候出问题。真郁闷
      

  3.   

    这个没有人有办法搞定了吗?