How do i get a combo box to display system colours
As i am using it so the user can select the background colour for a graph.
I have tried colourcb.text = color() but this produces an error any ideas
Andy
Sorry
i am new to VB so don't know all the default dialog boxes available.
Is there a list with all the dialogue boxes some where
How would i use the colour dialogue box just place a button
the type = colour dialogue
Andy
Just enter dialogs into the Help search and you should find them all.
Basic use as follows:
Dim CD As New ColorDialog
Dim SelectedColor As Color
If CD.ShowDialog() = Windows.Forms.DialogResult.OK Then
SelectedColor = CD.Color
End If
Change the background color of the form to the color the user selected in the color dialog..
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cd As New ColorDialog With cd.
AllowFullOpen = True.
Color = Me.BackColor.
FullOpen = True.
ShowDialog() End With Me.BackColor = cd.Color End SubNot that I'm aware of but try this:
Public Class Form1
Dim Colours() As KnownColor
Dim F As New Font("Arial", 12, FontStyle.Bold)
Dim WithEvents CMBO As New ComboBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CMBO.SetBounds(50, 50, 200, 20)
CMBO.DrawMode = DrawMode.OwnerDrawFixed
CMBO.Font = F
Me.Controls.Add(CMBO)
Colours = DirectCast([Enum].GetValues(GetType(KnownColor)), KnownColor())
For Each C As KnownColor In Colours
CMBO.Items.Add([Enum].GetName(GetType(KnownColor), CInt(C)))
Next
End Sub
Private Sub CMBO_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles CMBO.DrawItem
e.DrawBackground()
Dim C As Color = Color.FromName(CMBO.Items(e.Index).ToString)
Dim B As New SolidBrush(C)
e.Graphics.FillRectangle(B, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.Graphics.DrawString(CMBO.Items(e.Index).ToString, F, Brushes.Black, e.Bounds.X, e.Bounds.Y)
End Sub
Private Sub CMBO_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CMBO.SelectedIndexChanged
Me.Text = CMBO.SelectedItem.ToString
End Sub
End Class
Dave299 wrote:
Very nice - I'd been trying to think of an elegant way to make sure the text would always be readable.
nogChoco,
As i am rubbish at vb (new and all)
How would i make your code eqal my colourcbo
As in: i created your project and it displays three combo box's all i need is to produce the result you get in the combobox at the bottom (that displays aliceblue) in my
colourcbo (
and be able to make selected colour variable equal to the selsect colour.
By the way, very nice code
Many Thanks,
Andy
nogChoco,
Took me a few minutes to figure out how to 'dll build' it
to add to my toolbox. (versus just using it as a class.)
But man, that code rocks!
Make this change to fix the code:
Private Sub Update_ColorList() Try Me.Items.Clear() Dim zArrayList As New ArrayList(40)zArrayList.Clear()
The Sub New() and the ColorsToShow() are
both calling Me.Update_ColorList when the
control is initialized and because zArrayList
already exists, the code is not waiting for another
new one to get initialized.
Edited comment:
Well it fixed it in one test. Now I don't know ....