如果用户单击 [X] 按钮与选择“退出”按钮,则实现不同的行为。菜单项
C# 中有没有办法根据用户是否单击 [X] 按钮或选择“退出”菜单项来实现不同的行为?
我想要做的是,如果他们单击 [X],它会最小化为托盘图标,但如果他们右键单击托盘图标,则会弹出一个带有“退出”选项的上下文菜单。如果他们单击“退出”选项,应用程序应该退出。
我尝试在 Main_FormClosing(object sender, FormClosingEventArgs e) 方法中检查“对象发送者”...但它似乎总是表明该事件是由我的主类发送的?
Is there a way in C# to implement a different behavior based on whether the user clicks the [X] button, or chooses the "Exit" menu item?
What I want to do is make it so that if they click the [X], it minimizes to a tray icon, but if they right-click the tray icon, it brings up a context menu with an "Exit" option. If they click that "Exit" option, the application should exit.
I tried checking "object sender" in my Main_FormClosing(object sender, FormClosingEventArgs e) method... but it always seems to indicate the event is being sent by my main class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
bool
成员添加到您的表单中。在退出菜单项单击事件调用Close
之前将其设置为true
。在FormClosing
中,检查该变量是否已设置并正确处理逻辑。Add a
bool
member to your form. Set it totrue
right before your exit menu item click event callsClose
. InFormClosing
, check to see if that variable is set and handle the logic appropriately.