xplosiv_1

Hi,

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



Re: Visual Basic Language Colour Combo Box

Dave299

Why not use a ColorDialog



Re: Visual Basic Language Colour Combo Box

xplosiv_1

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






Re: Visual Basic Language Colour Combo Box

Dave299

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





Re: Visual Basic Language Colour Combo Box

DMan1

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 Sub






Re: Visual Basic Language Colour Combo Box

xplosiv_1

HI,

Is there a colour box which displays the list you get in visual sudio when you type

color. Aqua blue
Brick red
etc



Thanks,

ANdy





Re: Visual Basic Language Colour Combo Box

Dave299

Not 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





Re: Visual Basic Language Colour Combo Box

nogChoco

^^^ Dave299 beat me to it, but I'll post my code anyway:




Re: Visual Basic Language Colour Combo Box

Dave299

Very nice - I'd been trying to think of an elegant way to make sure the text would always be readable.



Re: Visual Basic Language Colour Combo Box

nogChoco

Dave299 wrote:
Very nice - I'd been trying to think of an elegant way to make sure the text would always be readable.

Thanks, Dave - I gradients, it's a cheap effect but they never go out of style




Re: Visual Basic Language Colour Combo Box

xplosiv_1

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






Re: Visual Basic Language Colour Combo Box

nogChoco

Thanks - it's pretty much the same as Dave's code, though

The xxColorCombobox class that I created (under the Form1 Class code) can be used like a normal combobox. If you're using VB2005, then build or run the solution once, and then you should see 'xxColorCombobox' in the toolbox like the other controls (it will be in a section that is named after your project).

Or you can create it via code, like you would with a normal combobox. xxColorCombobox has a .SelectedColor property that gives you the color that is currently selected in the ColorCombobox:

Private aNewCombo as New xxColorCombobox
Private aColor as Color = aNewCombo.SelectedColor

And you can use the .ColorsToShow property to limit the colors to SystemColors or NamedWebColors only (or All of them). The SelectedIndexChanged event can be used to keep your color-variable up to date with the color you selected in the combobox:

Private Sub aNewCombo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles aNewCombo.SelectedIndexChanged

        Dim zColorCombobox As xxColorCombobox = sender

        aColor = zColorCombobox.SelectedColor

End Sub



Re: Visual Basic Language Colour Combo Box

Tall Dude

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!






Re: Visual Basic Language Colour Combo Box

nogChoco

Thanks, Tall Dude - note that there seems to be a little problem with it at the moment, though - I think the ArrayList is causing the list to double (so all entries appear twice), but haven't gotten to the bottom of it yet - i'll edit the fixed code into my earlier reply, as soon as I figure out where the doubling is happening.

EDIT: The list only doubles when the control is added at designtime, so I'm not sure I'll be able to figure it out




Re: Visual Basic Language Colour Combo Box

Tall Dude

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 ....