Sign in to follow this  

listbox - focus

This topic is 4068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
You'll need to provide more information.

What OS are you using?

What programming language are you using?

What GUI library are you using?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this