Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 11th, 2005, 02:36 AM
Authorized User
 
Join Date: Apr 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating Files

Hi, I'm really new to vb.net and I've started a basic project that balances my checking account. The problem I'm having is:
I have a listview that contains all the activity in the account. A bunch of text boxes populate the list. I want to save the contents and state of the listview to file and be able to load it and repopulate the listview and still be able to do the neccessarry math. Also I'd like to know how to create my own file format. I've been struggling with this for a while and I'd greatly appreciate any help. thanks.
 
Old April 11th, 2005, 10:20 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

For the 1st part, I believe you will have to iterate through the properties of the listview, and create your file from what you find during that process.

The format of a file is established through “business rules” you create in your program. It will have the format of the order in which you wrote to it, and what it is that you wrote. It is up to you/your-program to keep track of how to interpret the contents of the file when reading it.
 
Old April 11th, 2005, 10:56 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

you can make a class including a collection of serilizable info,

then when you want to populate your listview deserialize your file and add that collection to the listview through listview.Items.AddRange(ListViewItem[]) and vise-versa instantiate the class with your listview and serialize your class.

_____________
Mehdi.
software student.
 
Old April 13th, 2005, 10:58 PM
Authorized User
 
Join Date: Apr 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok i had it working but it only saved one line in the list view. So ive made a serializable class where i save a ListView.ListViewItemCollection but when i try to repopulate the listview when i load the file it gives me an error saying:
"Value of type System.Windows.Forms.ListViewItem cannot be converted to '1-demensional array of System.Windows.Forms.ListViewItem'."

This doesnt make sense to me as i am saving it as a listview item collection my code looks like this:

 Public Sub PopulateListFromTransaction(ByVal transaction As Transaction)

        Dim index As Integer
        For index = 0 To transaction.Collection.Count - 1
            ListView1.Items.AddRange(transaction.Collection(in dex))


        Next


    End Sub

And the Serializable Class Looks like this:

Public Class Transaction
    Inherits SerializableCollection

    'members
    Public Amount As Decimal
    Public Description As String
    Public TheDate As Date
    Public Total As Decimal
    Public Collection As ListView.ListViewItemCollection

    Public Function AddItem(ByVal List As ListViewItem)
        List.Text = TheDate
        List.SubItems.Add(Description)
        List.SubItems.Add(Amount)

        AddToCollection(List)
    End Function

    Public Function AddToCollection(ByVal List As ListViewItem)
        Collection.Add(List)
    End Function
End Class

If anyone can help id appreciate it, thanks.

 
Old April 14th, 2005, 02:02 AM
Authorized User
 
Join Date: Apr 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My code has changed again. I feel im closer to getting this to work now, but im still getting an exception. this time its and invalid operation exception. there was an error reflecting the type of the class. i dont understand. i dont know much about enumeration but i think this has something to do with it. the code with the error is:

 Public Function Save(ByVal stream As Stream)

        Dim serializer As New XmlSerializer(Me.GetType)

        serializer.Serialize(stream, Me)
    End Function

 
Old April 14th, 2005, 08:14 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

such exception is thrown when your class doesn't have a logic schema for being serialized..
I tried some codes,like you I received that exception when I wanted to serialize a ListViewItem object besides this fact it implements ISerializable,I don't know why?????

anyway there are other solutions!
Code:
    [C#]
    public class Item
    {
        [XmlAttribute("amount")]public string amount;
        [XmlAttribute("description")]public string Description;
        [XmlAttribute("TheDate")]public DateTime TheDate;
        [XmlAttribute("Total")]public decimal Total;
        public Item(string amount,string description,DateTime theDate,decimal total)
        {
            this.amount=amount;
            this.Description=description;
            this.TheDate=theDate;
            this.Total=total;
        }
        public Item(){}
        //other business functions
    }
    [XmlRoot("root")]
    public class ListItems
    {
        [XmlElement("item")]public Item[] Items;
        public ListItems(Item[] items)
        {
            Items=items;
        }
        public ListItems(){}
        //returns an array of ListViewItem 
        public ListViewItem[] PopulateListView()
        {
            IList list=new ArrayList();
            foreach(Item item in Items)
            {
                string[] s={item.amount,item.Description,item.TheDate.ToString(),item.Total.ToString()};
                ListViewItem listViewItem=new ListViewItem(s);
                list.Add(listViewItem);
            }
            ListViewItem[] result=new ListViewItem[list.Count];
            list.CopyTo(result,0);
            return result;
        }
        //write other methods...
    }

then to use
Code:
        [C#]
        private void serlist()
        {
            Item item1=new Item("1","ar",DateTime.Now,12);
            Item item2=new Item("2","az",DateTime.Now,13);
            Item item3=new Item("3","ak",DateTime.Now,14);
            ListItems items=new ListItems(new Item[]{item1,item2,item3});
            XmlSerializer xs=new XmlSerializer(typeof(ListItems));
            StreamWriter write=new StreamWriter(@"C:\testlist.xml");
            xs.Serialize(write,items);
            write.Close();
            listView1.Items.AddRange(items.PopulateListView());
        }
        private void desrila()
        {
            XmlSerializer xs=new XmlSerializer(typeof(ListItems));
            Stream stream=new FileStream(@"C:\testlist.xml",FileMode.OpenOrCreate);
            ListItems items=(ListItems)xs.Deserialize(stream);
            listView1.Items.AddRange(items.PopulateListView());    
        }

should be self explanatory,
also you can work with DataSet.

_____________
Mehdi.
software student.
 
Old April 14th, 2005, 08:24 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

its XML file,
Code:
  <?xml version="1.0" encoding="utf-8" ?> 
- <root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <item amount="1" description="ar" TheDate="2005-03-21T15:40:21.5423904+04:30" Total="12" /> 
  <item amount="2" description="az" TheDate="2005-03-21T15:40:21.5423904+04:30" Total="13" /> 
  <item amount="3" description="ak" TheDate="2005-03-21T15:40:21.5423904+04:30" Total="14" /> 
  </root>


_____________
Mehdi.
software student.





Similar Threads
Thread Thread Starter Forum Replies Last Post
creating read-only files Fat_and_immoral Excel VBA 4 December 20th, 2006 09:24 AM
Creating PDF Files aldwinenriquez ASP.NET 1.0 and 1.1 Professional 3 January 12th, 2006 06:43 AM
Creating setup files Ali A Al Amudi Beginning VB 6 5 March 6th, 2005 01:40 AM
Creating Field Definition Files ejan Pro VB 6 0 December 31st, 2004 08:15 AM
Creating Field Definition Files ejan Crystal Reports 0 December 31st, 2004 08:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.