开发者

How to Save/Load ListBox, That Has string and decimal values in it

So I have a Windows Application I am working on in Visual Studio, coded in C#, and The user enters the name of a video in a text box and then they rate things, like sound, video quality, with a NumbericUpDown. They then plus a calculate button which fills in the rating label and adds it to a listbox. Below is all the code for the class Videos, which gets all the data, and is then put into the list box. and the rating is calculated by adding all the values up, don't think I need to show the code for that.

public class Videos
{
    // fields hold the data
    private string mName;
    private decimal mVideoScore;
    private decimal mSoundScore;
    private decimal mStoryScore;
    private string mRating;

    // Constructor
    public Videos String(string name, decimal videoScore, decimal soundScore, decimal storyScore, string rating)
    {
        mName = name;
        mVideoScore = videoScore;
        mSoundScore = soundScore;
        mStoryScore = storyScore;
        mRating = rating;
    }

    // Properties control access to the data
    public string Name
    {
        get { return mName; }
        set { mName = value;}
    }
    public decimal VideoScore
    {
        get { return mVideoScore; }
        set { mVideoScore = value;}
    }
    public decimal SoundScore
    {
        get { return mSoundScore; }
        set { mSoundScore = value; }
    }
    public decimal StoryScore
    {
        get { return mStoryScore; }
        set { mStoryScore = value; }
    }
    public string Rating
    {
开发者_StackOverflow中文版        get { return mRating; }
        set { mRating = value; }
    }

    // Methods perform operations on the data
    public override string ToString()
    {
        return mName;
    }
}

So, I put into the listbox with the following code (when the user clicks the add button)

private void AddToListButton_Click(object sender, EventArgs e)
{
    // Create a new Video Class
    Videos aVideo = new Video(NameTextBox.Text, VideoRating.Value, SoundRating.Value, StoryRating.Value, RatingDataLabel.Text);
    VideoList.Items.Add(aVideo);                
}

So when they hit add, it shows up as the name of the video, but the other data is still retreivable, I have a button to retrieve and display the video they select.

So I have tried xml and saving to txt, but it just adds the video name, not the other data.

So please help me get all the data saved into an xml or txt or whatever is the appropriate file type, and then to load/open it for when the user opens the application again they can load the previously entered data.


Because you are override ToString() to return the return mName.

When add item to the list box it uses that method to get the text represented to the user.

if you wish to serialize data to xml then:

1- Add [Serializable()] attribute to the Video class.

[Serializable()] 
public class Video { ...}

2- Implement a method to serialize and deserialize data to and from xml:

public static void SerializeToXml(Video video, string outputXmlFilePath)
{
    if (video== null)
    {
        throw new ArgumentNullException("video");
    }

    if (outputXmlFilePath == null)
    {
        throw new ArgumentNullException("outputXmlFilePath");
    }

    System.Xml.Serialization.XmlSerializer xmlSerializer =
                new System.Xml.Serialization.XmlSerializer(video.GetType());

    using (StreamWriter streamWriter = new StreamWriter(outputXmlFilePath, false, System.Text.Encoding.UTF8))
    {
        xmlSerializer.Serialize(streamWriter, video);
    }
}

public static Video DeserializeFromXml(string xmlFilePath)
{
    if (xmlFilePath == null)
    {
        throw new ArgumentNullException("xmlFilePath");
    }

    if (!File.Exists(xmlFilePath))
    {
        throw new FileNotFoundException("file to deserialize from xml is not exists", xmlFilePath);
    }

    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Video));

    Video video = null;

    using (StreamReader streamReader = new StreamReader(xmlFilePath, Encoding.UTF8))
    {
        deserializedObject = serializer.Deserialize(streamReader);
    }

    return video;
}

To serialize and deserialize data from file: Add these two methods SerializeToXml and DeserializeFromXml to your Video calss. 2- Whenever you want to save video to file use Video.SerializeToXml(yourVideoObject, yourOutputFilePath); and to get the video back from file use Video myVideo = Video.DeserializeFromXmlFile(yourVideoFilePath);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜