@Amr_Ali
Full source
Note CheckBoxes don't have Text set because I didn't set it but you can, that's what happens when I'm pressed for time, sorry.
Imports System.IO
Imports Newtonsoft.Json
Public Class FormOperations
Public Shared Container As New List(Of ControlContainer)
Public Shared Property FileName() As String
Public Shared ReadOnly Property HasControls() As Boolean
Get
Return Container.Count > 0
End Get
End Property
Public Shared ReadOnly Property FileExists() As Boolean
Get
Return File.Exists(FileName)
End Get
End Property
Public Shared Sub SerializeToFile()
If HasControls Then
Dim output As String = JsonConvert.SerializeObject(Container, Formatting.Indented)
File.WriteAllText(FileName, output)
End If
End Sub
Public Shared Function DeserializeFromFile() As Boolean
If FileExists Then
Dim json = File.ReadAllText(FileName)
Container = JsonConvert.DeserializeObject(Of List(Of ControlContainer))(json)
Return True
Else
Return False
End If
End Function
Public Shared Sub Collect(sender As Form)
Container.Clear()
Dim itemList As New List(Of ControlContainer)
sender.ButtonList.ForEach(Sub(button)
itemList.Add(New ControlContainer() With {
.Type = GetType(Button),
.Text = button.Text,
.Anchor = button.Anchor,
.Location = button.Location,
.TabIndex = button.TabIndex,
.Name = button.Name,
.FormName = sender.Name
}
)
End Sub)
sender.CheckBoxList.ForEach(Sub(checkBox)
itemList.Add(New ControlContainer() With {
.Type = GetType(CheckBox),
.Anchor = checkBox.Anchor,
.Location = checkBox.Location,
.TabIndex = checkBox.TabIndex,
.Name = checkBox.Name,
.Checked = checkBox.Checked,
.FormName = sender.Name
}
)
End Sub)
Dim buttons = itemList.Where(Function(item) item.Type Is GetType(Button)).ToList()
Dim checkBoxes = itemList.Where(Function(item) item.Type Is GetType(CheckBox)).ToList()
Container.AddRange(buttons)
Container.AddRange(checkBoxes)
End Sub
Public Shared Function GetByForm(sender As String) As List(Of ControlContainer)
If HasControls Then
Return Container.Where(Function(item) item.FormName = sender).ToList()
Else
Return Nothing
End If
End Function
End Class
Public Class Form1
Private Sub CollectButton_Click(sender As Object, e As EventArgs) Handles CollectButton.Click
FormOperations.Collect(Me)
End Sub
Private Sub GetByFormnName_Click(sender As Object, e As EventArgs) Handles GetByFormNameButton.Click
Dim controls = FormOperations.GetByForm(Name)
If controls IsNot Nothing Then
MessageBox.Show(controls.Count.ToString())
Else
MessageBox.Show("No controls located")
End If
End Sub
Private Sub SerializeButton_Click(sender As Object, e As EventArgs) Handles SerializeButton.Click
FormOperations.SerializeToFile()
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
FormOperations.FileName = "Forms.json"
End Sub
Private Sub DeserializeFromFile_Click(sender As Object, e As EventArgs) Handles DeserializeFromFileButton.Click
If FormOperations.DeserializeFromFile() Then
For Each controlContainer As ControlContainer In FormOperations.Container
Debug.WriteLine($"{controlContainer.FormName}.{controlContainer.Name} is {controlContainer.Type}")
Next
End If
End Sub
End Class
JSON
[
{
"FormName": "Form1",
"Text": "Button3",
"Location": "108, 28",
"Name": "Button3",
"Size": "0, 0",
"Anchor": 5,
"TabIndex": 5,
"Type": "System.Windows.Forms.Button, System.Windows.Forms, Version=5.0.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"Checked": false
},
{
"FormName": "Form1",
"Text": "Button2",
"Location": "27, 76",
"Name": "Button2",
"Size": "0, 0",
"Anchor": 9,
"TabIndex": 1,
"Type": "System.Windows.Forms.Button, System.Windows.Forms, Version=5.0.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"Checked": false
},
{
"FormName": "Form1",
"Text": "Button1",
"Location": "27, 31",
"Name": "Button1",
"Size": "0, 0",
"Anchor": 5,
"TabIndex": 0,
"Type": "System.Windows.Forms.Button, System.Windows.Forms, Version=5.0.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"Checked": false
},
{
"FormName": "Form1",
"Text": null,
"Location": "18, 191",
"Name": "CheckBox2",
"Size": "0, 0",
"Anchor": 5,
"TabIndex": 4,
"Type": "System.Windows.Forms.CheckBox, System.Windows.Forms, Version=5.0.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"Checked": false
},
{
"FormName": "Form1",
"Text": null,
"Location": "19, 158",
"Name": "CheckBox1",
"Size": "0, 0",
"Anchor": 5,
"TabIndex": 3,
"Type": "System.Windows.Forms.CheckBox, System.Windows.Forms, Version=5.0.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"Checked": false
}
]