Remove highlights and dotted items in ListBoxes.

MPIon

Well-known member
Joined
Jun 13, 2020
Messages
73
Location
England
Programming Experience
10+
I am porting an application with some ListBoxes from Net Framework to Net 7. I want them to work exactly as they did before, but there is a dotted box at the top of the listbox that was not there in Net Framework and when clicking in the list box.
The listbox is really just a reference list that I don't want to be selectable or highlighted - I could set it to disabled, but then the text is greyed out.

I think this may be something to do with Visual Styles which may have changed at some point. On researching this, I have seen some code along the lines of :-
C#:
<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
   <Setter Property="FocusVisualStyle" Value="{x:Null}"/>

However, I cannot fathom where this should be put - something about creating a ItemContainerStyle, but no documentation I can find explains exactly how you do that.
Must be missing a lot of knowledge here.

I am sure it must be simple, so can anyone shed some light on this please.
 
Were you also using WPF when the code was in .NET Framework? Are you still using WPF in .NET 7? If you are porting f rom WinForm .NET Framework to MAUI or Xamarin Forms for .NET 7, then yes there could differences in how the UI is rendered because you are basically dealing with completely different underlying UI frameworks. Now, if you were going from WPF .NET Framework to WPF .NET 7, everything should still render the same way.
 
Were you also using WPF when the code was in .NET Framework? Are you still using WPF in .NET 7? If you are porting f rom WinForm .NET Framework to MAUI or Xamarin Forms for .NET 7, then yes there could differences in how the UI is rendered because you are basically dealing with completely different underlying UI frameworks. Now, if you were going from WPF .NET Framework to WPF .NET 7, everything should still render the same way.

Thanks for the reply. Not sure about that. My old project was .NET Framework 4 Client Profile and a Windows Application. Is that WPF ? I don't know - not familiar with all this.
My new project is .NET 7.0. I looked in the properties and the box Windows Forms was checked, but Windows Presentation Foundation was not. I checked the WPF box and got a load of errors that I corrected by adding "using System.IO; " to a lot of files, but it made no difference to the ListBox. The dotted line box is still there :
1694121686182.png

So presumably that is not the issue. Now I don't know whether I should be using WPF or not. Does not seems to make much difference. I'll have to read up on why I would even need that or what it means. I'm just writing a stand alone program to be run in a Windows environment that uses forms.

Update
I believe that neither project is WPF, I am just using WinForms. Don't think I want to get into all that XAML stuff.
 
Last edited:
Ok, I've done some more research and development on this.
It seems to be due to a change in how the Property - Selection Mode = None is handled (WinForms - don't know about WPF).

In Net Framework 4, the Selection Mode = None, does what it says on the tin - nothing is selected when clicking on a listbox item (SelectedIndex = -1) and the item is not highlighted in any way.
This is what you would expect.

In Net 7, the Selection Mode = None still gives SelectedIndex = -1 when an item is clicked, but draws a dotted box around the item.
This could make it look as if the item has actually been selected.

I don't know why this change was made, but it looks wrong to me.

So, now I have two options,
1. Ignore the dotted line box.
2. Do yet another 'work around' - probably the usual setting focus to a tiny text box when clicking on the listbox - still get a flash of dotted lines though.
 
WinForms or WPF is a choice you make when creating a project; they're two completely different ways of creating a desktop application and are not interchanged mid project. The code snippet you've posted is WPF but if your project is winforms there is nowhere for that snippet to go because it's for a different project type

Show us the content of your Program.Main method so we can know what kind of project you've got

BTW, I don't think I'd have bothered "upgrading" a winforms app to .net 7 without good reason - winforms is hacked together to get the bare minimum working on core; many things are broken or turned off and won't be fixed/winforns on core will never be what it was on framework
 
WinForms or WPF is a choice you make when creating a project; they're two completely different ways of creating a desktop application and are not interchanged mid project. The code snippet you've posted is WPF but if your project is winforms there is nowhere for that snippet to go because it's for a different project type

Show us the content of your Program.Main method so we can know what kind of project you've got

BTW, I don't think I'd have bothered "upgrading" a winforms app to .net 7 without good reason - winforms is hacked together to get the bare minimum working on core; many things are broken or turned off and won't be fixed/winforns on core will never be what it was on framework

I'm pretty sure it is Winforms. Not sure what you mean by show us the content of your Program.Main method, but the Program.cs is basically :-
C#:
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.
            ApplicationConfiguration.Initialize();
            Application.Run(new MainForm());
        }
    }

What you say though is somewhat disturbing. Does not sound like Microsoft has much commitment to Winforms. From what I've read, not sure WPF has long to live either. Sounds like you prefer Net Framework to Net 7.

Maybe at some point I will investigate WPF or whatever is the replacement, but for the moment, I'll continue upgrading to Net 7. I may get some performance improvements to my Chess program with Net 7. I'll learn something new anyway.

N.B. no one has answered the main question though, as to why they changed the Selection Mode = None method to show the dotted lines. Maybe Microsoft don't care about Winforms anymore.

Update

Just created a new project in WPF and I see it is quite different - there is a XAML window below the Main window. I'll have to seriously look at this as an option.
 
Last edited:
I may just abandon the Winforms and go the whole hog to a WPF Net 7 application. Hopfully the back end c# will be just as fast and maybe I can beautify the front end. I tried copying an already created Form from Winforms to WPF but that did not work, so presumably I will have to create the Forms/Windows from scratch, unless I can find a way to transfer them.
 
Does not sound like Microsoft has much commitment to Winforms

They don't; industry has to twist their arm to support it on core at all. It's like, 20+ years old and is being left behind. Even WPF is heading towards legacy and maintenance mode. If you're starting new dev, consider something else

I tried copying an already created Form from Winforms to WPF but that did not work,

Chalk and cheese. Steam locomotive and Harley Davidson. It wouldn't ever work; the two techs just aren't even closely related

Selection Mode = None method to show the dotted lines.

As far as I'm concerned dotted lines are focus, and indicate a single item of current interest.
It is a separate and different concept to selections/selected items, which can be multiple and don't have to be the current item of interest at all

Users won't notice the dots if you want to kick it into the long grass
 
Last edited:
I don't know why this change was made, but it looks wrong to me.

I suspect that someone filed an 508 accessibility compliance bug, and Microsoft was obliged to fix it. Try using Windows for about a week with just a keyboard (without a mouse, touchpad, or touch screen) where you need to use the tab and Shift-Tab keys, as well as other keyboard mnemonics to navigate around the dialog boxes and windows. Having a visual indicate of where the current focus is at helps tremendously.
 
They don't; industry has to twist their arm to support it on core at all. It's like, 20+ years old and is being left behind. Even WPF is heading towards legacy and maintenance mode. If you're starting new dev, consider something else
Chalk and cheese. Steam locomotive and Harley Davidson. It wouldn't ever work; the two techs just aren't even closely related
As far as I'm concerned sorted lines are focus, and are single item. It is a separate and different concept to selections/selected items, which can be multiple and don't have to be the current item of interest at all
Users won't notice

Yes, but I am the User and I DO notice. Some of us left the professional IT world years ago and are just doing our own hobby projects, that when we die it will be as if they never existed.
Thanks very much though to Microsoft for providing Visual Studio for free - its pretty good really as is WinForms.

I've had a look at WPF and its far too complicated to even do something simple like create Menu Items. If I had the time, I might learn WPF, but for now I'll stick to WinForms.
 
I suspect that someone filed an 508 accessibility compliance bug, and Microsoft was obliged to fix it. Try using Windows for about a week with just a keyboard (without a mouse, touchpad, or touch screen) where you need to use the tab and Shift-Tab keys, as well as other keyboard mnemonics to navigate around the dialog boxes and windows. Having a visual indicate of where the current focus is at helps tremendously.

You are probably right, but it would be nice to have the option to turn it off. In this application, I don't need to see where the focus is - I don't ever want to see where the focus is - its irrelevant.
I have the same problem in Access projects, where the dotted line around focused items is just clutter. I have many zero size text boxes on forms just to get around this minor annoyance.
 
Oh, yea I can't disagree with that - winforms dev is incredibly easy.

One of the reasons I like Blazor so much is that I find it very similar to winforms, though there isn't exactly the same design experience (it's kinda more WPF flavored) it doesn't seem to matter so much with bootstrap grid, and you end up with a nice looking website, nothing the users need to install, control of your code and pivotally no need to contract a horrific disease from excessive exposure to JavaScript
 

Latest posts

Back
Top Bottom