简体   繁体   English

vb.net-通过字符串变量以编程方式设置表格背景色

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

I've got a string that contains an RGB value, so like "224,224,224". 我有一个包含RGB值的字符串,例如“ 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. 我尝试将字符串修改为包含第4个值,因此该字符串变为“ 255,224,224,224”,并且也出现了错误,这是因为算术运算导致重载。

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. 这次我得到异常'从字符串'Color [A = 255,R = 33,G = 33,B = 33]转换为整数”是无效的。

Any help appriciated. 任何帮助。

FromArgb is a method that doesn't accept a string as parameter. FromArgb是不接受字符串作为参数的方法。 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. 如果您选择Option Strict On ,则在编译时会捕获到此错误。

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 您可以用不同的方法来解决问题,例如,可以将字符串拆分为多个子部分,然后使用正确的颜色值调用FromArgb

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

You can use the ColorConverter from the namespace System.Drawing . 您可以从名称空间System.Drawing使用ColorConverter

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" . 它还可以转换以Web颜色名称(如"PaleVioletRed"和十六进制格式(如"#FF0D60"给出的颜色。

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? 有人吗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM