2011-09-07 39 views
0

我對VB很新穎,並且正在編譯一個包含多個窗體的程序,每個窗體都填充了文本框。該計劃的目的是在文本框之間拖動資源。我管理了拖放功能,但需要在程序關閉後將文本保留在文本框中,以便在重新打開時,所有移動的文本的最後位置仍然存在。在Visual Basic中持久化文本框文本

任何人都可以提出任何建議/請提供示例代碼嗎?


我試過最容易理解的建議,讓我開始,但是當我構建和發佈程序它說,我沒有訪問文件保存的值!誰能幫忙?下面的代碼

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 

回答

0

您應該將文本框的位置寫入程序出口的持久性存儲區,如文件,數據庫或註冊表。在程序加載時,您可以檢索保存的值並相應地設置位置。

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類。

+0

我用這個解決方案看到的一個問題是,如果文本框是多行並且用戶使用換行符,它們將不會被正確地讀回 – briddums

+0

事實上,沒有考慮到這一點。上述答案在這種情況下會更好。 – 2011-09-08 12:08:35

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) 
1

您可以使用內置的PropertyBinding您TextBox.Text鏈接到一個屬性。它會將它放到您的App.Config文件中,只要它是每個用戶,您就可以通過MySettings進行編輯。如果設置是應用程序級別,則最好使用其他答案之一。你也可以看看這個article瞭解更多信息。

0

我會使用任何應用程序設置做得一樣馬克·霍爾指出,或像這樣...

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