Discussion:
set several menuitems.visible=true
(too old to reply)
handoyo
2010-01-19 16:21:55 UTC
Permalink
Hi all,i got menu like this


Master Transaksi

Karyawan -> name : mnuKaryawan Penjualan -> name : mnuJual
User -> name : mnuUser
--
Logout
Exit

I'm trying to show the mnuKaryawan etc based on user rights...

I use database to store the menu name,and the table like this

position menu
admin mnuKaryawan
admin mnuUser
user mnuJual

How to set the mnuKaryawan etc.visible=true ?

Thanks a lot....
Captain Jack
2010-01-19 16:57:12 UTC
Permalink
Post by handoyo
How to set the mnuKaryawan etc.visible=true ?
The Controls property of the Form class can be accessed by control name;
assuming that the menu objects are in the form's control collection, you
should be able to access them by name. Something like this:

<FormObject>.Controls(strMenuControlName).Visible =
boolVisibilityBasedOnRights

It should be error trapped, of course, just in case. You should be able to
call that on each pass through your table results.

--
Jack
handoyo
2010-01-19 17:18:18 UTC
Permalink
Post by Captain Jack
Post by handoyo
How to set the mnuKaryawan etc.visible=true ?
The Controls property of the Form class can be accessed by control name;
assuming that the menu objects are in the form's control collection, you
<FormObject>.Controls(strMenuControlName).Visible =
boolVisibilityBasedOnRights
It should be error trapped, of course, just in case. You should be able to
call that on each pass through your table results.
--
Jack
I'ved tried using that..It gives me error.. Object reference xxxxxxx....Is it
because i can't directly access ToolStripMenuItem.DropDownItems ? Thanks...
Captain Jack
2010-01-19 19:48:24 UTC
Permalink
Post by handoyo
I'ved tried using that..It gives me error.. Object reference xxxxxxx....Is it
because i can't directly access ToolStripMenuItem.DropDownItems ? Thanks...
Might be... it depends on where the menu objects are stored. At what level
are the menu objects being declared, and what control owns them? If you know
what control owns them, the menu items should appear in the control
collection for that object.

Why can't you access the DropDownItems though? You should be able to
enumerate through the collection, and set the visibility of any components,
if you know which item's DropDownItems property you need to look at.

If the variables pointing to the menu controls are visible to your class but
not in it's Controls collection, I suppose you could build your own
reference. Before you read for permissions, define a Dictionary(Of String,
Control), and add each menu control and it's name to the dictionary. Then
reference the dictionary to set the visibility. That's a little ungainly,
but it would probably work.

--
Jack
handoyo via DotNetMonster.com
2010-01-20 02:45:16 UTC
Permalink
I create the menu like this

menustrip -> name : mnu
ToolStripMenuItem : Sistem -> name MasterMenuItem
ToolStripMenuItem : Transaksi -> name TransaksiMenuItem

Under Sistem i create

ToolStripMenuItem : Karyawan -> name mnuKaryawan
ToolStripMenuItem : User -> name mnuUser

Under Transaksi i create

ToolStripMenuItem : Penjualan -> name mnuJual

I've tested using frmMain.Controls("mnuUser").visible=true

It doesn't work..It gives me error Object reference not set to an instance of
an object

Thanks...
--
Message posted via http://www.dotnetmonster.com
Captain Jack
2010-01-20 15:55:47 UTC
Permalink
Post by handoyo via DotNetMonster.com
I create the menu like this
menustrip -> name : mnu
ToolStripMenuItem : Sistem -> name MasterMenuItem
ToolStripMenuItem : Transaksi -> name TransaksiMenuItem
Under Sistem i create
ToolStripMenuItem : Karyawan -> name mnuKaryawan
ToolStripMenuItem : User -> name mnuUser
Under Transaksi i create
ToolStripMenuItem : Penjualan -> name mnuJual
I've tested using frmMain.Controls("mnuUser").visible=true
It doesn't work..It gives me error Object reference not set to an instance of
an object
The error would be because the menu items aren't being added to the Controls
collection for the form. You mentioned the DropDownItems collection
before... is that where you're adding the menu items? I'm not sure what you
mean by "Under Sistem i create"; is like this:

Sistem.DropDownItems.Add(Karyawan)

If so, you should be able to find the item in that collection instead of
controls. You can use the dictionary method, too. Before you start building
your menu, create a new Dictionary(Of String, ToolStripItem). As you add
each item to the menu, also add it to the dictionary:

MenuDictionary.Add(Karyawan.Name, Karayawan)

Then you can set your visible property from that, later:

MenuDictionary(strItemName).Visible = boolVisibilityByAuthority

You'll still want the dictionary call to have an error trap, though, because
of the possiblity that an item name is spelled wrong in the database.

--
Jack
handoyo via DotNetMonster.com
2010-01-20 16:33:09 UTC
Permalink
Thanks a lot Captain Jack,i found out a solution here

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/ca49f946-e604-4cf4-9e53-cf171ceca007/


Thanks a lot for helping me..May God bless you always.. ^_^
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/winform-controls/201001/1
Loading...