2

In C# dialog, I want to add a single button with dual behaviour i.e. Save and Save As. When the user clicks on the upper right corner of the button, a little context menu should appear indicating the Save As option.

halfer
  • 19,824
  • 17
  • 99
  • 186
WAQ
  • 2,556
  • 6
  • 45
  • 86
  • 1
    You want to achieve this in WPF or WinForms or what? – Rafal May 30 '13 at 10:54
  • @Rafal in c# Winforms – WAQ May 30 '13 at 10:56
  • Is this button just on the form or in a toolstrip? Because in a toolstrip you can use a SplitButton which is a combined button with a drop-down menu. – DonBoitnott May 30 '13 at 10:59
  • Its just a button on the form. Actually I dont have much space on the form left, so I want to have this kind of behaviour so that I can have both the option on one button. – WAQ May 30 '13 at 11:00
  • You're going to have trouble with this part: "When the user clicks on the upper right corner of the button". How will you distinguish this zone? You will only know a click, but not precisely where unless you get into calculating using the button size, it's relative position in space, and MousePostion. That's going to be some ugly math. You could, however, use right-click on the button to display a ContextMenu. – DonBoitnott May 30 '13 at 11:41
  • @DonBoitnott: What you write is completely wrong. You need two lines of code to obtain the mouse coordinates and to convert them into relative button coordinates. However what is more work is drawing the split button. – Elmue May 06 '19 at 23:46

2 Answers2

2

SplitButton will be the best choice to achieve this. You will find here a code of SplitButton and sample of using it.

Rafal
  • 1,081
  • 8
  • 10
  • If it's not a toolstrip (or ContextMenuStrip in the example you offer), then SplitButton is not an option. Given OP's last comment, I'd say it's not. – DonBoitnott May 30 '13 at 11:38
  • @DonBoitnott I guess I'm going to have to spare space for the second button. – WAQ May 30 '13 at 11:55
  • @WasimQadir The right-click on the button to display a pop-up menu is the next best choice, but otherwise, yes...a second button would be in order. – DonBoitnott May 30 '13 at 11:56
  • @Wasim just try code from link I give. According to your requirements `SplitButton` is the best. Replace your `Button` by `SplitButton`. – Rafal May 30 '13 at 11:59
1

You can have your own button class, derived from Button, with a couple of overrides that will do the job simply. Just pass in a ContextMenuStrip (splitMenuStrip in my code below).

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);
}


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

For a more comprehensive answer with icon and right-clicking to invoke the menu check out my answer to this similar/same question.

Community
  • 1
  • 1
noelicus
  • 14,468
  • 3
  • 92
  • 111