从VB.NET中的文本文件中读取控件的位置时出错

时间:2015-11-29 20:18:43

标签: vb.net

我正在尝试在文本文件中保存并加载某些控件的位置。 我收到以下错误:

表达式是一个值,因此不能作为赋值的目标。

保存代码:

Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
        Dim Opslaan As StreamWriter

        Opslaan = New StreamWriter("Y:\Jordy Steyaert\Scania Tool Data\40-zone\locaties.txt", False)

        Opslaan.WriteLine(TextBox1.Name & "|" & TextBox1.Location.X)
        Opslaan.WriteLine(TextBox1.Name & "|" & TextBox1.Location.Y)

        Opslaan.Flush()
        Opslaan.Close()
    End Sub

正在加载代码:

    Private Sub Laden()
        Dim Laden As StreamReader
        Dim prop(5) As String

        If File.Exists("Y:\Jordy Steyaert\Scania Tool Data\40-zone\locaties.txt") Then
            Laden = New StreamReader("Y:\Jordy Steyaert\Scania Tool Data\40-zone\locaties.txt", False)
            Do Until Laden.EndOfStream
                prop = Laden.ReadLine().Split("|")

                If String.Equals(prop(0), TextBox1.Name) Then
                    TextBox1.Location.X = prop(1) 'Expression is a value and therefore cannot be the target of an assignment.
                End If

                If String.Equals(prop(0), TextBox1.Name) Then
                    TextBox1.Location.Y = prop(1) 'Expression is a value and therefore cannot be the target of an assignment.
                End If

            Loop
            Laden.Close()
        End If
    End Sub
End Class

是否有更简单的方法从文件中保存和读取位置?我知道可以使用My.Settings,但我需要能够从简单的文本文件中准备它。

1 个答案:

答案 0 :(得分:0)

管理解决我的问题。我为属性分配了X和Y值,并将它们保存并从同一行读取。我现在可以拖动我的控件并将位置保存到文本文件,因此当多台计算机一次使用该程序时,他们将能够看到更改。

Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
    Dim Opslaan As StreamWriter

    Opslaan = New StreamWriter("Y:\Jordy Steyaert\Scania Tool Data\40-zone\locaties.txt", False)

    Opslaan.WriteLine(TextBox1.Name & "|" & TextBox1.Location.X & "|" & TextBox1.Location.Y)

    Opslaan.Flush()
    Opslaan.Close()
End Sub

Private Sub Laden()
    Dim Laden As StreamReader
    Dim prop(2) As String
    Dim intX As Integer
    Dim intY As Integer

    If File.Exists("Y:\Jordy Steyaert\Scania Tool Data\40-zone\locaties.txt") Then
        Laden = New StreamReader("Y:\Jordy Steyaert\Scania Tool Data\40-zone\locaties.txt", False)
        Do Until Laden.EndOfStream
            prop = Laden.ReadLine().Split("|")

            If String.Equals(prop(0), TextBox1.Name) Then
                intX = prop(1)
                intY = prop(2)
            End If

        Loop
        Laden.Close()
    End If

    TextBox1.Location = New Point(intX, intY)
End Sub