mehrere Panels speichern

  • VB.NET

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    mehrere Panels speichern

    Ein Programm soll x Buttons per Code erzeugen. Diese buttons können ihre
    Position verändern. Beim Schließen des Programms werden alle Buttons in
    einer Konfigurationsdatei gespeichert. Wie mache ich es, dass die
    Buttons beim nächsten Programmstart wieder an ihrer gespeicherten
    Position erstellt werden?

    Quellcode

    1. Public Class Form1
    2. Inherits System.Windows.Forms.Form
    3. Private isdown As Boolean
    4. Private clickPoint As Point
    5. Private btnArray(1000000) As Button
    6. Dim pathdirectory As String = My.Computer.FileSystem.SpecialDirectories.ProgramFiles + "\Buttons bewegen"
    7. Dim path As String = My.Computer.FileSystem.SpecialDirectories.ProgramFiles + "\Buttons bewegen\location.ini"
    8. Private Declare Ansi Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
    9. Private Declare Ansi Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
    10. Public Function INI_ReadValueFromFile(ByVal strSection As String, ByVal strKey As String, ByVal strDefault As String, ByVal strFile As String) As String
    11. Dim strTemp As String = Space(1024), lLength As Integer
    12. lLength = GetPrivateProfileString(strSection, strKey, strDefault, strTemp, strTemp.Length, strFile)
    13. Return (strTemp.Substring(0, lLength))
    14. End Function
    15. Public Function INI_WriteValueToFile(ByVal strSection As String, ByVal strKey As String, ByVal strValue As String, ByVal strFile As String) As Boolean
    16. Return (Not (WritePrivateProfileString(strSection, strKey, strValue, strFile) = 0))
    17. End Function
    18. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19. If IO.File.Exists(path) = True Then
    20. IO.File.Delete(path)
    21. End If
    22. If IO.Directory.Exists(pathdirectory) = False Then
    23. IO.Directory.CreateDirectory(pathdirectory)
    24. If IO.File.Exists(path) = False Then
    25. IO.File.Create(path)
    26. End If
    27. End If
    28. End Sub
    29. Private Sub btnArray_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
    30. Dim itemClicked As Button = CType(sender, Button)
    31. Dim index As Integer = -1I
    32. For i As Integer = LBound(btnArray) To UBound(btnArray)
    33. If btnArray(i) Is itemClicked Then
    34. index = i
    35. Exit For
    36. End If
    37. Next
    38. If index >= 0 Then
    39. If isdown = True Then
    40. btnArray(index).Location = New Point(btnArray(index).Location.X - (clickPoint.X - e.Location.X), btnArray(index).Location.Y - (clickPoint.Y - e.Location.Y)) ' Berechnung des Abstands zum anfänglichen Punkt auf dem Button -> den Wert dann vom Button abziehen bzw. addieren (wenn negative Zahl heraus kommt)
    41. End If
    42. End If
    43. End Sub
    44. Private Sub btnArray_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
    45. Dim itemClicked As Button = CType(sender, Button)
    46. Dim index As Integer = -1I
    47. For i As Integer = LBound(btnArray) To UBound(btnArray)
    48. If btnArray(i) Is itemClicked Then
    49. index = i
    50. Exit For
    51. End If
    52. Next
    53. If index >= 0 Then
    54. isdown = False
    55. End If
    56. End Sub
    57. Private Sub btnArray_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
    58. Dim itemClicked As Button = CType(sender, Button)
    59. Dim index As Integer = -1I
    60. For i As Integer = LBound(btnArray) To UBound(btnArray)
    61. If btnArray(i) Is itemClicked Then
    62. index = i
    63. Exit For
    64. End If
    65. Next
    66. If index >= 0 Then
    67. clickPoint = New Point(e.Location.X, e.Location.Y)
    68. isdown = True
    69. End If
    70. End Sub
    71. Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    72. Try
    73. For i As Integer = LBound(btnArray) To UBound(btnArray)
    74. For Each Button As Control In Me.Controls
    75. INI_WriteValueToFile("Button(" & CStr(i) & ")", "X", btnArray(i).Location.X, path)
    76. INI_WriteValueToFile("Button(" & CStr(i) & ")", "Y", btnArray(i).Location.Y, path)
    77. My.Settings.buttons = CStr(i)
    78. Next
    79. Next
    80. Catch ex As Exception
    81. End Try
    82. End Sub
    83. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    84. For i As Integer = 0 To Val(TextBox1.Text) - 1
    85. btnArray(i) = New Button
    86. With btnArray(i)
    87. .Parent = Me
    88. .Parent.Controls.Add(btnArray(i))
    89. .Name = "Button" & CStr(i)
    90. .TabIndex = i
    91. .Text = "Button(" & CStr(i) & ")"
    92. .Size = New Size(80, 30)
    93. .Location = New Point(10, 10 + i * .Height)
    94. .Visible = True
    95. .FlatStyle = FlatStyle.Popup
    96. AddHandler .MouseDown, AddressOf btnArray_MouseDown
    97. AddHandler .MouseMove, AddressOf btnArray_MouseMove
    98. AddHandler .MouseUp, AddressOf btnArray_MouseUp
    99. End With
    100. Next i
    101. End Sub
    102. End Class
    Die gespeicherten Daten auslesen und den Buttons zuweisen?
    So wie du ausließt und abspeicherst nur eben anders herrum, die Code Zeile quasi umdrehen.

    Aber das meinst Du nicht wirklich oder doch?

    Das Ereignis Load/Init wirst Du ja wohl kennen, wenn ich mir so deinen Code anschaue.

    Und wofür braucht man bitte 1.000.000 Buttons?!
    Die 1000000 Buttons sind nur das Maximale, wieviele buttons erstellt werden, bestimmt der User.
    Ich habe es schon folgendermaßen versucht.

    VB.NET-Quellcode

    1. Try
    2. For i As Integer = LBound(btnArray) To UBound(btnArray)
    3. For Each Button As String In path
    4. btnArray(i) = New Button
    5. With btnArray(i)
    6. .Parent = Me
    7. .Parent.Controls.Add(btnArray(i))
    8. .Name = "Button" & CStr(i)
    9. .TabIndex = i
    10. .Text = "Button(" & CStr(i) & ")"
    11. .Size = New Size(INI_ReadValueFromFile("Button(" & CStr(i) & ")", "X", "", path), (INI_ReadValueFromFile("Button(" & CStr(i) & ")", "Y", "", path)))
    12. .Location = New Point(10, 10 + i * .Height)
    13. .Visible = True
    14. .FlatStyle = FlatStyle.Popup
    15. AddHandler .MouseDown, AddressOf btnArray_MouseDown
    16. AddHandler .MouseMove, AddressOf btnArray_MouseMove
    17. AddHandler .MouseUp, AddressOf btnArray_MouseUp
    18. End With
    19. My.Settings.buttons = CStr(i)
    20. Next
    21. Next i
    22. Catch ex As Exception
    23. End Try


    Allerdings wird mir nun nur 1 Button mit der Location (0, 0) im Standartdesign angezeigt (was eigentlich nicht sein sollte).
    Mach Dir eine DataTable, wo Du für jeden Button eine Zeile anlegst.
    Die DataTable kannst Du per DataTable.SaveXml(PFAD) und DataTable.LoadXml(PFAD) ganz easy handeln.
    Außerdem:
    Statt

    VB.NET-Quellcode

    1. Private btnArray(1000000) As Button

    malch lieber

    VB.NET-Quellcode

    1. Private btnArray As List(Of Button)
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Probier mal dies:
    Form mit DataGridView (leer!) und Button
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private _DataTable As DataTable
    3. Public Sub New()
    4. InitializeComponent()
    5. Dim _Bezeichnung As New System.Data.DataColumn("Bezeichnung")
    6. _Bezeichnung.DataType = GetType([String])
    7. '_Bezeichnung.AutoIncrement = true ' nur bei GetType([Integer])
    8. Dim _Inhalt As New System.Data.DataColumn("Inhalt")
    9. _Inhalt.DataType = GetType([String])
    10. _DataTable = New System.Data.DataTable("MyTableName")
    11. _DataTable.Columns.Add(_Bezeichnung)
    12. _DataTable.Columns.Add(_Inhalt)
    13. _DataTable.Constraints.Add("Key1", _Bezeichnung, True) ' "Key1" ist der Name der Einschränkung; IDColumn ist Primärschlüssel
    14. DataGridView1.DataSource = _DataTable
    15. End Sub
    16. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    17. _DataTable.WriteXml("DEIN_FILENAME")
    18. End Sub
    19. End Class
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!