Skip to content

ListView.AutoResizeColumns prevents control from firing events #1607

Closed

Description

@Pilchie

From @PeterPann23 on Sunday, August 11, 2019 5:19:35 PM

Describe the bug

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.

To Reproduce

Steps to reproduce the behavior:

  1. Visual Studio 2019, Core 3.0 Release 3.0.100-preview7-012821
  2. Run this the File New Windows Forms App(.Net Core) Project
    My project looks like this
<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

partial class Form1
{
	/// <summary>
	/// Required designer variable.
	/// </summary>
	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:

public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();
		ListViewControlItems.Click += ListViewControlItems_Click;
	}
	
	private void ListViewControlItems_Click( object sender, EventArgs e)
	{
		throw new NotImplementedException();
	}
}
  1. neither the Click event or the SelectedIndexChanged event ever get invoked

Expected behavior

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

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

Capture

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:\Source\Cats.ML\CATS.ML.Interfaces\bin\Debug\netstandard2.1\CATS.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:\Source\Deepbook\Cats.Deepbook.IBApi\bin\Debug\netcoreapp3.0\Cats.Deepbook.IBApi.dll
3>ML\Data\Versatile\VersatileMLDataSet.cs(103,64,103,89): warning CS0067: The event 'VersatileMLDataSet.OnColumnsWithNoAddedValue' is never used
3>Cats.ML.Core -> D:\Source\Cats.ML\cats.ml.core\bin\Debug\netstandard2.1\Cats.ML.Core.dll
3>Done building project "Cats.ML.Core.csproj".
4>CATS.ML.Data -> D:\Source\Cats.ML\CATS.ML.Data\bin\Debug\netstandard2.1\CATS.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:\Source\Deepbook\Cats.Deepbook.Shared\bin\Debug\netcoreapp3.0\Cats.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:\Source\Deepbook\CATS.Deepbook.NetworkTrader\bin\Debug\netcoreapp3.0\CATS.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 Files\dotnet\sdk\3.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 Files\dotnet\sdk]
  2.1.800-preview-009696 [C:\Program Files\dotnet\sdk]
  2.1.800 [C:\Program Files\dotnet\sdk]
  2.2.300 [C:\Program Files\dotnet\sdk]
  2.2.400-preview-010195 [C:\Program Files\dotnet\sdk]
  2.2.400-preview-010219 [C:\Program Files\dotnet\sdk]
  2.2.400 [C:\Program Files\dotnet\sdk]
  3.0.100-preview6-012264 [C:\Program Files\dotnet\sdk]
  3.0.100-preview7-012821 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview6.19307.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview7.19365.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview6-27804-01 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview7-27912-14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0-preview6-27804-01 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.0.0-preview7-27912-14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Copied from original issue: dotnet/aspnetcore#13035

Activity

  1. r-aghaei commented on Aug 12, 2019

    @r-aghaei

    @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:

    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!");
                }
            }
        }
    }
  2. RussKie commented on Aug 12, 2019

    @RussKie
  3. RussKie commented on Aug 12, 2019

    @RussKie
    Contributor

    @PeterPann23 please provide a repro project that exhibits the issue - either a zip or a link to a github repo will work.
    Thank you

  4. added
    waiting-author-feedback
    on Aug 12, 2019
  5. r-aghaei commented on Aug 12, 2019

    @r-aghaei

    Oops! I corrected the mention. Thanks @RussKie.

  6. PeterPann23 commented on Aug 12, 2019

    @PeterPann23
  7. removed
    waiting-author-feedback
    on Aug 12, 2019
  8. r-aghaei commented on Aug 12, 2019

    @r-aghaei

    @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?

  9. PeterPann23 commented on Aug 13, 2019

    @PeterPann23

    @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.

  10. r-aghaei commented on Aug 13, 2019

    @r-aghaei

    @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?

  11. PeterPann23 commented on Aug 13, 2019

    @PeterPann23

    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.

    image

  12. weltkante commented on Aug 13, 2019

    @weltkante
    Contributor

    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

    issue 1607 (edited).zip

  13. weltkante commented on Aug 13, 2019

    @weltkante
    Contributor

    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:

  14. 10 remaining items

  15. RussKie commented on Aug 14, 2019

    @RussKie
    Contributor

    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.

  16. r-aghaei commented on Aug 14, 2019

    @r-aghaei

    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.

  17. r-aghaei commented on Aug 14, 2019

    @r-aghaei

    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)

  18. PeterPann23 commented on Aug 18, 2019

    @PeterPann23

    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.

  19. weltkante commented on Aug 19, 2019

    @weltkante
  20. ghost added
    🚧 work in progress
    on Aug 19, 2019
  21. ghost removed
    🚧 work in progress
    on Aug 20, 2019
  22. added this to the 3.0.0-Preview9 milestone on Aug 20, 2019
  23. Zheng-Li01 commented on Aug 27, 2019

    @Zheng-Li01
    Member

    Verified with latest .NET Core 3.0 Preview 9 3.0.100.14004 build, it’s not reproduced for now.

  24. RussKie commented on Aug 27, 2019

    @RussKie
    Contributor

    Thank you!

  25. ghost locked as resolved and limited conversation to collaborators on Feb 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🪲 bug💥 regression-previewtenet-compatibility

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions