Grey out text in a listbox - c#

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;

Related

Custom listbox wont display string completly c#

Hi guys I am trying to rebrush individual items in a listbox, I went with listbox draw item event, somehow it works but when I add items to my listbox, only string "b" is added and never the + "hi" part, I could write whatever I want or add whatever I want, only string b will be printed.
Not using draw mode and sticking with normal listbox obviously fixes the issue, so I guess it must be related to draw item event, but I cant figue out what exactly does this, I appreciate any help
listBox3.DrawMode = DrawMode.OwnerDrawFixed;
private async void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
foreach (string b in liIDs)
{
listBox3.Invoke((MethodInvoker)delegate
{
listBox3.Items.Add(b + "hi");
listBox3.Update();
});
}
private void listBox3_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
e.DrawBackground();
Brush myBrush = Brushes.White;
myBrush = Brushes.Red;
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
catch
{
}
}
I could only reproduce using items with values width larger than the list box width.
If thats the case you need to set the HorizontalScrollbar propertie to True and HorizontalExtent value

No row indentation for items in ListView

I'm currently working on some software and the customer is dead-set on a specific UI change that has been giving me some trouble:
I would like items in a ListView to not have any indentation. To illustrate I changed the backcolor of the ListView and the items.
Now, the little piece to the left of each item should also be white. Any ideas?
You can set the OwnerDraw property of ListView to true and using DrawItem, DrawColumnHeader and DrawSubItem draw the list view yourself.
For example:
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.Graphics.DrawString(e.Item.Text, e.Item.Font, Brushes.Black,
new Rectangle(e.Bounds.Left-2, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height));
}
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
if(e.ColumnIndex>0)
e.DrawDefault = true;
}
Note:
The above rendering is just an example and shows you how you can draw item text in desired location for example using using Graphics.DrawString.
Also it shows you how to set e.DrawDefault = true; to perform default rendering.
The first subitem of each ListViewItem object represents the parent item itself
To avoid issues with graphics flickering when owner drawing, override the ListView control and set the DoubleBuffered property to true.
To learn more, read documents of OwnerDraw property of ListView control.

Apply different properties on specific items in listbox

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.

How to make a non editable drop down combobox?

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;

Draw Simple Circle in List box in C#.Net Desktop Application

i want to draw simple circle of different category size of circle come form database and different size in c#.net for desktop for winforms ..
as you saw me how to draw circle using listbox now i want to fill color in List box each circle using another List box when color is defined ....it is multiple color also we can apply .. please help me in this..
Set DrawMode property and handle DrawItem event of ListBox,
private void Form1_Load(object sender, EventArgs e)
{
listBox1.DrawMode = DrawMode.OwnerDrawVariable;
listBox1.Items.Add("One");
listBox1.Items.Add("Two");
listBox1.Items.Add("Three");
listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
}
void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox l=sender as ListBox;
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawEllipse(Pens.Blue, new Rectangle(1, 1+e.Index * 15, 100, 10));
e.Graphics.DrawString(l.Items[e.Index].ToString(), new Font(FontFamily.GenericSansSerif,9, FontStyle.Regular), Brushes.Red , e.Bounds);
}

Categories