Public Class Form1
Dim WithEvents mGrid As Griglia(Of Pannello)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
mGrid = New Griglia(Of Pannello)(Me, True, 30, 5, 10, 10)
For Each elemento As Pannello In mGrid
AddHandler elemento.MouseDown, AddressOf aggiungi
Next
End Sub
Sub aggiungi(sender As Object, e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim child As New miaLabel
AddHandler child.MouseDown, AddressOf genitore
sender.controls.add(child)
Dim base = 0
For Each C In sender.controls
C.width = sender.width / sender.controls.count
C.location = New Point(base * sender.width / sender.controls.count, 0)
base += 1
C.bringtofront()
Next
Else
End If
End Sub
Sub genitore(sender As Object, e As MouseEventArgs)
aggiungi(sender.parent, e)
End Sub
End Class
Class Pannello
Inherits Panel
Sub New()
Width = 100
Height = 20
BorderStyle = Windows.Forms.BorderStyle.FixedSingle
End Sub
End Class
Il problema è che quando clicco sul contenitore o su uno dei contenuti con il sinistro il risultato deve essere l'aggiunta di un contenuto al contenitore, mentre quando clicco sul contenitore con il destro non deve succedere niente, mentre se clicco su un contenuto con il destro, questo deve essere rimosso.Cominciamo a isolare una parte del codice che è quella che ridistribuisce lo spazio del contenitore fra i contenuti.
Sub aggiungi(sender As Object, e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim child As New miaLabel
AddHandler child.MouseDown, AddressOf genitore
sender.controls.add(child)
Ridistribuisci(sender)
Else
End If
End Sub
.........
Private Sub Ridistribuisci(contenitore As Object)
Dim base = 0
For Each C In contenitore.controls
C.width = contenitore.width / contenitore.controls.count
C.location = New Point(base * contenitore.width / contenitore.controls.count, 0)
base += 1
C.bringtofront()
Next
End Sub
...Ho trovato la soluzione:
Public Class Form1
Dim WithEvents mGrid As Griglia(Of Pannello)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
mGrid = New Griglia(Of Pannello)(Me, True, 30, 5, 10, 10)
For Each elemento As Pannello In mGrid
AddHandler elemento.MouseDown, AddressOf aggiungi
Next
End Sub
Sub aggiungi(sender As Object, e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim child As New miaLabel
AddHandler child.MouseDown, AddressOf ClickContenuto
sender.controls.add(child)
Ridistribuisci(sender)
End If
End Sub
Sub ClickContenuto(sender As Object, e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
aggiungi(sender.parent, e)
Else
Dim temp As Object = sender.parent
sender.parent.controls.remove(sender)
Ridistribuisci(temp)
temp = Nothing
End If
End Sub
Private Sub Ridistribuisci(contenitore As Object)
Dim base = 0
For Each C In contenitore.Controls
C.width = contenitore.Width / contenitore.Controls.Count
C.location = New Point(base * contenitore.Width / contenitore.Controls.Count, 0)
base += 1
C.bringtofront()
Next
End Sub
End Class
Class Pannello
Inherits Panel
Sub New()
Width = 100
Height = 20
BorderStyle = Windows.Forms.BorderStyle.FixedSingle
End Sub
End Class
Ecco il risultato (bella idea, nei filmati che non commento a voce, quella di inserire brani musicali):
Nessun commento:
Posta un commento