I am having the combobox of dropdownstyle - dropdownlist it is noneditable but in windows 7, its back color cannot be changed.so i want a non editable drop down down combobox .
There is two way to set the backcolor
First way
set the combobox property
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
then implement DrawItem event handler (sample code below)
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index > -1)
{
e.DrawBackground();
e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), this.Font, Brushes.Black, e.Bounds);
e.DrawFocusRectangle();
}
}
Second way
set the combobox property
comboBox1.FlatStyle = FlatStyle.Flat;
Then use the backcolor you needed for the combobox
stay on your comboBox and search the dropstyle property from properties window and then choose dropdown list;
Related
Is there a toolbox object that is like a listbox but each individual item can have a different font colour? I'm trying to make a mod manager of sorts and want to grey out the text for those mods that are uninstalled.
Try this:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.Graphics.DrawString(Convert.ToString(listBox1.Items[e.Index]), new Font("Aerial", 8), Brushes.Gray, new Point(0, 0));
e.DrawFocusRectangle();
}
For this to work, change the DrawMode of the Listbox to OwnerDrawFixed.
Also, if you want to select an item in a combobox, try something like this:
comboBox1.SelectedIndex = 0;
I have a property form where the background color of label text should be changed when the data in the control has been modified.
For TextBox and CheckBox controls I have managed to achieve this, but for ComboBox I cannot find a way to modify the background color of the label text.
The BackColor property only modifies the background color of the items inside the ComboBox and I don't want to change the color inside the box.
Try using the draw item event and brushes...
Example below:
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Brush brush = null;
ComboBox combo = (ComboBox) sender;
e.DrawBackground();
e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
e.Graphics.FillRectangle(Brushes.Blue, e.Bounds); //blue background
e.Graphics.DrawString("your string here", combo.Font, Brushes.Red, e.Bounds.X, e.Bounds.Y); //red font
}
I fill a listbox listBoxHome from a dictionary dictionaryHome :
dictionaryHome.Add(item.Id, item.Name);
listBoxHome.DataSource = new BindingSource(dictionaryHome, null);
listBoxHome.DisplayMember = "Value";
listBoxHome.ValueMember = "Key";
I also use the following code to be able only first 5 items to be selectable
private void listBoxHome_SelectedIndexChanged(object sender, EventArgs e)
{
InvertMySelection(listBoxHome, listBoxAway);
//make only first5 selectable
for (int i = 5; i < listBoxHome.Items.Count; i++)
{
if (listBoxHome.GetSelected(i) == true)
listBoxHome.ClearSelected();
}
}
I want to apply a different color to the first 5 items and different color the other items.
Or maybe a transparent panel that shows difference from the first 5 items and the other items. Also I want to draw a line inside the listbox as shown in the image. Any suggestion?
EDIT:
Adding Luc Morin's code the result shown in the following picture
Is there a way to show only the text and not the id(as was before)?The id is used in the back.
First you need to set the DrawMode to OwnerDrawFixed in the Windows Forms Designer property grid.
Then, add an event handler to the ListBox.DrawItem event, something along those lines:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Define the default color of the brush as black.
Brush myBrush = Brushes.Black;
// Determine the color of the brush to draw each item based
// on the index of the item to draw.
if (e.Index < 3)
{
myBrush = Brushes.Red;
}
// Draw the current item text based on the current Font
// and the custom brush settings.
e.Graphics.DrawString(((KeyValuePair<int, string>)listBox1.Items[e.Index]).Value,
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
Adapt to your specific needs.
Code adapted from MSDN sample at: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.drawitem(v=vs.110).aspx
Cheers
EDIT: In order to prevent selection of items, handle the ListBox.SelectedIhanged, something like this:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedIndex >=3)
{
listBox1.SelectedIndex = -1;
listBox1.Invalidate();
}
}
EDIT 2: When binding to a dictionary, the ListBox.Items collection actually contains KeyValuePair objects instead of just strings. I updated the code to account for this. My example assumes the Key is an int and the Value is a string.
Can I change the appearance of a Winforms ComboBox so that a Combobox with DropDownStyle = DropDownList looks more like one that is DropDownStyle = DropDown. The functional difference between them is that the former doesn't allow for user entered values, the problem is that it's default color scheme looks grayed out and doesn't match with textboxes on the same dialog.
you can get DropDown appearance from DropDownList style by changing DrawMode property to DrawMode.OwnerDrawFixed and handling item painting by yourself (thankfully, that's easy). Sample class, implementing this idea:
public class ComboBoxEx : ComboBox
{
public ComboBoxEx()
{
base.DropDownStyle = ComboBoxStyle.DropDownList;
base.DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
if(e.State == DrawItemState.Focus)
e.DrawFocusRectangle();
var index = e.Index;
if(index < 0 || index >= Items.Count) return;
var item = Items[index];
string text = (item == null)?"(null)":item.ToString();
using(var brush = new SolidBrush(e.ForeColor))
{
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
e.Graphics.DrawString(text, e.Font, brush, e.Bounds);
}
}
}
You could try to change the FlatStyle property and see if you get something more to your liking. If you really want it to look like it does with DropDownStyle set to DropDown, you could set the DropDownStyle to DropDown and eat the KeyPress event:
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
Still, I would probably not do this as the appearance of the ComboBox is a visual cue to the user indicating whether they should be able to type in the text area or not.
i want to change the color of a selected items from a list box control how to do that
in windows(Winforms)
As far as I know if you want to do that you need to make the ListBox.DrawMode OwnerDrawFixed and add an event handler on to the DrawItem method.
Something like this might do what you want:
private void lstDrawItem(object sender, DrawItemEventArgs e)
{
ListBox lst = (ListBox)sender;
e.DrawBackground();
e.DrawFocusRectangle();
DrawItemState st = DrawItemState.Selected ^ DrawItemState.Focus;
Color col = ((e.State & st) == st) ? Color.Yellow : lst.BackColor;
e.Graphics.DrawRectangle(new Pen(col), e.Bounds);
e.Graphics.FillRectangle(new SolidBrush(col), e.Bounds);
if (e.Index >= 0)
{
e.Graphics.DrawString(lst.Items[e.Index].ToString(), e.Font, new SolidBrush(lst.ForeColor), e.Bounds, StringFormat.GenericDefault);
}
}
Hope it helps
James
Assuming you're working with WinForms:
Most controls will have a BackColor and BorderColor property. You could add the Color objects to your listbox (the color name should be displayed as Color.ToString() returns the name), then use listbox.SelectedItems[0] to get the color and update the other controls' BackColor etc.