listbox - focus

Started by
2 comments, last by NenNarsters 17 years, 6 months ago
When you click on an item in a listbox it is highlighted in blue, but when you click on something else and the LB loses focus the highlight disappears. how can i keep the selected item highlighted even when the LB isn't in focus? Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
You'll need to provide more information.

What OS are you using?

What programming language are you using?

What GUI library are you using?
WinForms with C#
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
If you want to keep the item highlighted when the ListBox loses focus you are going to have to create your own control that inherits from the ListBox control and override the OnDrawItem. Below is the OnDrawItem function written in C# 2.0. I posted some code in a zip file on my website at www.ruysfamily.com/stuff/MultiLineListbox.zip. The control also includes code to make ListBox items span multiple lines.

protected override void OnDrawItem(DrawItemEventArgs e)
{
if (Items.Count > 0)
{
//check if listbox has any items
if (e.Index > -1)
{
string s = Items[e.Index].ToString();

//Normal items
if (e.State.ToString() == "NoAccelerator, NoFocusRect")
{
e.Graphics.FillRectangle(new SolidBrush(SystemColors.Window), e.Bounds);
e.Graphics.DrawString(s, Font, new SolidBrush(SystemColors.WindowText),
e.Bounds);
}
else //Selected item, needs highlighting
{
e.Graphics.FillRectangle(new SolidBrush(SystemColors.Highlight),
e.Bounds);
e.Graphics.DrawString(s, Font, new SolidBrush
(SystemColors.HighlightText), e.Bounds);
}
}
}
}


-Nen Narsters
www.ruysfamily.com

This topic is closed to new replies.

Advertisement