I'm trying to change the color of some ListBox items. It's not so difficult because you need only two cut&paste this code from MSDN this code snippet and adapt it to your needs:
private void ListBox1_DrawItem(object sender,
System.Windows.Forms.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.
switch (e.Index)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}
// Draw the current item text based on the current Font
// and the custom brush settings.
e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
But there is a problem with ScrollBars . Neither Horizontal Scroll bar nor Vertical are shown. How is the problem? thanks | | RoboBoot Tuesday, September 08, 2009 7:37 AM | Hi RoboBoot,
Thanks for your reply which helps me get the key issue: The vertical scroll bar works file but the horizontal scroll bar does not work correctly. Please feel free to tell me if I misunderstand you.
To have the horizontal scroll bar work fine, we need to set the HorizontalExtent property of the ListBox to the maximum width of the items. This is a code snippet:
private ListBox ListBox1 = new ListBox();
private void InitializeListBox()
{
//Initialize the ListBox.
ListBox1.IntegralHeight = true;
ListBox1.Items.AddRange(new Object[] { "Welcome to the new world , everyone.", "Orange Item", "Purple Item"});
ListBox1.Location = new System.Drawing.Point(81, 69);
ListBox1.Size = new System.Drawing.Size(120, 95);
ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
ListBox1.ScrollAlwaysVisible = true;
ListBox1.HorizontalScrollbar = true;
ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);
Controls.Add(ListBox1);
//Show the vertical scroll bar.
ListBox1.ScrollAlwaysVisible = true;
//Show the horizontal scroll bar.
ListBox1.HorizontalScrollbar = true;
//maxWidth of the drawn string.
int width = 0;
//Create a graphics.
Graphics g = ListBox1.CreateGraphics();
//Measure the string of each item and select the max width.
foreach (object item in ListBox1.Items)
{
string text = item.ToString();
SizeF s = g.MeasureString(text, ListBox1.Font);
if (s.Width > width)
width = (int)s.Width;
}
//Set the horizontal extent to the width plus the offset.
ListBox1.HorizontalExtent = width + 2;
}
private void ListBox1_DrawItem(object sender,
System.Windows.Forms.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.
switch (e.Index)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}
// Draw the current item text based on the current Font
// and the custom brush settings.
e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
We can get more information about HorizontalExtent from: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.horizontalextent(VS.71).aspx.
Let me know if this does not help. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byRoboBoot Tuesday, September 15, 2009 7:01 AM
-
| | Aland Li Tuesday, September 15, 2009 3:31 AM | Hi RoboBoot, It is because the scroll bars are impliedly hidden and would be shown if the content beyond the client size of the ListBox. For example, if you add lots of items to the ListBox, the vertical scroll bar would be shown. If we want the scroll bars always be shown, we can set the ScrollAlwaysVisible and HorizontalScrollbar properties to true. This is a code snippet:
//Show the vertical scroll bar.
ListBox1.ScrollAlwaysVisible = true;
//Show the horizontal scroll bar.
ListBox1.HorizontalScrollbar = true;
Let me know if this does not help. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. | | Aland Li Wednesday, September 09, 2009 9:44 AM | I have already done this test and It didn't work. The scrollbar placeholder is shown but the scrollbar no. Why this? | | RoboBoot Wednesday, September 09, 2009 11:12 AM | Hi RoboBoot,
Do you mean the scroll bar is shown, but a square which we press and drag to scroll the ListView is not shown? Based on my understanding, it is because the client rectangle can contain all the items. If the scroll square is shown, we still cannot scroll.
Regards, Aland Li Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. | | Aland Li Thursday, September 10, 2009 2:11 AM | The scroll bar is shown but the square no. The strange thing is that a lot of items' hotizontal size is larger than the client rectangle. In other words: the space for the scroll bars is there, but the square isn't present. Do I have to setup some size in some place? | | RoboBoot Monday, September 14, 2009 2:00 PM | Hi RoboBoot,
Thanks for your reply which helps me get the key issue: The vertical scroll bar works file but the horizontal scroll bar does not work correctly. Please feel free to tell me if I misunderstand you.
To have the horizontal scroll bar work fine, we need to set the HorizontalExtent property of the ListBox to the maximum width of the items. This is a code snippet:
private ListBox ListBox1 = new ListBox();
private void InitializeListBox()
{
//Initialize the ListBox.
ListBox1.IntegralHeight = true;
ListBox1.Items.AddRange(new Object[] { "Welcome to the new world , everyone.", "Orange Item", "Purple Item"});
ListBox1.Location = new System.Drawing.Point(81, 69);
ListBox1.Size = new System.Drawing.Size(120, 95);
ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
ListBox1.ScrollAlwaysVisible = true;
ListBox1.HorizontalScrollbar = true;
ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);
Controls.Add(ListBox1);
//Show the vertical scroll bar.
ListBox1.ScrollAlwaysVisible = true;
//Show the horizontal scroll bar.
ListBox1.HorizontalScrollbar = true;
//maxWidth of the drawn string.
int width = 0;
//Create a graphics.
Graphics g = ListBox1.CreateGraphics();
//Measure the string of each item and select the max width.
foreach (object item in ListBox1.Items)
{
string text = item.ToString();
SizeF s = g.MeasureString(text, ListBox1.Font);
if (s.Width > width)
width = (int)s.Width;
}
//Set the horizontal extent to the width plus the offset.
ListBox1.HorizontalExtent = width + 2;
}
private void ListBox1_DrawItem(object sender,
System.Windows.Forms.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.
switch (e.Index)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}
// Draw the current item text based on the current Font
// and the custom brush settings.
e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
We can get more information about HorizontalExtent from: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.horizontalextent(VS.71).aspx.
Let me know if this does not help. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byRoboBoot Tuesday, September 15, 2009 7:01 AM
-
| | Aland Li Tuesday, September 15, 2009 3:31 AM |
|