I want to display a message box when any person clicks on the menu item which is not enabled I have tried the following coding but it is not displaying the message box.
当任何人点击未启用的菜单项时,我想显示一个消息框我尝试了以下编码,但它没有显示消息框。
Coding:
private void updateFineDetailsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (updateFineDetailsToolStripMenuItem.Enabled == true)
{
frmUpdateFineDetails objUpdateFineDetails = new frmUpdateFineDetails();
objUpdateFineDetails.MdiParent = this;
objUpdateFineDetails.Show();
}
else if (updateFineDetailsToolStripMenuItem.Enabled == false)
{
MessageBox.Show("Unauthorized Person");
}
}
By default I have set the enabled status to false and when the form loads I am checking whether the user is administrator, if the user is admin then this menu item will be enabled for all other user who logs into the application the above menu item has to be disabled.
默认情况下,我已将启用状态设置为false,当表单加载时我正在检查用户是否是管理员,如果用户是管理员,则此菜单项将为登录到应用程序的所有其他用户启用上述菜单项被禁用
Please note that the above coding does not generate any error, but it does not even display the messagebox as unauthorized person.
请注意,上述编码不会产生任何错误,但它甚至不会将消息框显示为未经授权的人。
Can anybody help me out in performing this task?
任何人都可以帮我完成这项任务吗?
Thanks in advance!
提前致谢!
5 个解决方案
#1
The whole idea of disabling a menu item or a button is to prevent the user from interacting with it. Typically the control will also be rendered in a way so that this becomes clear to the user. If you want to prevent the user from taking certain actions, for instance based on whether the user is an administrator or not, you can use one of three approaches:
禁用菜单项或按钮的整个想法是阻止用户与其交互。通常,控件也将以某种方式呈现,以使用户清楚。如果您想阻止用户采取某些操作,例如根据用户是否是管理员,您可以使用以下三种方法之一:
- Keep the control enabled and inform unauthorized users that the function is not available if he or she invokes it
- Disable the control
- Hide the control
启用控件并告知未经授权的用户,如果他或她调用该功能,则该功能不可用
禁用该控件
隐藏控件
In the later two cases there is no interaction, since the user cannot invoke the command. I usually tend to prefer to hide the command, if the access to it is role based (meaning that if I don't have access to the command when I start the application, it will not happen at any point while running it), or disabling it if the availability of the command is related to data state.
在后两种情况下,没有交互,因为用户无法调用该命令。我通常倾向于隐藏命令,如果对它的访问是基于角色的(意味着如果我在启动应用程序时无法访问该命令,则在运行它时不会发生任何操作),或者如果命令的可用性与数据状态相关,则禁用它。
#2
As I understand, a disabled menu item does not raise Click events.
据我了解,禁用的菜单项不会引发Click事件。
#3
If you need to display the error message then enable the menu item.
如果需要显示错误消息,请启用菜单项。
If admin clicks then default action occurs and if other users click show a message box and prevent the default action.
如果管理员点击,则会发生默认操作,如果其他用户单击显示消息框并阻止默认操作。
From your code I think you can do this just by enabling the menu item.
从您的代码我认为您可以通过启用菜单项来完成此操作。
#4
Disabling a menu item isn't a cosmetic display issue, it does just what you'd expect from the term - it disables the menu item.
禁用菜单项不是化妆品显示问题,它只是你对术语的期望 - 它会禁用菜单项。
When disabled, the menu item is effectively inert - it will not raise the click event, and there's no (straightforward) way to make it do so.
禁用时,菜单项实际上是惰性的 - 它不会引发单击事件,并且没有(直接)方式来实现它。
I can guess at your motivations - to explain to your users why the particular menu item is unavailable to them - but you'll need to find a different approach, one that works with the system, not against it.
我可以猜测你的动机 - 向你的用户解释为什么特定的菜单项对他们不可用 - 但是你需要找到一种不同的方法,一种适用于系统的方法,而不是它。
Some possible approaches ...
一些可能的方法......
- Leave the menu item disabled, allowing the user to select it - you can then make a role based choice on how to handle the event.
- Disable the menu item and provide feedback elsewhere in your UI telling the user what level of access they have.
- Display a tooltip with the reason for disabling the control
- Leave the menu item enabled, but change it's display to look inactive (this is hard to achieve 100%); this will allow the events to fire and your handler to be invoked
- Create a separate "Explain" menu item, perhaps on the "Help" menu; when invoked, all disabled menu items become enabled, and will explain to the user why the command is unavailable.
- Some systems display a line of help text relevant to the highlighted menu item on the status bar - you could leverage this to explain availability: "Maintain customer details (requires Administrator role)"
保持菜单项处于禁用状态,允许用户选择它 - 然后您可以基于角色选择如何处理事件。
禁用菜单项并在UI中的其他位置提供反馈,告诉用户他们具有什么级别的访问权限。
显示工具提示,其中包含禁用控件的原因
保持菜单项已启用,但将其显示更改为不活动(这很难达到100%);这将允许触发事件并调用您的处理程序
创建单独的“说明”菜单项,可能在“帮助”菜单上;在调用时,所有禁用的菜单项都将启用,并将向用户解释命令不可用的原因。
某些系统显示与状态栏上突出显示的菜单项相关的一行帮助文本 - 您可以利用此文档来解释可用性:“维护客户详细信息(需要管理员角色)”
#5
a disabled control does not raise Click events. you should simulate that state! for example:
禁用的控件不会引发Click事件。你应该模拟那个状态!例如:
- enable menu item
- change menu item's forecolor to SystemColors.GrayText
- save state of item in item's tag (enable or disable)
- if click event raised, first check item's tag
- change menu item's forecolor to normal fore color if enabled item.
启用菜单项
将菜单项的forecolor更改为SystemColors.GrayText
保存项目标签中的项目状态(启用或禁用)
如果点击事件引发,首先检查项目的标签
如果启用项目,则将菜单项的前景颜色更改为正常前色。
#1
The whole idea of disabling a menu item or a button is to prevent the user from interacting with it. Typically the control will also be rendered in a way so that this becomes clear to the user. If you want to prevent the user from taking certain actions, for instance based on whether the user is an administrator or not, you can use one of three approaches:
禁用菜单项或按钮的整个想法是阻止用户与其交互。通常,控件也将以某种方式呈现,以使用户清楚。如果您想阻止用户采取某些操作,例如根据用户是否是管理员,您可以使用以下三种方法之一:
- Keep the control enabled and inform unauthorized users that the function is not available if he or she invokes it
- Disable the control
- Hide the control
启用控件并告知未经授权的用户,如果他或她调用该功能,则该功能不可用
禁用该控件
隐藏控件
In the later two cases there is no interaction, since the user cannot invoke the command. I usually tend to prefer to hide the command, if the access to it is role based (meaning that if I don't have access to the command when I start the application, it will not happen at any point while running it), or disabling it if the availability of the command is related to data state.
在后两种情况下,没有交互,因为用户无法调用该命令。我通常倾向于隐藏命令,如果对它的访问是基于角色的(意味着如果我在启动应用程序时无法访问该命令,则在运行它时不会发生任何操作),或者如果命令的可用性与数据状态相关,则禁用它。
#2
As I understand, a disabled menu item does not raise Click events.
据我了解,禁用的菜单项不会引发Click事件。
#3
If you need to display the error message then enable the menu item.
如果需要显示错误消息,请启用菜单项。
If admin clicks then default action occurs and if other users click show a message box and prevent the default action.
如果管理员点击,则会发生默认操作,如果其他用户单击显示消息框并阻止默认操作。
From your code I think you can do this just by enabling the menu item.
从您的代码我认为您可以通过启用菜单项来完成此操作。
#4
Disabling a menu item isn't a cosmetic display issue, it does just what you'd expect from the term - it disables the menu item.
禁用菜单项不是化妆品显示问题,它只是你对术语的期望 - 它会禁用菜单项。
When disabled, the menu item is effectively inert - it will not raise the click event, and there's no (straightforward) way to make it do so.
禁用时,菜单项实际上是惰性的 - 它不会引发单击事件,并且没有(直接)方式来实现它。
I can guess at your motivations - to explain to your users why the particular menu item is unavailable to them - but you'll need to find a different approach, one that works with the system, not against it.
我可以猜测你的动机 - 向你的用户解释为什么特定的菜单项对他们不可用 - 但是你需要找到一种不同的方法,一种适用于系统的方法,而不是它。
Some possible approaches ...
一些可能的方法......
- Leave the menu item disabled, allowing the user to select it - you can then make a role based choice on how to handle the event.
- Disable the menu item and provide feedback elsewhere in your UI telling the user what level of access they have.
- Display a tooltip with the reason for disabling the control
- Leave the menu item enabled, but change it's display to look inactive (this is hard to achieve 100%); this will allow the events to fire and your handler to be invoked
- Create a separate "Explain" menu item, perhaps on the "Help" menu; when invoked, all disabled menu items become enabled, and will explain to the user why the command is unavailable.
- Some systems display a line of help text relevant to the highlighted menu item on the status bar - you could leverage this to explain availability: "Maintain customer details (requires Administrator role)"
保持菜单项处于禁用状态,允许用户选择它 - 然后您可以基于角色选择如何处理事件。
禁用菜单项并在UI中的其他位置提供反馈,告诉用户他们具有什么级别的访问权限。
显示工具提示,其中包含禁用控件的原因
保持菜单项已启用,但将其显示更改为不活动(这很难达到100%);这将允许触发事件并调用您的处理程序
创建单独的“说明”菜单项,可能在“帮助”菜单上;在调用时,所有禁用的菜单项都将启用,并将向用户解释命令不可用的原因。
某些系统显示与状态栏上突出显示的菜单项相关的一行帮助文本 - 您可以利用此文档来解释可用性:“维护客户详细信息(需要管理员角色)”
#5
a disabled control does not raise Click events. you should simulate that state! for example:
禁用的控件不会引发Click事件。你应该模拟那个状态!例如:
- enable menu item
- change menu item's forecolor to SystemColors.GrayText
- save state of item in item's tag (enable or disable)
- if click event raised, first check item's tag
- change menu item's forecolor to normal fore color if enabled item.
启用菜单项
将菜单项的forecolor更改为SystemColors.GrayText
保存项目标签中的项目状态(启用或禁用)
如果点击事件引发,首先检查项目的标签
如果启用项目,则将菜单项的前景颜色更改为正常前色。