Как сохранить координаты объектов на форме?
Подскажите пожалуйста, как это можно сделать?
Оттуда их можно "брать" и менять им координаты и размеры.
А где хранить координаты - это вам решать. И когда менять - тоже.
Но я бы менял после вызова функции InitializeComponent().
Код:
Public Class Form1
Dim B As New List(Of Btn)
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
B.Clear()
For Each C As Control In Me.Controls
If TypeOf C Is Button Then
Dim Tmp As New Btn With {.Name = C.Name, .Text = C.Text, .Location = C.Location, .Forecolor = C.ForeColor}
B.Add(Tmp)
End If
Next
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Using stream As IO.Stream = IO.File.Create("Button.bin")
formatter.Serialize(stream, B)
stream.Close()
End Using
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each C As Control In Me.Controls
If TypeOf C Is Button Then
AddHandler C.MouseMove, AddressOf Bmm
End If
Next
Try
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Using stream As IO.Stream = IO.File.Open("Button.bin", IO.FileMode.Open)
B = formatter.Deserialize(stream)
stream.Close()
End Using
For Each But As Btn In B
With Me.Controls(But.Name)
.Text = But.Text
.Location = But.Location
.ForeColor = But.Forecolor
End With
Next
Catch ex As Exception
End Try
End Sub
Sub Bmm(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Right Then
sender.Location = New Point(sender.Left + e.X, e.Y + sender.top)
Me.Text = e.X + sender.Left
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Me.Controls("button" & Val(TextBox1.Text)).Text = Now.ToLongTimeString
Me.Controls("button" & TextBox1.Text).ForeColor = Color.FromArgb(255 * Rnd(), 255 * Rnd(), 255 * Rnd())
End Sub
End Class
<System.Serializable()> Class Btn
Public Name As String
Public Text As String
Public Location As Point
Public Forecolor As Color
End Class
Dim B As New List(Of Btn)
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
B.Clear()
For Each C As Control In Me.Controls
If TypeOf C Is Button Then
Dim Tmp As New Btn With {.Name = C.Name, .Text = C.Text, .Location = C.Location, .Forecolor = C.ForeColor}
B.Add(Tmp)
End If
Next
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Using stream As IO.Stream = IO.File.Create("Button.bin")
formatter.Serialize(stream, B)
stream.Close()
End Using
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each C As Control In Me.Controls
If TypeOf C Is Button Then
AddHandler C.MouseMove, AddressOf Bmm
End If
Next
Try
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Using stream As IO.Stream = IO.File.Open("Button.bin", IO.FileMode.Open)
B = formatter.Deserialize(stream)
stream.Close()
End Using
For Each But As Btn In B
With Me.Controls(But.Name)
.Text = But.Text
.Location = But.Location
.ForeColor = But.Forecolor
End With
Next
Catch ex As Exception
End Try
End Sub
Sub Bmm(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Right Then
sender.Location = New Point(sender.Left + e.X, e.Y + sender.top)
Me.Text = e.X + sender.Left
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Me.Controls("button" & Val(TextBox1.Text)).Text = Now.ToLongTimeString
Me.Controls("button" & TextBox1.Text).ForeColor = Color.FromArgb(255 * Rnd(), 255 * Rnd(), 255 * Rnd())
End Sub
End Class
<System.Serializable()> Class Btn
Public Name As String
Public Text As String
Public Location As Point
Public Forecolor As Color
End Class
Вродибы все работает
Цитата: Rayrat7
Вродибы все работает
С бинарным файлом это слишком, на мой взгляд.
Вообще сейчас мода в хмл записывать. Заодно в случае чего руками подредактировать можно.
Rayrat7, про сериализацию почитайте