_From @PeterPann23 on Sunday, August 11, 2019 5:19:35 PM_
Created a new project in .net Core 3.0
Added a ListView to the form that I populate based on user actions (button click works just fine)
The events on the listview however do not get firred.
Steps to reproduce the behavior:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.7.0-preview7.19362.9" />
<PackageReference Include="System.Windows.Forms.DataVisualization" Version="1.0.0-prerelease.19218.1" />
</ItemGroup>
</Project>
The form uses the "normal" template
```C#
partial class Form1
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
// controls before ...
this.ListViewControlItems = new System.Windows.Forms.ListView();
// other controls after ...
//
// ListViewControlItems
//
this.ListViewControlItems.Dock = System.Windows.Forms.DockStyle.Fill;
this.ListViewControlItems.FullRowSelect = true;
this.ListViewControlItems.HideSelection = false;
this.ListViewControlItems.Location = new System.Drawing.Point(0, 37);
this.ListViewControlItems.MultiSelect = false;
this.ListViewControlItems.Name = "ListViewControlItems";
this.ListViewControlItems.Size = new System.Drawing.Size(464, 413);
this.ListViewControlItems.TabIndex = 2;
this.ListViewControlItems.UseCompatibleStateImageBehavior = false;
this.ListViewControlItems.View = System.Windows.Forms.View.Details;
this.ListViewControlItems.SelectedIndexChanged += new System.EventHandler(this.OnSelectedTrainingFileChanged);
}
}
the "Code Behind" part of the form is like this:
```CSharp
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ListViewControlItems.Click += ListViewControlItems_Click;
}
private void ListViewControlItems_Click( object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
The button click event's get invoked without any problem, I'd expect the Listview to invoke as well. I now add a button to trigger the SelectedIndex change event and works just fine, also knows if any items are selected
```C#
private void OnSelectedTrainingFileChanged(object sender, EventArgs e)
{
if (ListViewControlItems.SelectedItems.Count > 0)
{
ConfigPropGrid.SelectedObject = ListViewControlItems.SelectedItems[0].Tag as NativeConfig;
}
else
ConfigPropGrid.SelectedObject = null;
}
### Screenshots

### Additional context
Build output:
● Started at 19:17:54
1>------ Build started: Project: CATS.ML.Interfaces, Configuration: Debug Any CPU ------
1>You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview
1>CATS.ML.Interfaces -> D:SourceCats.MLCATS.ML.InterfacesbinDebugnetstandard2.1CATS.ML.Interfaces.dll
2>------ Build started: Project: Cats.Deepbook.IBApi, Configuration: Debug Any CPU ------
3>------ Build started: Project: Cats.ML.Core, Configuration: Debug Any CPU ------
4>------ Build started: Project: CATS.ML.Data, Configuration: Debug Any CPU ------
4>You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview
3>You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview
2>Cats.Deepbook.IBApi -> D:SourceDeepbookCats.Deepbook.IBApibinDebugnetcoreapp3.0Cats.Deepbook.IBApi.dll
3>MLDataVersatileVersatileMLDataSet.cs(103,64,103,89): warning CS0067: The event 'VersatileMLDataSet.OnColumnsWithNoAddedValue' is never used
3>Cats.ML.Core -> D:SourceCats.MLcats.ml.corebinDebugnetstandard2.1Cats.ML.Core.dll
3>Done building project "Cats.ML.Core.csproj".
4>CATS.ML.Data -> D:SourceCats.MLCATS.ML.DatabinDebugnetstandard2.1CATS.ML.Data.dll
5>------ Build started: Project: Cats.Deepbook.Shared, Configuration: Debug Any CPU ------
5>DeeBookNativeBarBuffer.cs(42,81,42,96): warning CS0067: The event 'DeeBookNativeBarBuffer.OnCrazyMovement' is never used
5>Cats.Deepbook.Shared -> D:SourceDeepbookCats.Deepbook.SharedbinDebugnetcoreapp3.0Cats.Deepbook.Shared.dll
5>Done building project "Cats.Deepbook.Shared.csproj".
6>------ Build started: Project: CATS.Deepbook.NetworkTrader, Configuration: Debug Any CPU ------
6>CATS.Deepbook.NetworkTrader -> D:SourceDeepbookCATS.Deepbook.NetworkTraderbinDebugnetcoreapp3.0CATS.Deepbook.NetworkTrader.dll
========== Build: 6 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
● Finished at 19:17:56 (total time 2.34s)
`dotnet --info`
.NET Core SDK (reflecting any global.json):
Version: 3.0.100-preview7-012821
Commit: 6348f1068a
Runtime Environment:
OS Name: Windows
OS Version: 10.0.18362
OS Platform: Windows
RID: win10-x64
Base Path: C:Program Filesdotnetsdk3.0.100-preview7-012821
Host (useful for support):
Version: 3.0.0-preview7-27912-14
Commit: 4da6ee6450
.NET Core SDKs installed:
2.1.800-preview-009677 [C:Program Filesdotnetsdk]
2.1.800-preview-009696 [C:Program Filesdotnetsdk]
2.1.800 [C:Program Filesdotnetsdk]
2.2.300 [C:Program Filesdotnetsdk]
2.2.400-preview-010195 [C:Program Filesdotnetsdk]
2.2.400-preview-010219 [C:Program Filesdotnetsdk]
2.2.400 [C:Program Filesdotnetsdk]
3.0.100-preview6-012264 [C:Program Filesdotnetsdk]
3.0.100-preview7-012821 [C:Program Filesdotnetsdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.11 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.12 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.5 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.6 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.11 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.12 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.5 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.6 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0-preview6.19307.2 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0-preview7.19365.7 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.11 [C:Program FilesdotnetsharedMicrosoft.NETCore.App]
Microsoft.NETCore.App 2.1.12 [C:Program FilesdotnetsharedMicrosoft.NETCore.App]
Microsoft.NETCore.App 2.2.5 [C:Program FilesdotnetsharedMicrosoft.NETCore.App]
Microsoft.NETCore.App 2.2.6 [C:Program FilesdotnetsharedMicrosoft.NETCore.App]
Microsoft.NETCore.App 3.0.0-preview6-27804-01 [C:Program FilesdotnetsharedMicrosoft.NETCore.App]
Microsoft.NETCore.App 3.0.0-preview7-27912-14 [C:Program FilesdotnetsharedMicrosoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0-preview6-27804-01 [C:Program FilesdotnetsharedMicrosoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.0.0-preview7-27912-14 [C:Program FilesdotnetsharedMicrosoft.WindowsDesktop.App]
```
_Copied from original issue: aspnet/AspNetCore#13035_
@PeterPann23 The code which you have shared doesn't reproduce the problem. The Click event of ListView will raise if you click on an item, but I cannot see any item in your code.
I've already tried the following code in VS2019, .NET CORE 3 without any problem:
C#
using System;
using System.Windows.Forms;
namespace WinFormsNetCore
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true);
Application.Run(new MainForm());
}
class MainForm : Form
{
public MainForm()
{
var listView1 = new ListView();
listView1.Items.Add("Item1");
listView1.Items.Add("Item2");
listView1.Click += ListView1_Click;
this.Controls.Add(listView1);
}
private void ListView1_Click(object sender, EventArgs e)
{
MessageBox.Show("Clicked!");
}
}
}
}
@r-aghaei the question should be directed at @PeterPann23 as the original topic started.
@PeterPann23 please provide a repro project that exhibits the issue - either a zip or a link to a github repo will work.
Thank you
Oops! I corrected the mention. Thanks @RussKie.
Sure
@PeterPann23 It would be great if you can share a really simple repro, like what I shared. I couldn't use your project.
Did you give my code a try?
@r-aghaei
I'm guessing you did not see my post just above yours. it contains a zip file with code that, on my pc does not fire the events.
@PeterPann23 In fact I tried to run it, but it has a lot of dependencies and non-relevant codes which makes it a bit hard to run and debug. That's why I posted my previous comment asking for a minimal repro if possible. Something like what I shared. Did you try the example which I shared?
What dependencies are you referencing? I have json, that's not needed.
My code follows the normal pattern of a partial class with InitializeComponent(), actually this is auto generated code by visual studio and does not contain any 3rd party controls.
Sure, there are more controls than needed, I agree but this code contains the issue of the treeview not firing the selection changing as wel as the the onclick event on line 44.
I was asked for a sample that reproduced the problem, compile it and click… nothing happens.

I can confirm that the zipped project won't run as-is on the current VS 2019 preview. It has problems resolving the nuget packages and even after removing them something about the project file doesn't work out correctly. Anyways, it was pretty easy to comment all that stuff out, and the problem of no click event still repros. I'm attaching the edited project with nuget/csproj/designer items commented out, in case it helps you debugging. It probably can be reduced further by removing unnecessary UI and data files, but as it is now it shouldn't have any external dependencies beyond WinForms Core
The SelectedIndexChanged can be made working by moving the initialization (except InitializeComponent) from constructor to Load event. This might be related to #1311 however click event still doesn't work. Removing the message box from the SelectedIndexChanged event makes the click work too.
So I can make two observations:
SelectedIndexChanged will break the click event@weltkante okey... I tried to do what you described however I must miss understand what you write as when I bind the event in an Form1.Show and remove it in InitializeComponent() the show gets invoked, however my bound event still get's ignored.
private void OnFormLoaded(object sender, EventArgs e)
{
ListViewControlItems.SelectedIndexChanged += OnSelectedTrainingFileChanged;
}
What am I missing for this "work around" to work?
@PeterPann23 I had to move the whole initialization from constructor to Load event, not just the event subscription. Something in that initialization breaks the ListView. I'm attaching the zip with the modifications. (Note I'm no longer at my VS 2019 machine so might have syntax errors, but that was what got Click event working. I've got VS 2019 preview with nightly build of .NET Core on that machine, in case it matters.)
issue 1607 (with workaround).zip
(for reproduction of the bug use the zip from my previous comment, this one has a workaround)
@weltkante
Ok, now I understand what you mentioned by _moving the initialization_ looks like controls should not be altered at the constructor until a fix is implemented, your sample solved my issue, sorting and selecting works like a charm now.
Perhaps one should mention your work around it in the "known" issues
I've narrowed it down to ListViewControlItems.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); call.
If you comment it out - everything will work as expected.
Having a look at the documentation for InvokeRequired I noticed familiarity especially reading the section regarding the Property IsHandleCreated. I noticed that the constructor tells me that I'm to early. Perhaps throw an error calling AutoResizeColumns when the handle is not created? No harm done when fixed, no harm done when developing as it can save a few hours trying to figure out what is wrong....
if (ListViewControlItems.IsHandleCreated)
{
Trace.WriteLine("NO");
}
I now have tested both scenarios on .NET Core and Desktop Framework:
ListViewControlItems.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); in the constructor works in Desktop Framework, so its a regressionSelectedIndexChanged prevents the Click event also on Desktop Framework, so this is not a regression.I suggest keeping this issue about the AutoResizeColumns regression (as the changed title already indicates). @PeterPann23 if this wasn't just for debugging and you actually need to show a message box from SelectedIndexChanged and still get the Click event then you probably should open a separate issue for that, but it'll be much lower priority since its not a regression against Desktop Framework.
actually need to show a message box from SelectedIndexChanged and still get the Click event then you probably should open a separate issue for that, but it'll be much lower priority since its not a regression against Desktop Framework.
I don't think we can safely implement this change without adding new quirks since it would affect existing consumers.
It is possible to call requied functionality from both handlers of Click and SeletedIndexchanged events.
It's not showing the MessageBox which prevents the Click event, it's releasing the capture which prevents a mouse up and prevents a click. If you don't release the capture and close the message box using Esc key, the Click event will raise. IMO, it's expected behavior.
Also consider the following points:
In general, showing a message box in SelectedIndexChanged doesn't look like a good idea. The event may raise more than once. For example once for the item which get selected and once for the item which get deselected.
The other thing that you should be careful about, is putting some logic in Click event. Users may select items using keyboard, so the logic which you have in Click event handler will not run in case of selecting item with keyboard.
If for any reason, you want to rely on mouse events, use MouseDown. It raises before SelectedindexChanged. If you want to make sure the mouse down event has been raised by keeping mouse down on an item, just check using HitTest method: if (((ListView)sender).HitTest(e.Location).Item != null)
I have noted the same issue, on some other controls being Chart (KeyDown, MouseDown) and Datagridview (RowEnter), moving the binding from the AutoGenerated code called in InitializeComponent() to the Load event solves this.
Should be tested again once PR #1661 is in.
Verified with latest .NET Core 3.0 Preview 9 3.0.100.14004 build, it’s not reproduced for now.
Thank you!
Most helpful comment
I've narrowed it down to
ListViewControlItems.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);call.If you comment it out - everything will work as expected.