Changing Me.Backcolor to one that a user defines.

MiserableMad

Active member
Joined
Feb 2, 2007
Messages
25
Programming Experience
Beginner
What I want to do is input a color, example "red", in a textbox, and the backcolor changes. I assume it is something to do with a Function(byval Change as Color) and a cType. But I have no idea, help...please.
 
VB.NET:
Dim inputcolor As Color = Color.FromName("Red")
 
VB.NET:
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Color.FromName(TextBox1.Text).IsKnownColor Then
            Try
                Me.BackColor = Color.FromName(TextBox1.Text)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub
 
FromName doesn't raise exception, it returns black (argb 0 values) if the color string isn't known. The check for IsKnownColor could be useful to give feedback to user if written color string wasn't recognized, but so could providing the user with a better color selection interface.. :) A nice thing about the FromName function is that it recognize color name strings case-insensitive.
 
True, but it is not possible to assign a colour that contains transparency or a transparent component, to the backcolor of a form. Not knowing whether any of the named colours did or did not contravene this limitation, I put the Try in! The only message I ever expect it would show, would be "COntrol cannot accept a transparent color " or something like that... :D
 
Very clever! Actually the message is:
Control does not support transparent background colors.
 
Back
Top