读取控件时出错';VB.NET中文本文件中的位置
我正在尝试将一些控件的位置保存并加载到文本文件中。 我得到以下错误: 表达式是一个值,因此不能作为赋值的目标 保存代码:读取控件时出错';VB.NET中文本文件中的位置,vb.net,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 Steya
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
加载代码:
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
,但我需要能够从一个简单的文本文件中准备好它。设法解决了我的问题。我将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
您似乎没有为
prop
指定任何值。此外,如果使用,它将指出一些类型不一致的问题,您应该加以解决。TextBox1.Location=newpoint(intX,intY)
…是的,启用选项strict,因为字符串不是数字。看起来您正在尝试将X和Y设置为相同的读取值(prop(1)
)prop=ladden.ReadLine().Split(“|”)
-这不是分配?顺便说一句,设置会将数据保存为正确的类型,即保存。位置为点以保存大量代码感谢反馈。我设法解决了我的问题。答案是附加的如果你要发布答案,他们应该在选项严格下编译intY=prop(2)
不会编译,有些人会把这样的答案记录下来