Hi @Štefan Mihael Rihar ,
Try the following code.
Imports System.ComponentModel
Public Class Form1
WithEvents SmoothieMixer, BarrierField As New ListBox
WithEvents LabelRectangle As New MyLabel
Public ColorObjList As New ArrayList
Public Sub New()
Dim ColorNames(), ColorName As String
ColorNames = System.Enum.GetNames(GetType(KnownColor))
Dim cnvrt As TypeConverter = TypeDescriptor.GetConverter(GetType(KnownColor))
For Each ColorName In ColorNames
ColorObjList.Add(Color.FromKnownColor(cnvrt.ConvertFromString(ColorName)))
Next
Dim list1 As New List(Of Rectangle)()
For i As Integer = 0 To 10
list1.Add(New Rectangle(10, i * 2, 50, 50))
Next
' Dieser Aufruf ist für den Designer erforderlich.
InitializeComponent()
SmoothieMixer.DataSource = list1
With SmoothieMixer
.Location = New Point(50, 50)
.Size = New Size(200, 160)
End With
BarrierField.DataSource = ColorObjList
With BarrierField
.Location = New Point(350, 50)
.Size = New Size(200, 160)
End With
With LabelRectangle
.Location = New Point(10, SmoothieMixer.Bottom + 10)
.Size = New Size(100, 100)
.BorderStyle = BorderStyle.FixedSingle
.squaresnippet = list1(0)
End With
Controls.AddRange({SmoothieMixer, BarrierField, LabelRectangle})
' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
End Sub
Private Sub SmoothieMixer_SelectedIndexChanged(sender As Object, e As EventArgs) Handles SmoothieMixer.SelectedIndexChanged
Dim lb As ListBox = CType(sender, Control)
LabelRectangle.squaresnippet = lb.SelectedItem
LabelRectangle.Invalidate()
End Sub
Private Sub BarrierField_SelectedIndexChanged(sender As Object, e As EventArgs) Handles BarrierField.SelectedIndexChanged
Dim lb As ListBox = CType(sender, Control)
LabelRectangle.squarecolor = lb.SelectedItem
LabelRectangle.Invalidate()
End Sub
Public Class MyLabel
Inherits Label
Public squaresnippet As Rectangle
Public squarecolor As Color
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Using br = New SolidBrush(squarecolor)
If squaresnippet <> Nothing Then
e.Graphics.FillRectangle(br, squaresnippet)
End If
End Using
End Sub
End Class
End Class
Best Regards.
Jiachen Li
----------
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.