winform自定义按钮菜单
//填写其他报表按钮
private void btnWriteRep_Click(object sender, EventArgs e)
{
try
{
Point p1 = new Point();
Point p = this.btnWriteRep.Location;
p1.X = p.X;
p1.Y = p.Y + btnWriteRep.Height;
contextmenustrip.Show(this.flowLayoutPanel3, p1);
}
catch (Exception ex)
{
}
}
//加载填写其他报表下拉按钮菜单
private bool LoadDropButton()
{
try
{
contextmenustrip = new ContextMenuStrip();//下拉菜单
contextmenustrip.Font = new Font("宋体", 10f);
List<ReportListItem> reportlist = basedeclar.GetReportListItem("Y");
foreach (ReportListItem rep in reportlist)
{
contextmenustrip.Items.Add(AddMenu("[" + rep.Bbzt_mc + "]" + rep.Name, rep.Value, rep.Bbzt_mc));
}
//分隔线
ToolStripSeparator line = new ToolStripSeparator();
contextmenustrip.Items.Add(line);
contextmenustrip.Items.Add(AddMenu("选择更多报表......", "", ""));
contextmenustrip.ItemClicked += new ToolStripItemClickedEventHandler(contextmenustrip_ItemClicked);
return true;
}
catch (Exception e)
{
MessageBox.Show("加载按钮(填写其他报表)菜单失败");
return false;
}
}
//下拉项单击事件
void contextmenustrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
try
{
if (isChanged)
{
if (MessageBox.Show("数据有变化,是否保存", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
btnSave_Click(sender, null);
}
else
{
//LoadAxCellData();
axCell1.ReadFromBuffer(PreBytes);
}
isChanged = false;
}
//选择更多报表显示所有报表
if (e.ClickedItem.Text.Contains("选择更多报表"))
{
isCloseMenu = true;
contextmenustrip.Items.RemoveAt(contextmenustrip.Items.Count - 1);
List<ReportListItem> reportlist = basedeclar.GetReportListItem("N");
foreach (ReportListItem rep in reportlist)
{
contextmenustrip.Items.Add(AddMenu("[" + rep.Bbzt_mc + "]" + rep.Name, rep.Value, rep.Bbzt_mc));
}
contextmenustrip.Closing += new ToolStripDropDownClosingEventHandler(contextmenustrip_Closing);
}
else
{
isCloseMenu = false;
//axCell1.SetSheetVisible(axCell1.GetCurSheet(), false);
//axCell1.SetSheetVisible(axCell1.GetSheetIndex(e.ClickedItem.Name), true);
axCell1.SetCurSheet(axCell1.GetSheetIndex(e.ClickedItem.Name));
}
}
catch (Exception ex)
{
}
}
//下拉菜单关闭判断是否关闭
void contextmenustrip_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
if (isCloseMenu)
{
if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked || e.CloseReason == ToolStripDropDownCloseReason.Keyboard)
e.Cancel = false;
else
e.Cancel = true;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗