开发者

Split button in .NET Winforms [closed]

开发者 https://www.devze.com 2022-12-09 14:19 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 7 years ago.

Improve this question

I'm looking for a Split Bu开发者_高级运维tton in .NET WinForms. The kind where one side is a button and the other side has a dropdown button.

I see them used all over in windows, like in the Visual Studio Save As window, so I figured they've got to have the control in some library.

I know there's one for toolstrips, but I need one thats usable outside of toolstrips.

Is there a Microsoft library that has one or preferably a free library? I'm using .NET 3.5

For an example:

Split button in .NET Winforms [closed]


You can do a simple version yourself, using the button's image. I have my own class that is derived from Button.

I set up the image (which is of a down arrow) like so:

{
    this.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
    this.Image = YourResources.split_button; // Your down-arrow image

    this.TextImageRelation = System.Windows.Forms.TextImageRelation.TextBeforeImage;
}


protected override void OnClick(EventArgs e)
{
    var clickPos = this.PointToClient(new System.Drawing.Point(MousePosition.X, MousePosition.Y));

    // If click is over the right-hand portion of the button show the menu
    if (clickPos.X >= (Size.Width - Image.Width))
        ShowMenuUnderControl()
    else
        base.OnClick(e);
}

// If you want right-mouse click to invoke the menu override the mouse up event
protected override void OnMouseUp(MouseEventArgs mevent)
{
    if ((mevent.Button & MouseButtons.Right) != 0)
        ShowMenuUnderControl();
    else
        base.OnMouseUp(mevent);
}

// Raise the context menu
public void ShowMenuUnderControl()
{
    splitMenuStrip.Show(this, new Point(0, this.Height), ToolStripDropDownDirection.BelowRight);
}

If you also wanted an icon, as in the OP, you could use a BackgroundImage and appropriate padding, like so:

this.BackgroundImageLayout = ImageLayout.None;
this.BackgroundImage = YourResources.ButtonIcon;

// Add padding so the text doesn't overlay the background image
this.Padding = new Padding(
    this.Padding.Left + this.BackgroundImage.Width,
    this.Padding.Top,
    this.Padding.Right,
    this.Padding.Bottom);

Here's a button of mine in action:

Split button in .NET Winforms [closed]

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号