2017-10-23 19 views
0

코드를 찾을 수는 없지만이 사이트에서 찾은 일부 코드에서 색상 선택기 콤보 박스를 만들었습니다.숫자 값에서 명명 된 색으로 콤보 상자를 선택하는 방법

코드 색상을 그리려면

Private Sub cboColorPicker_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles cboColor.DrawItem, cboBackground.DrawItem 

    Dim g As Graphics 
    Dim ItemBrush As Brush 
    Dim ItemColor As Color 
    Dim ItemFont As Font 
    Dim ItemName As String 
    Dim rect As Rectangle 


    g = e.Graphics 
    rect = e.Bounds 

    If e.Index >= 0 Then 
     'Get item color name 
     ItemName = CType(sender, ComboBox).Items(e.Index).ToString 

     'Get instance a font to draw item name with this style 
     ItemFont = New Font("Arial", 9, FontStyle.Regular) 

     'Get instance color from item name 
     ItemColor = Color.FromName(ItemName) 

     'Get instance brush with Solid style to draw background 
     ItemBrush = New SolidBrush(ItemColor) 

     'Draw the item name 
     g.DrawString(ItemName, ItemFont, Brushes.Black, rect.X, rect.Top) 

     'Draw the background with my brush style and rectangle of item 
     g.FillRectangle(ItemBrush, rect.X, rect.Y, rect.Width, rect.Height) 

    End If 

End Sub 

나는 이런 식으로 콤보 상자를 채우는 :

Private Sub PopulateColorCombo(ByVal cbo As ComboBox) 

    cbo.Items.Add("Black") 
    cbo.Items.Add("Blue") 
    cbo.Items.Add("Lime") 
    cbo.Items.Add("Cyan") 
    cbo.Items.Add("Red") 
    cbo.Items.Add("Fuchsia") 
    cbo.Items.Add("Yellow") 
    cbo.Items.Add("White") 
    cbo.Items.Add("Navy") 
    cbo.Items.Add("Green") 
    cbo.Items.Add("Teal") 
    cbo.Items.Add("Maroon") 
    cbo.Items.Add("Purple") 
    cbo.Items.Add("Olive") 
    cbo.Items.Add("Gray") 

End Sub 

내가 int로 데이터베이스에 값을 저장 될 것입니다 예를 들어 검은 색 -16777216.

사용자가 보고서를 사용자 정의 할 수있는 양식의 일부입니다. 데이터베이스에 저장된 내용을 기반으로 색상을 표시하기 위해 데이터가로드 될 때 콤보를 업데이트하고 싶습니다. 나는 이것을 할 수 없다.

그래서 내가 HDI.Color = -16777216

Color.FromArgb(HDI.Color).ToString = "Color [A=255, R=0, G=0, B=0]" 
Color.FromArgb(HDI.Color).Name = "ff000000" 

로로드 데이터 말을하지만 실제 색상 이름을 얻을 수 없습니다. 다음 작업은 없습니다.

cboColor.SelectedIndex = cboColor.FindStringExact(Color.FromArgb(HDI.Color).ToKnownColor.ToString) 
cboColor.SelectedIndex = cboColor.FindStringExact(CType(Color.FromArgb(HDI.Color), Color).ToString) 
cboColor.SelectedIndex = cboColor.FindStringExact(System.Drawing.ColorTranslator.FromHtml(Color.FromArgb(HDI.Color).Name).ToString) 

이 작업에 잘못된 콤보 박스를 사용하고 있습니까? 그것을 작동시키는 방법이 있습니까?

많은 색상을 사용하지 않으므로 전체 색상 선택기를 제공하고 싶지 않았으므로 사용자가 간단하게 유지하려고했습니다. 나도 색상을 절반으로 줄일 수 있습니다.

수정 : 누출 문제를 수정하십시오. 여기, enter image description here

+0

[색상 선택기 콤보 상자] (https://stackoverflow.com/a/25616698/719186)의 코드가 있다고 생각합니다. 콤보 상자에 "문자열"을 추가하고 있으므로 데이터베이스에 "문자열"을 저장하는 것이 좋습니다. – LarsTech

+0

코드가 누출됩니다. 각 항목에 대해 새 글꼴이 필요하지 않습니다. cbo를 원하는 글꼴로 설정하고이를 사용하십시오. 또한 브러쉬를 처리해야합니다 – Plutonix

+0

데이터베이스에'Color.ToArgb' 값을 저장하는 대신 KnownColor enum 값을 저장하십시오. 그렇게하면 KnownColor를 다시 만들고 이름에 액세스 할 수 있습니다. – TnTinMn

답변

0

마지막을받은 솔루션입니다 :

개인 서브 cboColorPicker_DrawItem (개체로 ByVal의 보낸 사람, DrawItemEventArgs으로 ByVal의 e)이 테이블 구조가 cboColor.DrawItem, cboBackground.DrawItem

Dim g As Graphics 
    Dim ItemColor As Color 
    Dim ItemName As String 
    Dim rect As Rectangle 


    g = e.Graphics 
    rect = e.Bounds 

    If e.Index >= 0 Then 
     'Get item color name 
     ItemName = CType(sender, ComboBox).Items(e.Index).ToString 

     'Get instance color from item name 
     ItemColor = Color.FromName(ItemName) 

     'Get instance brush with Solid style to draw background 
     Using ItemBrush = New SolidBrush(ItemColor) 

      'Draw the item name 
      g.DrawString(ItemName, DirectCast(sender, ComboBox).Font, Brushes.Black, rect.X, rect.Top) 

      'Draw the background with my brush style and rectangle of item 
      g.FillRectangle(ItemBrush, rect.X, rect.Y, rect.Width, rect.Height) 

     End Using 

    End If 

End Sub 

를 처리 :

Dim ActualColor As Color 
    Dim ColorToCheck As KnownColor 


    For Each ColorToCheck In [Enum].GetValues(GetType(KnownColor)) 
     ActualColor = Color.FromKnownColor(ColorToCheck) 
     If Not ActualColor.IsSystemColor And HDI.Color = ActualColor.ToArgb Then 
      cboColor.Text = ActualColor.Name 
      Exit For 
     End If 
    Next