在C#对话框中,我想添加一个具有双重行为的按钮,即Save and Save As。当用户点击按钮的右上角时,会出现一个小的上下文菜单,指示“另存为”选项。
答案 0 :(得分:1)
SplitButton
将是实现这一目标的最佳选择。您会发现here代码SplitButton
以及使用它的示例。
答案 1 :(得分:1)
您可以拥有自己的按钮类,这些按钮类派生自Button
,只需几个覆盖即可完成此任务。只需在我的代码中传递ContextMenuStrip
(splitMenuStrip
)。
我设置了图像(向下箭头),如下所示:
{
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);
}
要获得更全面的图标答案并右键点击以调用菜单,请查看我对this similar/same question的回答。