Unable to hide ListBox in MouseLeave

Asked 2 years ago, Updated 2 years ago, 43 views

Thank you for your help.

I'm trying to create a ListBox that appears in the mouse Enter and hides in the Leave.

I put a panel on the back, put a ListBox on top of it, show ListBox on the MouseEnter of the panel, and try to hide it on MouseLeave, but it doesn't work.

It works fine with other FlowLayoutPanels, not ListBox, but it doesn't work well with ListBox.

However, when I put a breakpoint on the process in the MouseLeave of ListBox, the process runs correctly, and then it is hidden, but when I remove the breakpoint and make it work normally, it doesn't work.

Here's the code: (VisualStudioExpress 2015)

using System;
using System.Collections.General;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace A
{
    public partial class Form 1:Form
    {
        public Form 1()
        {
            InitializeComponent();
        }

        private void listBox1_MouseLeave(object sender, EventArgse)
        {
            This.listBox1.Visible=false;
        }

        private void panel1_MouseEnter(object sender, EventArgse)
        {
            This.listBox1.Visible=true;
        }
    }
}
namespace A
{
    partial class Form 1
    {
        /// <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 dispatched;otherwise, false.</param>
        protected override void disposition (bool disposition)
        {
            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()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.listBox1 = new System.Windows.Forms.ListBox();
            This.panel1.SuspendLayout();
            This.SuspendLayout();
            // 
            // panel1
            // 
            This.panel1.Controls.Add (this.listBox1);
            this.panel1.Dock=System.Windows.Forms.DockStyle.Left;
            this.panel1.Location=new System.Drawing.Point(0,0);
            This.panel1.Name="panel1";
            this.panel1.Size = new System.Drawing.Size(100,253);
            This.panel1.TabIndex=0;
            this.panel1.MouseEnter+=newSystem.EventHandler(this.panel1_MouseEnter);
            // 
            // listBox1
            // 
            this.listBox1.Dock=System.Windows.Forms.DockStyle.Fill;
            This.listBox1.FormattingEnabled=true;
            This.listBox1.ItemHeight=15;
            this.listBox1.Location=new System.Drawing.Point(0,0);
            This.listBox1.Name="listBox1";
            This.listBox1.ScrollAlwaysVisible=true;
            this.listBox1.Size=new System.Drawing.Size(100,253);
            This.listBox1.TabIndex=0;
            this.listBox1.MouseLeave+=newSystem.EventHandler(this.listBox1_MouseLeave);
            // 
            // Form 1
            // 
            this.AutoScaleDimensions=new System.Drawing.SizeF (8F, 15F);
            this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
            This.ClientSize = new System.Drawing.Size(282,253);
            This.Controls.Add (this.panel1);
            This.Name="Form1";
            This.Text="Form1";
            This.panel1.ResumeLayout(false);
            This.ResumeLayout(false);
        }

        #endregion

        private System.Windows.Forms.Panel1;
        private System.Windows.Forms.ListBox listBox1;
    }
}

Also, the panel and ListBox's Margin and Padding are all zero, and they are arranged perfectly without any gaps.
Panel is Dock.Left and ListBox is Dock.Fill.

Please tell me the cause and countermeasures.

Thank you for your cooperation.

c# windows

2022-09-29 22:33

2 Answers

As for the source you suggested, the panel1_MouseEnter event will run immediately after the listBox1_MouseLeave event, and as a result, the Visible in listBox1 will not change.

If you choose two types of events, you will be able to achieve your expectations.

private void panel1_MouseEnter(object sender, EventArgse)
{
    This.listBox1.Visible=true;
}

private void Form1_MouseEnter(object sender, EventArgse)
{
    This.listBox1.Visible=false;
}


2022-09-29 22:33

It's not a beautiful way to write, but I think it will be the desired action.

public partial class Form 1:Form {

    publicForm1(){
        InitializeComponent();
        This.listBox1.Visible=false;
    }

    const int INTERVAL = 100;
    int lastLeavedTickCount = 0;

    private void listBox1_MouseLeave(object sender, EventArgse){
        this.lastLeavedTickCount=System.Environment.TickCount;
        This.listBox1.Visible=false;
    }

    private void panel1_MouseEnter(object sender, EventArgse){
        if (lastLeavedTickCount + INTERVAL > System.Environment.TickCount)
            return;
        This.listBox1.Visible=true;
    }

}

Depending on the speed of the mouse, the MouseEnter of the panel may or may not occur during MouseLeave of ListBox, so it is forced...


2022-09-29 22:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.