简体   繁体   English

VB 2010保存和加载颜色对话框

[英]VB 2010 save and load Color Dialog

I am working on a program that contains saved user preferences. 我正在开发一个包含保存的用户首选项的程序。 I the program a user can set a color and it will be saved to use again. 在程序中,用户可以设置颜色,然后将其保存以供再次使用。 However, try as I might their is no way after hours of work that I've found to save a System.Drawing.Event ARGB to a string of Integer to save as a file. 但是,尝试工作后,我发现他们无法将System.Drawing.Event ARGB保存到Integer字符串以另存为文件。

The code below shows my most successful attempt I can get the hex conversion to work but cannot succeed in returning it to 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

When trying to convert to Strings or Integers usually I just get random numbers that aren't what I want or Color [Black] for every color please help! 通常,当我尝试转换为字符串或整数时,我只会得到不是我想要的随机数,或者会得到每种颜色的颜色[黑色],请帮忙!

Try using the ColorConverter Class. 尝试使用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

You're attempting to use the selected color from the dialog before it's been shown to the user, thus the random colors. 您正在尝试使用在对话框中选择的颜色, 然后再将其显示给用户,从而使用随机颜色。 Move the code that converts to a string and back to within the If block (and after the dialog has been displayed) and it should be fine: 将转换为字符串的代码移至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

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

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