How to chose a fillrectangle color form a listbox list entry?

Štefan Mihael Rihar 181 Reputation points
2022-08-02T08:00:55.01+00:00

It is never enough. Accessibility with PC Systems is for me easier to maintain without the internet. Although it is easier i started to have a homepgae with github pages. Maybe the tensions are not only therefore what ever therefore is for me. I asumed that i would have more hlep with a homepage related to visual basic dot net.
However. I am still not able to choose form a list a knowncolor to change the color of a fillrectangle. The file content
A VBdotNet Form1 FileContent ColourFields.txt

is my start to collect information for my tensions.
Brainstorming. Fillrectangle has to be used with brushes. Hm. The class brush with, a i asum, sturcture or enum brushes and the colornames with brushes is not the same, still i asum, as with the class color. the methods fromknowncolor and knowncolor show with intellisens in visual studio an error meassge if i use them with brushes.

For some controls i do not know if the controls can be reused or new dynamic link liberaries have to be build. I asume not. The next step i am not able to execute is to convert brushes names to numbers so that an index number from a list can be choosen instead.

To aim "building", typing my own properties i am missing some points and cannot pinpont them.

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-08-03T08:50:01.477+00:00

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.