在visual basic中保留文本框文本

时间:2011-09-07 22:09:46

标签: vb.net

我是VB的新手,我正在编写一个包含多个表单的程序,每个表单都填充了文本框。该程序的目的是在框之间拖动文本以移动资产。我已经管理了拖放功能,但是一旦程序关闭就需要在文本框中保留文本,以便在重新打开时,所有移动文本的最后位置仍然存在。

有人可以提出任何建议/提供示例代码吗?


我尝试了最容易理解的建议让我开始,但是当我构建并发布程序时,它说我无法访问该文件以保存值!有人可以帮忙吗?代码

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

6 个答案:

答案 0 :(得分:1)

您可以使用内置的PropertyBinding将TextBox.Text链接到Property。它会将其放入您的App.Config文件中,只要是每个用户,您就可以通过MySettings对其进行编辑。如果设置是应用程序级别,则最好使用其他答案之一。您还可以查看此article以获取更多信息。

答案 1 :(得分:0)

您应该在程序退出时将文本框的位置写入持久性存储,例如文件,数据库或注册表。在程序加载时,您可以检索保存的值并相应地设置位置。

答案 2 :(得分:0)

您可以将每个文本框的文本保存在文件中,并在运行时重新打开它。

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

假设你有3个名为textBox1,textBox2和textBox3的文本框。只需在文件中写入每个文本框的文本属性,即可保存每个人的状态。像这样:

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

最后,您只需关闭文件。

yourfile.Close()

重新加载数据非常简单。

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

如果您有任何疑问或需要进一步的帮助,请与我们联系。请务必导入System.IO命名空间,以访问此处使用的IO类。

答案 3 :(得分:0)

持久化数据的最常用方法是将其存储在数据库中。当然,这会为您的项目增加更多工作,因为您现在必须创建,更新和维护数据库。更简单的解决方案是使用文件。

我们将创建一个新类以便阅读&从文件中写入数据。这样,如果稍后切换到数据库,则只需更改类。因为我确信在某些时候你会想要一个数据库,我们会让这个类使用数据表来最小化它所需的变化。这是我们的课程:

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


接下来你要用这个优秀的课程来阅读和阅读写你的数据。这样做的代码很简单:

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)

答案 4 :(得分:0)

我会使用Mark Hall指出的应用程序设置或者像这样做...

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

那么你可以做的就是......

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

2保存

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

答案 5 :(得分:0)

要维护您可以使用存储的用户设置的值,请​​参阅以下链接。

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

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

问候。