C # Use Panel to implement dynamic sidebar OF THE FORM

Source: Internet
Author: User

You can expand, hide, and lock the sidebar. By default, the added sidebar is not displayed and is triggered by the mouse hover event MouseHover. The Sidebar is actually a form or container. The expanded action must gradually increase the width within a period of time. I use Panel as the container carrier, gradually add the Width attribute of the Panel in a thread. Hiding is mainly implemented by the Visible attribute of the Panel. The hiding condition is determined by judging whether the mouse position is beyond the boundary of the sidebar. Locking means that the Panel is always displayed, so that the hidden Panel function is not executed.
Interface

Related code:
Expand:
Private void toolStripButton2_MouseHover (object sender, EventArgs e)
{
// Hover the cursor over the event to expand
If (! This. panel2.Visible) // panel2 is the sidebar container
{
Thread ts = new Thread (new ParameterizedThreadStart (TaskShowPanel ));
Ts. Is true; // set it to a background thread.
Ts. Priority = ThreadPriority. Normal; // set the Priority.
Ts. Start (Object) 245); // The expanded Panel width is 245.
}
}
// Defined thread execution function
Private void TaskShowPanel (Object ParObject)
{
Int w = (int) ParObject;
For (int I = 0; I <= w; I ++)
{
ShowPanel (this. panel2, I );
}
}
Private delegate void SetTextCallback (Panel p1, int w );
/// <Summary>
/// Expand an action
/// </Summary>
/// <Param name = "p1"> panel container </param>
/// <Param name = "w"> width </param>
Private void ShowPanel (Panel p1, int w)
{
// Cross-thread access
Try
{
If (p1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback (ShowPanel );
P1.Invoke (d, new Object [] {p1, w });
}
Else
{
P1.Width = w;
If (! P1.Visible)
{
P1.Visible = true;
}
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message, "ShowPanel function execution error ");
}
}
Hide:
Private void listView1_MouseMove (object sender, MouseEventArgs e)
{
Point panelPoint = this. PointToClient (Control. MousePosition); // The current mouse position
// This. Start position of panel2.Location. X panel2
// This. panel2.Width the width of panel2
If (this. panel2.Visible & (panelPoint. X> (this. panel2.Location. X + this. panel2.Width )))
{
This. panel2.Visible = false;
}
}
Lock:
Private void button#click (object sender, EventArgs e)
{
// Delete hidden events
This. listView1.MouseMove-= new System. Windows. Forms. MouseEventHandler (this. listview#mousemove );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.