0

我正在开发一个包含已保存用户首选项的程序。用户可以设置颜色的程序,它将被保存以再次使用。但是,在我发现将 System.Drawing.Event ARGB 保存为 Integer 字符串以另存为文件的数小时工作之后,尽我所能尝试。

下面的代码显示了我最成功的尝试,我可以进行十六进制转换,但无法成功将其返回到 ARGB

    Dim color As New ColorDialog
    Dim userpref As String = ColorTranslator.ToHtml(color.Color)
    Dim readcolor As Color = ColorTranslator.FromHtml(userpref)
    If (color.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
        Button1.BackColor = Drawing.Color.FromArgb(readcolor)
    End If

尝试转换为字符串或整数时,通常我只是得到不是我想要的随机数或每种颜色的颜色 [黑色] 请帮忙!

4

2 回答 2

1

尝试使用 ColorConverter 类。

Private colorConv As New ColorConverter

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim color As New ColorDialog
    Dim userpref As String
    Dim readcolor As Color

    If (color.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
        userpref = colorConv.ConvertToString(color.Color)
        readcolor = colorConv.ConvertFromString(userpref)
        Button1.BackColor = readcolor
    End If
End Sub
于 2014-12-22T10:45:44.177 回答
0

您正在尝试在向用户显示之前使用对话框中选择的颜色,因此是随机颜色。将转换为字符串的代码移回 If 块内(并在显示对话框之后),它应该没问题:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim color As New ColorDialog
    If color.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim userpref As String = ColorTranslator.ToHtml(color.Color)
        Debug.Print("userpref = " & userpref)
        Dim readcolor As Color = ColorTranslator.FromHtml(userpref)
        Button1.BackColor = readcolor
    End If
End Sub
于 2014-12-22T15:01:31.220 回答