Address Book

decrypt

Well-known member
Joined
Oct 6, 2003
Messages
216
Im trying to make an address book. Ive tried alot of things and still no luck. Anybody got any ideas?
 
well, I think hes asking for any ideas on creating an address book :)
How about using XML for the address book?? something like

<addressbook>
<contact>
<name>A NAME HERE</name>
<address>AN ADDRESS HERE</address>
</contact>
</addressbook>

What you think about it????
 
well i was wondering if you can make a sort of thing like in windows explorer when you do view-detials. Can a regular listbox be split up into 5 different sections?

E.g.
Listbox:
__________________________________________________
|collum1 | collum 2 | collum 3 | collum 4 | collum 5|
| Data 1 | data 2 | data 3 | data 4 | data 5 |__________________________________________________
 
Last edited by a moderator:
Try looking into a ListView or a DataGrid control. Theyll probably better suit your needs. You can also easily serialize the items in these controls for saving to disk.
 
You may want to look at the Xml.Serialization.XmlSerializer class. There are a couple of pretty good samples on how to use it in MSDN.
 
Ok I looked that up and I got this, but I do not know what to change in it. :confused:

Code:
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim myObject As MySerializableClass = New MySerializableClass
         Insert code to set properties and fields of the object.
        Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(MySerializableClass))
         To write to a file, create a StreamWriter object.
        Dim myWriter As StreamWriter = New StreamWriter("contacts.xml")
        mySerializer.Serialize(myWriter, myObject)

    End Sub
I get the squigily line under the following: MySerializableClass, XmlSerializer, and StreamWriter. It says that they are not defined.
 
MySerializableClass needs to be the class that has a collection of
all your contacts. As for the other squiggly lines, you need to
import the namespaces System.IO and System.Xml.Serialization
 
Thanks alot, but I dont know which MySerializableClass needs to be changed the first and the second. And I still dont understand which one.
Code:
Dim myObject As MySerializableClass = New MySerializableClass

would it be the datagrid or the xml file?
Edit: Well?
 
Last edited by a moderator:
Its a class that is a collection of your address book contacts. So,
for example, you first create a class that stores all the address
information, like so:

Code:
Public Class Contact
   Private members of the class
  Private m_FirstName As String
  Private m_LastName As String
  
   Public properties that are saved in the XML file
  Public Property FirstName() As String
    Get
      Return m_FirstName
    End Get
    Set(Value As String)
      m_FirstName = Value
    End Set
  End Property

  Public Property LastName() As String
    Get
      Return m_LastName
    End Get
    Set(Value As String)
      m_LastName = Value
    End Set
  End Property
End Class

Then make a collection, add some contacts, and serialize it:
Code:
Dim contacts As New ArrayList()

Dim person As New Contact()
person.FirstName = "Bucky"
person.LastName = "Fuller"

contacts.Add(person)

Now the contacts variable is the substitution for
MySerializableClass. You dont need to initialize it again, because
its already done in the above code. Just serialize it.
 
Ok ill start from the begining. First I made an xml file that included this:

<addressbook>
<contact>
<name>A NAME HERE</name>
<address>AN ADDRESS HERE</address>
</contact>
</addressbook>

Then I did this so that a DataGrid would display it.
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim dsUsers As New DataSet
        dsUsers.ReadXml("..\contacts.xml")
        With Contactsgrid
            .CaptionText = "Your Contacts"
            .DataSource = dsUsers.Tables(0)
        End With
    End Sub
now im trying to serialize it using this code:
Code:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim myObject As MySerializableClass = New MySerializableClass
         Insert code to set properties and fields of the object.
        Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(MySerializableClass))
         To write to a file, create a StreamWriter object.
        Dim myWriter As StreamWriter = New StreamWriter("contacts.xml")
        mySerializer.Serialize(myWriter, myObject)

    End Sub
Edit: Well?
 
Last edited by a moderator:
Oh, heh. You dont need to serialize it this way. The DataSet can
serialize the data itself by using the WriteXml method. Thats all
you need to do, just call dsUsers.WriteXml() with a file path.
 
what if ..

Hello decrypt,

What if the values are coming from multiple forms? I mean if user fills in fields on multiple forms, can we use only one class to serialize on all forms giving some of the entries only?

Pl. Reply

Regards.
 
the dsUsers.WriteXml("path") should serialize all of the fields in the xml file. The xml file should be created by yourself.

Be sure to not declare dsUsers in a sub, so that you can save it and load it in different subs.
This is what i did:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        dsUsers.ReadXml("contacts.xml")
        With Contactsgrid
            .CaptionText = "Your Contacts"
            .DataSource = dsUsers.Tables(0)
        End With
    End Sub
For Loading and....

Code:
 Private Sub Button1_Click (whatever goes in here which i cant remember)
DsUsers.WriteXml("contacts.xml")
End Sub
To save and...
Code:
 Dim dsUsers As New DataSet
Does this answer your question?
 
Back
Top