【问题标题】:VB 2010 save and load Color DialogVB 2010 保存和加载颜色对话框
【发布时间】:2014-12-22 05:55:45
【问题描述】:

我正在开发一个包含已保存用户首选项的程序。用户可以设置颜色的程序,它将被保存以再次使用。但是,经过数小时的工作,我发现将 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

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

【问题讨论】:

    标签: vb.net hex argb colordialog


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      您正在尝试使用从对话框显示给用户之前选择的颜色,因此是随机颜色。将转换为字符串的代码移回 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-04-21
        • 2016-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多