开发者

persisting textbox text in visual basic

I am pretty new to VB and am compiling a program which contains several forms, each of which is populated with text boxes. The purpose of the program is for text to be dragged between boxes to move as开发者_如何学Pythonsets around. I've managed the drag and drop functionality but need to persist the text in the text boxes once the program is shut down so that when reopened, the last location of all moved text is still present.

Can anyone make any suggestions/supply sample code please?


I've tried the easiest to understand suggestion to get me started but when I build and publish the program it says that I do not have access to the file to save the values!! Can anyone help? Code below

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim txtpersist As IO.TextWriter = New IO.StreamWriter("C:\Users\HP\Documents\namestore")
        txtpersist.WriteLine(TextBox1.Text)
        txtpersist.WriteLine(TextBox2.Text)
        txtpersist.WriteLine(TextBox3.Text)
        txtpersist.Close()
        Dim yourfile As IO.TextReader = New IO.StreamReader("C:\Users\HP\Documents\namestore")
        TextBox1.Text = yourfile.ReadLine()
        TextBox2.Text = yourfile.ReadLine()
        TextBox3.Text = yourfile.ReadLine()
        yourfile.Close()
    End Sub

End Class


You can use the built in PropertyBinding to link your TextBox.Text to a Property. It will put it into your App.Config File which will allow you to edit it through MySettings as long as it is per user. If the settings are application level you would be better of using one of the other answers. You can also look at this article for some more information.


You should write the location of the textboxes to a persistent store on program exit, such as a file, a database, or the registry. On program load, you can retrieve the saved values and set the locations accordingly.


You could save each textbox's text inside of a file and re-open it at runtime.

Dim yourfile As TextWriter = New StreamWriter("pathtofile")

Say you had 3 textBoxes called textBox1, textBox2 and textBox3. You would save each one's status by simply writing each textbox's text property inside the file. Like so:

yourfile.WriteLine(textBox1.Text)
yourfile.WriteLine(textBox2.Text)
yourfile.WriteLine(textBox3.Text)

At the end, you just close the file.

yourfile.Close()

Loading the data back is just as simple.

Dim yourfile As TextReader = New StreamReader("pathtofile")
textBox1.Text = yourfile.ReadLine()
textBox2.Text = yourfile.ReadLine()
textBox3.Text = yourfile.ReadLine()
yourfile.Close()

Let me know if you have any questions or require further assistance. Be sure to import the System.IO namespace, to get access to the IO classes used here.


The most common method of persisting data is to store it in a database. Of course that adds more work to your project since you now have to create, update, and maintain a database. An easier solution is to use a file.

We'll create a new class in order to read & write data from the file. This way if you switch to a database later on, you only need to change the class. And since I'm sure at some point you'll want a database we'll make the class use datatables to minimize the changes needed to it. Here's our class:

Public Class TextBoxes
    Private tbl As DataTable
    Private filename As String

    'Use constants for our column names to reduce errors
    Private Const ctrlName As String = "CtrlName"
    Private Const text As String = "Text"

    Public Sub New(ByVal file As String)
        'Create the definition of our table
        tbl = New DataTable("TextBox")
        tbl.Columns.Add(ctrlName, Type.GetType("System.String"))
        tbl.Columns.Add(text, Type.GetType("System.String"))

        'Save the filename to store the data in 
        Me.filename = file
    End Sub


    Public Sub Save(ByVal frm As Form)
        Dim row As DataRow

        'Loop through the controls on the form
        For Each ctrl As Control In frm.Controls

            'If the control is a textbox, store its name & text in the datatable
            If TypeOf (ctrl) Is TextBox Then
                row = tbl.NewRow

                row.Item(ctrlName) = ctrl.Name
                row.Item(text) = ctrl.Text

                tbl.Rows.Add(row)
            End If
        Next

        'Save the additions to the dataset and write it out as an XML file
        tbl.AcceptChanges()
        tbl.WriteXml(filename)
    End Sub


    Public Sub Load(ByVal frm As Form)
        'Don't load data if we can't find the file
        If Not IO.File.Exists(filename) Then Return

        tbl.ReadXml(filename)

        For Each row As DataRow In tbl.Rows
            'If this control is on the form, set its text property
            If frm.Controls.ContainsKey(row.Item(ctrlName)) Then
                CType(frm.Controls(row.Item(ctrlName)), TextBox).Text = row.Item(text).ToString
            End If
        Next
    End Sub
End Class


Next you'll want to use this fine class to read & write your data. The code for doing this is nice and simple:

Dim clsTextBoxes As New TextBoxes("C:\Txt.xml")

'Save the textboxes on this form
clsTextBoxes.Save(Me)

'Load the textboxes on this form
clsTextBoxes.Load(Me)


I would do it using either the Application settings as Mark Hall pointed out or like this...

Public Class MyTextBoxValueHolder

    Public Property Value1 As String
    Public Property Value2 As String
    Public Property Value3 As String

    Public Sub Save(Path As String)
        Dim serializer As New XmlSerializer(GetType(MyTextBoxValueHolder))
        Using streamWriter As New StreamWriter(Path)
            serializer.Serialize(streamWriter, Me)
        End Using
    End Sub

    Public Shared Function Load(Path As String) As MyTextBoxValueHolder
        Dim serializer As New XmlSerializer(GetType(MyTextBoxValueHolder))
        Using streamReader As New StreamReader(Path)
            Return DirectCast(serializer.Deserialize(streamReader), MyTextBoxValueHolder)
        End Using
    End Function

End Class

So what you can then do is...

Dim myValues As MyTextBoxValueHolder = MyTextBoxValueHolder.Load("SomeFilePath.xml")
myTextBox1.Text = myValues.Value1
myTextBox2.Text = myValues.Value2
'And so on....

2 Save

Dim myValues As New MyTextBoxValueHolder
myValues.Value1 = myTextBox1.Text
myValues.Value2 = myTextBox2.Text
myValues.Save("SomeFilePath.xml")
'All saved


to maintain the values ​​you can use stored user settings, see the following links.

http://msdn.microsoft.com/en-us/library/ms379611(v=vs.80).aspx

http://www.codeproject.com/KB/vb/appsettings2005.aspx

Regards.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜