System color name error - VB .net

José Carlos 886 Reputation points
2023-09-29T17:51:08.5533333+00:00

Friends.

I'm making a program where the user enters the RGB values of the colors, and I show the color and the color name. The program is not showing the name but the hexa code. I've tried and I can't find where the error is. See the code.

If Integer.TryParse(txtRed.Text, red) AndAlso Integer.TryParse(txtGreen.Text, green) AndAlso    Integer.TryParse(txtBlue.Text, blue) Then             
            
	Dim cor As Color = Color.FromArgb(red, green, blue)              
             
	lblNomeCor.Text = cor.Name             
	picCor.BackColor = cor         
Else             
	MessageBox.Show("Enter valid values for R, G e B.", "Erro", MessageBoxButtons.OK, 				 MessageBoxIcon.Error)         
End If
Developer technologies VB
Developer technologies Visual Studio Other
{count} votes

Accepted answer
  1. Anonymous
    2023-09-29T20:29:38.6+00:00

    Hi

    As Dwayne says, this is not an error, just that there are more RGB values that don't have a given name than those that do. If you want, you could offer the User a List of Colours which do have a name and prevent them from using a random RGB unnamed colour.

    If that is an option you want to pursue then here is some sample code to show one way to do it.

    This is a test example and you should try it out first. All it needs is a new Project with a Default Foem1 and a ComboBox1 and Labels 1 & 2 added via the Designer, then copy/replace the default code with this code.

    111

    ' Form1 with a ComboBox1 and a Label1
    ' and a Label2
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		' from your post
    		Dim c As Color = Color.Red
    		Dim s As String = c.Name  ' gives RED
    
    		c = Color.FromArgb(255, 222, 111, 200)
    		s = c.Name 'gives "ffde6fc8" which is
    		' the RGB values in Hex (as you said)
    
    
    		' Possible alternative
    		' set up a ComboBox to allow User to
    		' select only a KnowBolor
    		ComboBox1.DataSource = [Enum].GetValues(GetType(KnownColor))
    
    	End Sub
    
    	Private Sub ComboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    
    		Dim cb As ComboBox = DirectCast(sender, ComboBox)
    
    		Dim c As Color = Color.FromKnownColor(DirectCast(cb.SelectedItem, KnownColor))
    
    		Label1.BackColor = c
    
    		Label2.Text = CInt(c.A).ToString & ", " & CInt(c.R).ToString & ", " & CInt(c.G).ToString & ", " & CInt(c.B).ToString
    	End Sub
    End Class
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. KOZ6.0 6,655 Reputation points
    2023-09-29T20:25:28.9+00:00

    Let's create a dictionary for KnownColor

    Private _NamedColors As Dictionary(Of Color, String)
    
    Private ReadOnly Property NamedColors As IReadOnlyDictionary(Of Color, String)
        Get
            If _NamedColors Is Nothing Then
                _NamedColors = New Dictionary(Of Color, String)()
                For Each kColor As KnownColor In [Enum].GetValues(GetType(KnownColor))
                    Dim color As Color = 
                             Color.FromArgb(Color.FromKnownColor(kColor).ToArgb())
                    _NamedColors(color) = kColor.ToString()
                Next
            End If
            Return _NamedColors
        End Get
    End Property
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim red, green, blue As Integer
        If Integer.TryParse(txtRed.Text, red) AndAlso Integer.TryParse(txtGreen.Text, green) AndAlso Integer.TryParse(txtBlue.Text, blue) Then
            Try
                Dim cor As Color = Color.FromArgb(red, green, blue)
                Dim nameStr As String = Nothing
                If NamedColors.TryGetValue(cor, nameStr) Then
                    lblNomeCor.Text = nameStr
                Else
                    lblNomeCor.Text = cor.Name
                End If
                picCor.BackColor = cor
            Catch ex As ArgumentException
                MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        Else
            MessageBox.Show("Enter valid values for R, G e B.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
    

    TextBox is fine, but try NumericUpDown as well.:)

    1 person found this answer 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.