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?
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
- Public Class Form1
- Inherits System.Windows.Forms.Form
- Private isdown As Boolean
- Private clickPoint As Point
- Private btnArray(1000000) As Button
- Dim pathdirectory As String = My.Computer.FileSystem.SpecialDirectories.ProgramFiles + "\Buttons bewegen"
- Dim path As String = My.Computer.FileSystem.SpecialDirectories.ProgramFiles + "\Buttons bewegen\location.ini"
- 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
- 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
- Public Function INI_ReadValueFromFile(ByVal strSection As String, ByVal strKey As String, ByVal strDefault As String, ByVal strFile As String) As String
- Dim strTemp As String = Space(1024), lLength As Integer
- lLength = GetPrivateProfileString(strSection, strKey, strDefault, strTemp, strTemp.Length, strFile)
- Return (strTemp.Substring(0, lLength))
- End Function
- Public Function INI_WriteValueToFile(ByVal strSection As String, ByVal strKey As String, ByVal strValue As String, ByVal strFile As String) As Boolean
- Return (Not (WritePrivateProfileString(strSection, strKey, strValue, strFile) = 0))
- End Function
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- If IO.File.Exists(path) = True Then
- IO.File.Delete(path)
- End If
- If IO.Directory.Exists(pathdirectory) = False Then
- IO.Directory.CreateDirectory(pathdirectory)
- If IO.File.Exists(path) = False Then
- IO.File.Create(path)
- End If
- End If
- End Sub
- Private Sub btnArray_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
- Dim itemClicked As Button = CType(sender, Button)
- Dim index As Integer = -1I
- For i As Integer = LBound(btnArray) To UBound(btnArray)
- If btnArray(i) Is itemClicked Then
- index = i
- Exit For
- End If
- Next
- If index >= 0 Then
- If isdown = True Then
- 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)
- End If
- End If
- End Sub
- Private Sub btnArray_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
- Dim itemClicked As Button = CType(sender, Button)
- Dim index As Integer = -1I
- For i As Integer = LBound(btnArray) To UBound(btnArray)
- If btnArray(i) Is itemClicked Then
- index = i
- Exit For
- End If
- Next
- If index >= 0 Then
- isdown = False
- End If
- End Sub
- Private Sub btnArray_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
- Dim itemClicked As Button = CType(sender, Button)
- Dim index As Integer = -1I
- For i As Integer = LBound(btnArray) To UBound(btnArray)
- If btnArray(i) Is itemClicked Then
- index = i
- Exit For
- End If
- Next
- If index >= 0 Then
- clickPoint = New Point(e.Location.X, e.Location.Y)
- isdown = True
- End If
- End Sub
- Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
- Try
- For i As Integer = LBound(btnArray) To UBound(btnArray)
- For Each Button As Control In Me.Controls
- INI_WriteValueToFile("Button(" & CStr(i) & ")", "X", btnArray(i).Location.X, path)
- INI_WriteValueToFile("Button(" & CStr(i) & ")", "Y", btnArray(i).Location.Y, path)
- My.Settings.buttons = CStr(i)
- Next
- Next
- Catch ex As Exception
- End Try
- End Sub
- Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
- For i As Integer = 0 To Val(TextBox1.Text) - 1
- btnArray(i) = New Button
- With btnArray(i)
- .Parent = Me
- .Parent.Controls.Add(btnArray(i))
- .Name = "Button" & CStr(i)
- .TabIndex = i
- .Text = "Button(" & CStr(i) & ")"
- .Size = New Size(80, 30)
- .Location = New Point(10, 10 + i * .Height)
- .Visible = True
- .FlatStyle = FlatStyle.Popup
- AddHandler .MouseDown, AddressOf btnArray_MouseDown
- AddHandler .MouseMove, AddressOf btnArray_MouseMove
- AddHandler .MouseUp, AddressOf btnArray_MouseUp
- End With
- Next i
- End Sub
- End Class