通过方法(功能)启用/禁用按钮/上下文菜单 [英] Enable/disable button/contextmenu via a method (function)

查看:72
本文介绍了通过方法(功能)启用/禁用按钮/上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。我有8个按钮,每个按钮执行不同的任务,即编辑,删除,创建等,以及每个任务的上下文菜单



2.我是DB中的* Moderations *表,包括bools,即groupTitle,canEdit,canDelete,canCreate ..... groupTitle是字符串而不是bool



3.我有一个bool函数* canDoIt(task,userid)*检查登录用户(具有特定的groupTitle),是否可以执行或不能(函数返回true或false为提供的任务,简而言之)



假设,我想检查登录用户是否可以执行任务,通过canDoit(任务,用户ID)检查,如果他不能,则按钮将被禁用不会....

OnForm_Load我抛出这个功能(或者可能是我需要它的时候)并检查每个按钮,即



btnEdit.Enabled = canDo(canEdit,userID)

btnDelete.Enabled = canDo(canDelete,userID)

btnCreate.Enabled = canDo(canCreat,userID)



cnxMenuEdit.Enabled = canDo(canEdit,userID)

cnxMenuDelete.Enabled = canDo (canDelete,userID)







.....等等等等。



我的方法工作得很好但很好但我有疑问和问题....



第一个问题,好不容易这样?

第二个问题,它是否专业?

另一个是,会影响程序或数据库性能吗?



很抱歉,如果已经询问/发布了.....



我尝试了什么:



btnEdit.Enabled = canDo(canEdit,userID)

btnDelete.Enabled = canDo(canDelete,userID)

btnCreate.Enabled = canDo(canCreat,userID)



cnxMenuEdit.Enabled = canDo(canEdit,userID)

cnxMenuDelete.Enabled = canDo(canDelete,userID)







.....等等等等。

1. I have 8 buttons, each performs a different task, i.e. edit, delete, create etc, and a context​Menu for each of the task

2. I've a table called *Moderations* in DB, which consists of bools i.e. groupTitle, canEdit, canDelete, canCreate..... groupTitle is string not bool

3. I have a bool function *canDoIt(task, userid)* to check whether the logged in user (which will have specific groupTitle), can perform or can't (function return true or false for provided task, in short)

Suppose, I want to check whether a logged in user can perform the task or not, check via canDoit(task, userid), and If he cannot, the button will be disabled otherwise won't....
OnForm_Load I throw the function (or may be another time when I need it) and check for each button, i.e.

btnEdit.Enabled = canDo("canEdit", userID)
btnDelete.Enabled = canDo("canDelete", userID)
btnCreate.Enabled = canDo("canCreat", userID)

cnxMenuEdit.Enabled = canDo("canEdit", userID)
cnxMenuDelete.Enabled = canDo("canDelete", userID)
.
.
.
.....and so on and so forth.

My method work fine and good but I have doubts and questions....

First question, is good to be so?
Second question, is it professional?
Another is, will that effect program or database performance?

Sorry if same has already asked/posted.....

What I have tried:

btnEdit.Enabled = canDo("canEdit", userID)
btnDelete.Enabled = canDo("canDelete", userID)
btnCreate.Enabled = canDo("canCreat", userID)

cnxMenuEdit.Enabled = canDo("canEdit", userID)
cnxMenuDelete.Enabled = canDo("canDelete", userID)
.
.
.
.....and so on and so forth.

推荐答案

你的解决方案很好。



这通常由角色或用户完成。如果您担心过于频繁地访问数据库,那么您可以缓存权限,但如果几乎任何操作都有权限,那么最好只在form_onload中为该页面加载该用户/角色的权限集,就像你做的那样。



我确定还有其他聪明的方法可以做到这一点,但我对此没有任何疑问。
Your solution is fine.

This is usually done with roles or users. If you're worried about hitting the database too often then you can "cache" permissions, but if there are permissions for just about any action then it may be best to only load permission sets for that user / role for that page in the form_onload, as you have done.

I'm sure there are other clever ways of doing this but I have no issue with this.


这篇关于通过方法(功能)启用/禁用按钮/上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆