将二进制文件读入文本框

时间:2011-06-28 00:41:25

标签: c# textbox

我试图在二进制文件中读取2个值,并将第1个值放在文本框3中,将第2个值放在文本框4中。需要从文件中读取的2个值是使用文本框1和2创建的并使用第一个按钮保存到文件。在点击按钮2时,它应该读取文件并将值放在正确的位置。我不确定它是否读数不正确或是否写得不正确。当我点击按钮阅读并显示框时显示:     System.Collections.Generic.List`1 [test.Form1 +矿石]

代码如下:

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;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace test
{

public partial class Form1 : Form
{
    [Serializable]
    public class ore
    {
        public float Titan;
        public float Eperton;
    }
    ore b1 = null;
    ore b2 = null;



    public Form1()
    {
        InitializeComponent();

        b2 = new ore();
        b1 = new ore();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;


        if (float.TryParse(textBox1.Text, out tempFloat))
        {
            b1.Titan = tempFloat;
        }
        else
            MessageBox.Show("uh oh");



    }


    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        // 2nd text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {
            b2.Eperton = tempFloat;
        }
        else
            MessageBox.Show("uh oh");


    }


    private void button1_Click(object sender, EventArgs e)
    {
        // supposed to save list to file 
        List<ore> oreData = new List<ore>();
        oreData.Add(b1);
        oreData.Add(b2);

        FileStream fs = new FileStream("ore.dat", FileMode.Create);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(fs, oreData);
        fs.Close();
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {
        // 3rd text box 
    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {
        //4th text box
    }

    List<ore> books = new List<ore>(); // create list
    private void button2_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream("ore.dat", FileMode.Open);
        BinaryFormatter bf = new BinaryFormatter();
        List<ore> books = (List<ore>)bf.Deserialize(fs);
        fs.Close();

        if (books.Count > 1)
        {

            textBox3.Text = books.ToString();//update the 3rd text box
            textBox4.Text = books.ToString();//update the 4th text box

        }
    }
}
}

1 个答案:

答案 0 :(得分:1)

我想知道你是否真的问过你想要什么,因为你在评论中有效地回答了你自己的问题......

List<ore> books = new List<ore>(); // create list
private void button2_Click(object sender, EventArgs e)
{
    FileStream fs = new FileStream("ore.dat", FileMode.Open);
    BinaryFormatter bf = new BinaryFormatter();
    List<ore> books = (List<ore>)bf.Deserialize(fs);
    fs.Close();

    if (books != null)
    {
        if (books.Count > 0)
            textBox3.Text = books[0].SomeProperty.ToString();//update the 3rd text box
        if (books.Count > 1)
            textBox4.Text = books[1].SomeProperty.ToString();//update the 4th text box

    }
}

请注意使用 SomeProperty - 这是因为books中的每个项目都是对象ore的一个实例,因此您需要替换相应的属性(Titan)或{Eperton)来自ore代替我的 SomeProperty 占位符..如果您只是ToString() ore,那么您将获得该名称的ToString()键入字符串,而不是其中的任何单个值 - 除非您已覆盖{{1}}。