Zachary
Zachary

Reputation: 3

Visual Studio Custom Color

So I'm wondering if there is a way where I can have a custom color background Using the RGB. I have the three text boxes one for Red one for Green and One for the Blue. But when i try this code for it

Me.BackColor = Color.FromArgb(TextBox1.Text, TextBox2.Text, TextBox3.Text)

It just won't work... I Tried searching it up on Google and no results found for what I want it to do

Upvotes: 0

Views: 440

Answers (2)

Mark Hall
Mark Hall

Reputation: 54532

First thing I would say is to make sure that you enable Option Strict it will tell the compiler to notify you of implicit type conversions among other things.

Restricts implicit data type conversions to only widening conversions, disallows late binding, and disallows implicit typing that results in an Object type

Since you are using a TextBox for input I would use the Integer.TryParse Method to make sure that you are entering the proper data, something like this.

Private Sub SetBackground()
    Dim argR, argG, argB As Byte
        If Byte.TryParse(TextBox1.Text, argR) Then
            If Byte.TryParse(TextBox2.Text, argG) Then
                If Byte.TryParse(TextBox3.Text, argB) Then
                    Me.BackColor = Color.FromArgb(argR, argG, argB)
                    Return
                End If
            End If
        End If
        MsgBox("Error Invalid Entry", MsgBoxStyle.Information, "Entry Error")
End Sub

Upvotes: 1

ElektroStudios
ElektroStudios

Reputation: 20464

I suggest you to avoid textboxes then better use a NumericUpDown control in order to friendly set a maximum value of 255, then suscribe to the ValueChanged or KeyUp event depending on the responsiveness behavior you want.

An example:

enter image description here

Public Class Form1 : Inherits Form

    Private Sub NumericUpDownRGB_ValueChanged(sender As Object, e As KeyEventArgs) _
    Handles NumericUpDownR.ValueChanged,
            NumericUpDownG.ValueChanged,
            NumericUpDownB.ValueChanged

        Dim r As Integer = CInt(Me.NumericUpDownR.Value)
        Dim g As Integer = CInt(Me.NumericUpDownG.Value)
        Dim b As Integer = CInt(Me.NumericUpDownB.Value)

        Me.BackColor = Color.FromArgb(Me.BackColor.A, r, g, b)

    End Sub

End Class

Upvotes: 1

Related Questions