VB 2010保存并加载Color Dialog

时间:2014-12-22 05:55:45

标签: vb.net hex argb colordialog

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

下面的代码显示了我最成功的尝试,我可以让十六进制转换工作,但无法成功将其返回到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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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