问题描述
大家好,
我遇到了一个问题,我想根据我在ColorDialog中的选择更改组合框项目的背景颜色和文本颜色.
例如.当我从ColorDialog中选择红色时,组合框项目的背景颜色为红色,并且新项目将添加到组合框,其值为Red color的RGB值.
将颜色更改为蓝色后,组合框项目的背景颜色为蓝色,新项目将添加到组合框,其值为蓝色的RGB值.所以下一次打开列表时,我可以看到选择了哪种颜色.
以下是代码.我可以更改文本颜色,但无法保存.当您选择另一种颜色时.然后,一切又变了.你可以帮帮我吗?谢谢.
Hello everyone,
I encourter a problem.I want to change the combobox items'' background color and its text color according to my choice from the ColorDialog.
Eg. When I choose red from ColorDialog, the combobox items'' background color is red and a new items will add to the combobox, its value is the RGB value of Red color.
After I change the color to blue, the combobox items''background color is blue and a new item will add to the combobox, its value is the RGB value of blue color. So next time I could see which color I have chosen when I open the list.
The following is the code. I could change the text color but could not save it. When you choose another color. Then it all changed again. Could you help me? Thanks.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dlgChooser.ShowDialog()
Dim colorR As String = dlgChooser.Color.R.ToString()
Dim colorG As String = dlgChooser.Color.G.ToString()
Dim colorB As String = dlgChooser.Color.B.ToString()
TextBox1.ForeColor = dlgChooser.Color()
cmbRecent.Items.Insert(0, colorR & "," & colorG & "," & colorB)
cmbRecent.BackColor = dlgChooser.Color()
cmbRecent.SelectedIndex = 0
End Sub
Private Sub cmbRecent_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cmbRecent.DrawItem
If e.Index < 0 Then Exit Sub
Dim rect As Rectangle = e.Bounds
If e.State And DrawItemState.Selected Then
e.Graphics.FillRectangle(SystemBrushes.Highlight, rect)
Else : e.Graphics.FillRectangle(SystemBrushes.Window, rect)
End If
Dim b As New SolidBrush(Color.FromName(colorname))
e.Graphics.FillRectangle(b, rect)
e.Graphics.DrawRectangle(Pens.Black, rect)
Dim b2 As SolidBrush
Dim fillcolor As Color = Color.FromArgb(CInt(dlgChooser.Color.R), CInt(dlgChooser.Color.G), CInt(dlgChooser.Color.B))
b2 = New SolidBrush(fillcolor)
e.Graphics.DrawString(sender.items(e.Index), e.Font, b2, e.Bounds.X, e.Bounds.Y)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cmbRecent.DrawMode = DrawMode.OwnerDrawVariable
End Sub
End Class
推荐答案
<br />
m_Color = Color.FromName((string)base.Items[e.Index]);<br />
g.FillRectangle(new SolidBrush(m_Color), e.Bounds.X + this.inMargin, e.Bounds.Y + this.inMargin, e.Bounds.Width / this.boxWidth - 2 * this.inMargin, e.Bounds.Height - 2 * this.inMargin);<br />
<br />
g.DrawRectangle(Pens.Black, e.Bounds.X + this.inMargin, e.Bounds.Y + this.inMargin, e.Bounds.Width / this.boxWidth - 2 * this.inMargin, e.Bounds.Height - 2 * this.inMargin);<br />
让我知道的问题.
谢谢,
巴克斯(Bhasker)
Let me know in questions.
Thanks,
Bhasker
这篇关于如何更改单个组合框项目的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!