Hide text in comboBox - c#

I've got a WinForms comboBox with the datasouce set to a list of a custom class. I'm displaying these items as colors (based on a property in the class) and would like to only display the color (i.e. no text). I'm displaying the items as colors in the dropdown through the DrawItem event, but this doesn't work for the comboBox itself (the part other than the dropdown). I've tried changing the ForeGround Color to Transparent, but that didn't work either. What I'd really like is a comboBox.DisplayMember = "None"; or something similar.
What is the best way to accomplish this?
Edit: So after a bit of fiddling around, I've found one solution: adding a "None" property to the class like this:
public string None
{
get
{
return "";
}
}
then I can just do the comboBox.DisplayMember = "None"; like I mentioned before. But I think the question still stands: is there a better way?

You can make a ComboBox control as a color picker to display and select colors by using DrawItem event and also there is a property called DrawMode for the ComboBox control which determines whether the Operating System or the code will handle the drawing of the items in the list. This property must be set to OwnerDrawFixed using the Properties window in order for the DrawItem event implementation to be called.
private void ColorComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rect = e.Bounds;
if (e.Index >= 0)
{
Color c = Color.FromName(n);
Brush b = new SolidBrush(c);
g.DrawString(n, f, Brushes.Black, rect.X, rect.Top);
g.FillRectangle(b, rect.X, rect.Y + 5, rect.Width -10, rect.Height - 10);
}
}
You can read more about CodeProject: Color Picker Combo Box

As my "pseudo-solution" seems to be the best solution here, I'll just copy it down here:
Since the items in my comboBox are of a custom class, I added another property to that class:
public string None
{
get
{
return "";
}
}
and I set the comboBox.DisplayMember = "None";. This achieves the result I was looking for

Related

change color item listbox c#

I have created a ListBox to which I add elements during code compilation. and I want to record its color when adding one element (so that each added element has a different color)
listBox1.Items.Add(string.Format("Місце {0} | В роботі з {1} | ({2} хв)", temp[7].Substring(6, 4), temp[8].Substring(11, 5), rezult)); `
I tried everywhere possible to embed this change
BackColor = Color.Yellow;ForeColor = Color.Yellow;
I am working with listbox because I have seen so many answers about ListView.
Set the listbox DrawMode to either OwnerDrawFixed or OwnerDrawVariable and set this as the DrawItem event handler:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e){
if(e.Index == 1) e.DrawBackground(); //use e.Index to see if we want to highlight the item
else e.Graphics.FillRectangle(new SolidBrush(Color.Yellow), e.Bounds); //This does the same thing as e.DrawBackground but with a custom color
e.DrawFocusRectangle();
if(e.Index < 0) return;
TextRenderer.DrawText(e.Graphics, (string)listBox1.Items[e.Index], listBox1.Font, e.Bounds, listBox1.ForeColor, TextFormatFlags.Left);
}
Well, best idea I have is to not use list box, but flowLayoutPanel and add usercontrols where you will have labels.
flowLayoutPanel works as list of controls which you can scroll, so we will just create a usercontrol, where we will put label and change the usercontrol background
Don't forget to turn on the AutoScroll feature to flowLayoutPanel, otherwise the scroll bar wont work and wont even show up.
If you want to be able to be clickable just add to the label click event.
public void CreateItem(Color OurColor, string TextToShow)
{
Label OurText = new Label()
{
Text = "TextToShow",
Font = new Font("Segoe UI", 8f),
Location = new Point(0, 0),
AutoSize = true,
};
UserControl OurUserControl = new UserControl();
OurUserControl.Size = new Size((int)((double)flowLayoutPanel1.Width * 0.9) , OurText.Font.Height);
OurUserControl.BackColor = OurColor;
OurUserControl.Controls.Add(OurText);
flowLayoutPanel1.Controls.Add(OurUserControl);
}

WinForms: Changing ForeColor of Selected item in ListView

I am setting the ForeColor of all items in my ListView to a different color, but this get's overrided when the item is selected (changes to Black again; changes back to custom color on deselection).
I want my items to retain my custom color, even in selection.
I'm basically asking the same question that was asked here 7 years ago, and doesn't seem to have any satisfactory answer.
I tried searching in SO and elsewhere, and no luck. The only solution provided so far is to draw the whole thing (the DrawItem method), which I gave a try but is ridiculously complicated for such a petty requirement...
Is this the only way? Say it ain't so.
Enable your ListView OwnerDraw mode, then subscribe its DrawItem and DrawColumnHeader events.
If your design requires it, also subcribe the DrawSubitem event.
At this point, you can draw anything in the related areas of your ListView.
In the example, I've painted a little symbol in the Header area.
The Header text needs to be painted too.
If the Background color doesn't change (same as in design mode), you just need to use the DrawListViewItemEventArgs e parameter function e.DrawBackground();
If not, use e.Graphics.FillRectangle() to color the Item area, defined by e.Bounds.
The Item Text is drawn using e.Graphics.DrawString().
The item Text is e.Item.Text, the text area is defined by e.Bounds again.
If you don't need any specific details/settings for the item's text, you can simply use e.DrawText();, which uses the default properties (defined at design-time).
Here, the item color complex logic is that the color is specified inside the item text. Could be anything else. The item tag, its Index position, a List<Parameters>, you name it.
This is how it might look like:
(I added e.Graphics.TextRenderingHint = [] to show how you can control the quality of the rendered text. e.Graphics.TextContrast can be also used to enhance the contrast).
Note: this code sample only draws a generic image, if the ListView has an ImageList. You should also verify whether the SmallIcon/LargeIcon ImageLists are defined and draw the related Image in the specified size. It's the same procedure, though.
protected void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.Item.UseItemStyleForSubItems = true;
int imageOffset = 0;
Rectangle rect = e.Item.Bounds;
bool drawImage = !(e.Item.ImageList is null);
Color itemColor = Color.FromName(e.Item.Text.Substring(e.Item.Text.LastIndexOf(" ") + 1));
using (var format = new StringFormat(StringFormatFlags.FitBlackBox)) {
format.LineAlignment = StringAlignment.Center;
if (drawImage) {
imageOffset = e.Item.ImageList.ImageSize.Width + 1;
rect.Location = new Point(e.Bounds.X + imageOffset, e.Item.Bounds.Y);
rect.Size = new Size(e.Bounds.Width - imageOffset, e.Item.Bounds.Height);
e.Graphics.DrawImage(e.Item.ImageList.Images[e.Item.ImageIndex], e.Bounds.Location);
}
if (e.Item.Selected) {
using (var bkgrBrush = new SolidBrush(itemColor))
using (var foreBrush = new SolidBrush(e.Item.BackColor)) {
e.Graphics.FillRectangle(bkgrBrush, rect);
e.Graphics.DrawString(e.Item.Text, e.Item.Font, foreBrush, rect, format);
}
e.DrawFocusRectangle();
}
else {
//e.DrawDefault = true;
using (var foreBrush = new SolidBrush(itemColor)) {
e.Graphics.DrawString(e.Item.Text, e.Item.Font, foreBrush, rect, format);
}
}
}
}
// Draws small symbol in the Header beside the normal Text
protected void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawBackground();
string extra = (e.ColumnIndex == 1) ? (char)32 + "\u2660" + (char)32 : (char)32 + "\u2663" + (char)32;
using (var brush = new SolidBrush(e.ForeColor)) {
e.Graphics.DrawString(extra + e.Header.Text, e.Font, brush, e.Bounds, StringFormat.GenericTypographic);
}
}

How to change the BackColor of a ComboBox when DropdownStyle is DropDownList?

I'm trying to change the dispaly color of a ComboBox when the DropdownStyle property is DropdownList. When the property is changed to Dropdown from DropdownList the color changes.
How can I control the view color of the dropdown boxes ?
Thanks
You can set FlatStyle property to Popup. This way the back color will use in both DropDown and DropDownList mode.
If you don't like flat style or you need more customization on rendering of ComboBox, you can use an owner-drawn ComboBox. For example you can set DrawMode property to OwnerDrawFixed and handle DrawItem event and draw the combo box based on your logic.
You may also be interested in the following posts to customize ComboBox:
Flat ComboBox - Change border color and Dropdown button color
I have been using stack overflow for a couple of years without subscribing or contributing. It's my first choice whe looking for a solution because it generally supplies a solution and I can read it without having to zoom. At 81 years of age, I am fossilized, but "It’s kind of fun to be extinct."
Thanks, Ogden Nash.
When background shading is applied to text, the reduced contrast makes it difficult for my old eyes to read it. I Googled the problem, and the offered solutions scared me off.
I even considered cobbling up the functionality using graphics, but I needed several instances. Gotta be a way.
Cover the text part of the combobox with a textbox, and change the textbox to multiline to make its height match the combobox. Add a couple of event handlers and Bob's your uncle.
Private Sub cmbPoints_SelectedIndexChanged(sender As Object, e As EventArgs
)HandlescmbPoints.SelectedIndexChanged
' Make the selection visible in the textbox
txtPoints.Text = cmbPoints.Text
End Sub
Private Sub txtPoints_GotFocus(sender As Object, e As EventArgs
) Handles txtPoints.GotFocus
' Prevent the user changing the text.
cmbPoints.Focus()
End Sub
Just like mentioned above; You can set FlatStyle property to Popup/Flat. This way the back color will use in both DropDown and DropDownList mode.
But then you wont have the look you expected.
There's a trick i do where i create a panel and change its border property to FixedSingle. Change the color of the panel to as desired and then change its size property to match the size of your ComboBox. Eg to 80, 22.
On the position where you had your ComboBox, place your panel.
Place your combobox on the Panel.
If you can fine tune its position, When you debug, you will find that your ComboBox looks like it has a border.
I created my own Usercontrol. You have to set the dropdown to Flatstyle=Flat and change the Backcolor=White. Then the code below will draw the border which is missing. Below is code and a pic of what it looks like. You can copy and paste this into your own namespace somewhere and name it what you like.
Note: You will need to add System.Windows.Forms; System.ComponentModel; And System.Drawing; to your Class.
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
public class KDCombo : ComboBox
{
public KDCombo()
{
BorderColor = Color.DimGray;
}
[Browsable(true)]
[Category("Appearance")]
[DefaultValue(typeof(Color), "DimGray")]
public Color BorderColor { get; set; }
private const int WM_PAINT = 0xF;
private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
using (var g = Graphics.FromHwnd(Handle))
{
// Uncomment this if you don't want the "highlight border".
/*
using (var p = new Pen(this.BorderColor, 1))
{
g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
}*/
using (var p = new Pen(this.BorderColor, 2))
{
g.DrawRectangle(p, 0, 0, Width , Height );
}
}
}
}
}

How can I include icons in my ListBox?

I know that similar questions have already been asked here before, but they all lead to the same codeproject article that doesn't work. Does anybody know of a working ListBox with icons?
Will a ListView work for you? That is what I use. Much easier and you can make it look just like a ListBox. Also, plenty of documentation on MSDN to get started with.
How to: Display Icons for the Windows Forms ListView Control
The Windows Forms ListView control can display icons from three image
lists. The List, Details, and SmallIcon views display images from the
image list specified in the SmallImageList property. The LargeIcon
view displays images from the image list specified in the
LargeImageList property. A list view can also display an additional
set of icons, set in the StateImageList property, next to the large or
small icons. For more information about image lists, see ImageList
Component (Windows Forms) and How to: Add or Remove Images with the
Windows Forms ImageList Component.
Inserted from How to: Display Icons for the Windows Forms ListView Control
If you don't want to change ListBox to a ListView you can write a handler for DrawItemEvent. for example:
private void InitializeComponent()
{
...
this.listBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox_DrawItem);
...
}
private void listBox_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index == -1)
return;
// Draw the background of the ListBox control for each item.
e.DrawBackground();
var rect = new Rectangle(e.Bounds.X+10, e.Bounds.Y+8, 12, 14);
//assuming the icon is already added to project resources
e.Graphics.DrawIconUnstretched(YourProject.Properties.Resources.YouIcon, rect);
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
e.Font, Brushes.Black, new Rectangle(e.Bounds.X + 25, e.Bounds.Y + 10, e.Bounds.Width, e.Bounds.Height), StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
you can play around with the rectangle to set the location of the icon right
If you're stuck working in WinForms, then you'll have to owner-draw your items.
See the example for the DrawItem event.
A little different approach - don't use a list box.
Instead of using that control that bounds me to its limited set of properties and methods I am making a listbox of my own.
It's not as hard as it sounds:
int yPos = 0;
Panel myListBox = new Panel();
foreach (Object object in YourObjectList)
{
Panel line = new Panel();
line.Location = new Point(0, Ypos);
line.Size = new Size(myListBox.Width, 20);
line.MouseClick += new MouseEventHandler(line_MouseClick);
myListBox.Controls.Add(line);
// Add and arrange the controls you want in the line
yPos += line.Height;
}
Example for myListBox event handlers - selecting a line:
private void line_MouseClick(object sender, MouseEventArgs)
{
foreach (Control control in myListBox.Controls)
if (control is Panel)
if (control == sender)
control.BackColor = Color.DarkBlue;
else
control.BackColor = Color.Transparent;
}
The code samples above were not tested but the described method was used and found very convenient and simple.

How to change the ForeColor of individual items in a ComboBox? (C# Winforms)

I know I can change the ForeColor of the ComboBox like this:
comboBox1.ForeColor = Color.Red;
But that makes all the items that color. When you drop down the ComboBox every single item is then red.
I want to individually color items so that the first item is always black, the second always red, the third always blue, et cetera. Is this possible?
Also, I don't think I can create a UserControl for this because the ComboBox I am using is the one for Toolstrips.
You can use DrawItem event.
This event is used by an owner-drawn ComboBox. You can use this event
to perform the tasks needed to draw items in the ComboBox. If you have
a variable sized item (when the DrawMode property set to
DrawMode.OwnerDrawVariable), before drawing an item, the MeasureItem
event is raised. You can create an event handler for the MeasureItem
event to specify the size for the item that you are going to draw in
your event handler for the DrawItem event.
MSDN Example:
// You must handle the DrawItem event for owner-drawn combo boxes.
// This event handler changes the color, size and font of an
// item based on its position in the array.
protected void ComboBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
float size = 0;
System.Drawing.Font myFont;
FontFamily family = null;
System.Drawing.Color animalColor = new System.Drawing.Color();
switch(e.Index)
{
case 0:
size = 30;
animalColor = System.Drawing.Color.Gray;
family = FontFamily.GenericSansSerif;
break;
case 1:
size = 10;
animalColor = System.Drawing.Color.LawnGreen;
family = FontFamily.GenericMonospace;
break;
case 2:
size = 15;
animalColor = System.Drawing.Color.Tan;
family = FontFamily.GenericSansSerif;
break;
}
// Draw the background of the item.
e.DrawBackground();
// Create a square filled with the animals color. Vary the size
// of the rectangle based on the length of the animals name.
Rectangle rectangle = new Rectangle(2, e.Bounds.Top+2,
e.Bounds.Height, e.Bounds.Height-4);
e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle);
// Draw each string in the array, using a different size, color,
// and font for each item.
myFont = new Font(family, size, FontStyle.Bold);
e.Graphics.DrawString(animals[e.Index], myFont, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X+rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
// Draw the focus rectangle if the mouse hovers over an item.
e.DrawFocusRectangle();
}
EDIT :
Just found a similar thread.
For a ToolStripComboBox derive from ToolStripControlHost.
//Declare a class that inherits from ToolStripControlHost.
public class ToolStripCustomCombo : ToolStripControlHost
{
// Call the base constructor passing in a MonthCalendar instance.
public ToolStripCustomCombo() : base(new ComboBox()) { }
public ComboBox ComboBox
{
get
{
return Control as ComboBox;
}
}
}
Then say you have a toolstrip named m_tsMain. Here's how to add the new control.
ToolStripCustomCombo customCombo = new ToolStripCustomCombo();
ComboBox c = customCombo.ComboBox;
c.Items.Add("Hello World!!!");
c.Items.Add("Goodbye cruel world!!!");
m_tsMain.Items.Add(customCombo);
And you should be able to add an event handler to c for DrawItem.

Categories