5 Replies - 5688 Views - Last Post: 06 November 2008 - 06:29 PM Rate Topic: -----

#1 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 88
  • Joined: 06-October 08

Saving data to array from more than one control

Post icon  Posted 04 November 2008 - 05:03 PM

Hi, I haven't written the code yet as I'm still sorting out my own logic, but is it possible to populate an array (single? multi-dimensional?) from more than one control? In the past I've only every done this process using a 1D array and from only one control.

I have an interface that uses text boxes, rich text boxes and check boxes. My logic at this stage is for the user to be able to enter text (strings) and press 'Save'. This will then do two things:

1. Add data to the array
2. Save data in the array to a text file (which will also ultimately be used for printing too)

That aside, is it actually possible to save data (of the same data type) from different controls to one array or am I barking up the wrong tree entirely?

Any advice here would be greatly appreciated.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Saving data to array from more than one control

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8366
  • View blog
  • Posts: 31,089
  • Joined: 12-June 08

Re: Saving data to array from more than one control

Posted 05 November 2008 - 10:37 AM

Yes, yes it is possible. You just need to be very cognizant of where your data is going in the array. Say you have an array of size four, and you have four text boxes. Your job is to make sure the four text boxes consistantly write to their corrisponding spots on the array.
Was This Post Helpful? 0
  • +
  • -

#3 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 88
  • Joined: 06-October 08

Re: Saving data to array from more than one control

Posted 05 November 2008 - 12:35 PM

Thanks, modi123_1

Are you saying that I have an array whose size matches the number of controls on my interface, with each element in the array dedicated to data from one control only? In other words, I wouldn't have to use a multi-dimensional array at all?

I'll try it out later today anyway.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Saving data to array from more than one control

Posted 05 November 2008 - 01:07 PM

Why using a simple array. Why not use a Collection based on a class that defines your data. Then serialize the collection to a file. I need to stop this old VB6 thought process! I mean I just wrote this example in under 3 minutes, serialization is so powerful and easy to implement.

Example of using the Collection...
        'Collection
        Dim Data As New MyCollection

        'Add Items to Collection
        Data.Add(New MyItem With {.Name = "Test1", .Value = "1"})
        Data.Add(New MyItem With {.Name = "Test2", .Value = "2"})

        'Save Data
        Data.Save(Application.StartupPath & "\Test.xml")

        'Load Data
        Dim LoadData As MyCollection = MyCollection.Load(Application.StartupPath & "\Test.xml")



Collection Code with simple Save and Load routines
<Serializable()> _
Public Class MyCollection
    Inherits List(Of MyItem)

    Public Sub Save(ByVal File As String)
        Dim s As New Xml.Serialization.XmlSerializer(GetType(MyCollection))
        Using w = New IO.StreamWriter(File)
            s.Serialize(w, Me)
        End Using
    End Sub

    Public Shared Function Load(ByVal File As String) As MyCollection
        Dim LoadMyCollection As MyCollection
        Dim s As New Xml.Serialization.XmlSerializer(GetType(MyCollection))
        Using r = New IO.StreamReader(File)
            LoadMyCollection = DirectCast(s.Deserialize®, MyCollection)
        End Using
        Return LoadMyCollection
    End Function
End Class



Item Code, add what ever properties you need to store, screw multi-dimensional arrays...
<Serializable()> _
Public Class MyItem
    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Private _Value As String
    Public Property Value() As String
        Get
            Return _Value
        End Get
        Set(ByVal value As String)
            _Value = value
        End Set
    End Property
End Class



Example of XML file saved...
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMyItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyItem>
    <Name>Test1</Name>
    <Value>1</Value>
  </MyItem>
  <MyItem>
    <Name>Test2</Name>
    <Value>2</Value>
  </MyItem>
</ArrayOfMyItem>


Was This Post Helpful? 0
  • +
  • -

#5 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 88
  • Joined: 06-October 08

Re: Saving data to array from more than one control

Posted 06 November 2008 - 02:09 PM

Thanks magicmonkey,

The task I've been set specifies that I need to use an array (1D or multi-D) and text files, but no strict OOP (it's only a very beginner's class!)

I've taken a note of the code you wrote, but I think it's a little beyond me at the moment. I'm intrigued to learn more in time though! :-)

Currently, I've hit a problem when trying to create a new text file and save data from multiple text boxes into this file. I can only get this to work if I save the data to a text file that already exists, such as here:

   'CODE FOR SAVING USER INPUT TO A FILE THAT ALREADY EXISTS IN THE BIN FOLDER
        Dim arrLesson(15) As String
        Dim DataToSave As String
        Dim Filename As String = "TestData.txt"
        Dim i As Integer

        arrLesson(0) = txtLessonTitle.Text
        arrLesson(1) = rtxtLearningObjectives.Text
        arrLesson(3) = rtxtBlackboard.Text
        arrLesson(4) = rtxtIntroduction.Text
        arrLesson(5) = rtxtMain.Text
        arrLesson(6) = rtxtClosing.Text
        arrLesson(7) = rtxtResources.Text

        FileOpen(1, Filename, OpenMode.Output)
        For i = 0 To arrLesson.Length - 1
            DataToSave = arrLesson(i)
            PrintLine(1, DataToSave)
        Next i


What I really want is the user to input text into various text boxes, then save this data to a file that they create, name and specify the location of at run-time.

The code that's causing the problems is here:

        Dim dr As DialogResult
        dlgSave.ShowDialog()
        dlgSave.Filter = "Text File (*.txt)|*.txt|Word Document (*.doc)|*.doc"
        dlgSave.FilterIndex = 1
        If dr = DialogResult.OK Then
            Dim writerVar As StreamWriter
            writerVar = New StreamWriter(dlgSave.FileName, False)
            Dim arrLesson(6) As String
            arrLesson(0) = txtLessonTitle.Text
            arrLesson(1) = rtxtLearningObjectives.Text
            arrLesson(2) = rtxtBlackboard.Text
            arrLesson(3) = rtxtIntroduction.Text
            arrLesson(4) = rtxtMain.Text
            arrLesson(5) = rtxtClosing.Text
            arrLesson(6) = rtxtResources.Text
            Dim i As Integer
            For i = 0 To 6
                writerVar.WriteLine(arrLesson(i))
            Next i
            MessageBox.Show("Lesson Saved!")
        End If


Two problems exist at this stage:
1. Why does the SaveFileDialog box that appears NOT have the two file types as options? I've double-checked the Filter syntax online and in some books, but I don't get where I've gone wrong.

2. Nothing saves into the file, even when I select a text file that already exists - I get the messagebox asking if I want to overwrite the file and I say yes - but when I check the text file outside of VB, there's nothing in it.

BTW - I'm using Visual Basic Express 2008

Hopefully someone can shed some light on my confusion...

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Saving data to array from more than one control

Posted 06 November 2008 - 06:29 PM

1. You are adding the filters after you show the dialog, move them before the ShowDialog method.
2. Are you closing the file? Closing the file will flush the buffer out to disk. Look at using the USING statement when opening files, it is the best practice to ensure your files are closed and disposed of.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1