vb.net - set form background color programatically from a string variable

Related searches

I've got a string that contains an RGB value, so like "224,224,224". I'm trying to use that value to set the background color of a form, but its erroring out and I'm not sure why.

I'm trying...

 If Not this_dialog_backcolor = "" Then _
     new_dialog.BackColor = Color.FromArgb(this_dialog_backcolor)

I get the exception

control does not support transparent background colors.

I tried amending the string to contain a 4th value, so it became "255,224,224,224" and this also errored, giving the exception that the arithmatic operation resulted in an overload.

I also tried having the string formatted like so:

 Color [A=255, R=33, G=33, B=33]

This time i get the exception 'Conversion from string 'Color [A=255, R=33, G=33, B=33]' to type integer is not valid.

Any help appriciated.

FromArgb is a method that doesn't accept a string as parameter. So an automatic conversion happens here and you cannot be sure that this conversion do what you need to do. If you had Option Strict On this error would have been catched at compile time.

You can approach your problem in a different way, for example, you could split the string in its subparts and then call the FromArgb using the proper color values

Dim s As String = "224,224,224"

if Not string.IsNullOrEmpty(s) Then
    Dim p = s.Split(","c).Select(Function(x) int32.Parse(x.Trim()))
    form1.BackColor = Color.FromArgb(p(0),p(1),p(2))
End If

How to set the Background color of the Button in C#?, In Windows form, you are allowed to set the background color of the you can set the BackColor property of the Button programmatically with� Public Class Form1 'The next highlighted line should be on line in your code window.>>>>. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

You can use the ColorConverter from the namespace System.Drawing.

Dim converter = New ColorConverter()
Dim color = DirectCast(converter.ConvertFromString("255,224,224"), Color)

It can also convert colors given as web color name like "PaleVioletRed" and in hex format like "#FF0D60".

Control.Background Property (System.Windows.Controls), The following example shows how to set the background property of a control. XAML Copy. <Button Name� expression A variable that represents a TextBox object. Remarks. The BackColor property contains a numeric expression that corresponds to the color used to fill a control's or section's interior. You can set the default for this property by using a control's default control style or the DefaultControl property in Visual Basic.

So I've managed to achieve what i was after, but I'm not sure on its efficiency... it feels a bit dirty...

 If custom_color_scheme = true Then
 Dim back_color_bits() As String = this_dialog_backcolor.Replace(" ", "").Split(",")
 If Not this_dialog_backcolor = "" Then
     new_dialog.BackColor = Color.FromArgb(Convert.ToInt32(back_color_bits(0)), _
         Convert.ToInt32(back_color_bits(1)), Convert.ToInt32(back_color_bits(2)))
 End If

Like i said, it works, but i'm sure there must be a cleaner way. anybody?

Control.BackColor Property (System.Windows.Forms), Gets or sets the background color for the control. This code example requires that you have a Form with at least one child control; however, a child container� Drag and drop a label control, a button control and a ColorDialog control on the form. Set the Text property of the label and the button control to 'Give me a new Color' and 'Change Color', respectively. Change the font of the label as per your likings. Double-click the Change Color button and modify the code of the Click event.

C# Color Examples: FromKnownColor, FromName, FromName method to convert the string value into an actual color. We then set the form's background to that color. C# program that uses BackColor property using� The BackColor property does not support transparent colors unless the SupportsTransparentBackColor value of System.Windows.Forms.ControlStyles is set to true. The BackColor property is an ambient property. An ambient property is a control property that, if not set, is retrieved from the parent control.

C# TextBox Control - CSharp, A text box object is used to display text on a form or to get user input while a C# You can also collect the input value from a TextBox control to a variable like this way. You can set background color and foreground color through property window Parse method Converts the string representation of a number to its integer� In this method, you can set the BackColor property of the Button programmatically with the help of given syntax: public override System.Drawing.Color BackColor { get; set; } Here, Color indicates the background color of the button. Following steps are used to set the BackColor property of the Button:

Console.WriteLine("All the foreground colors except {0}, the background color:", currentBackground) For Each color In colors If color = currentBackground Then Continue For Console.ForegroundColor = color Console.WriteLine(" The foreground color is {0}.", color) Next Console.WriteLine() ' Restore the foreground color.

  • Is this related to a WPF application or is this a WinForms one?
  • I like that - its tidier than what i came up with. Just implementing now...
  • Maybe a note that, since the OP is referring to a Form, to support transparent colors it's only required to SetStyle(ControlStyles.SupportsTransparentBackColor, true); in the consructor.