Closing form while opening

JFK

Member
Joined
Nov 28, 2005
Messages
20
Location
Czech Republic
Programming Experience
5-10
I need solve problem with my MDI and child forms. When user select (thru menu), which child form he wants to show, I would like to check, if user has permission for it. If not, dont want to show child form.

I'm not shure, that I am going the right way, but in VB6 it worked OK. Not in VB.NET.


1) I call frmChildForm.show from MDI.menu_onClick event.
2) in frmChildForm_Load i check for permissions and call Me.Hide for frmChildForm object
3) in frmChildForm_Activated I call Me.Close for frmChildForm object


After that, I receive error:
***************************
An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll
Additional information: Cannot call Close() while doing CreateHandle().
***************************
:confused:
What is wrong? Any ideas?

Any advice will be appreciated.

Regards. #JFK
 
Hey JFK, I do this simply by calling Me.Close in the form load event if the user doesn't have the correct permissions. This will only work however if you are creating a new instance of the form. I believe the Activated event is called every time the form receives focus so I wouldn't check there.
 
I tend to do the check (if I can) before the form even opens or loads. Or better yet, when the main MDI form opens, disable all menu items the user doesn't have access to.

-tg
 
i would handle this as techgnome suggested

you can simply make a sub that handles the checking of user rights and disabling the menu items that arnt allowed then just call this sub in the form's load event.

and if you include a way to change the user while the program is running, when the user is switched just have it call that sub again to set the menu allowances for that user

to disable menu items it's MenuName.Enabled = True/False
 
Thanks.

I'm trying to check for users rights and enabling/disabling menus, but I have new problem:

I have two kinds of MenuItems. First one are Menus generaly accessible for all users. Those are begining with "mnu". Second one are Menus, which I want to check for rights. Those are begining with "mnu1".

But HOW TO GO THROUGH ALL MENUS ON THE FORM AND CHECK FOR MENUITEM NAME?

In VB6 I could do smthing like this:

For Each Control in Me.Controls
if left(Control.Name,4) = "mnu1" then ...
Next Control

But in VB.NET I'm not able to get MenuItem.Name property! I can only get MenuItem.Text, but it is not enogh for me now.

Any suggestions?

Regards JFK
 
Can you show the code you do have? And instead of trying to access the Name property, try accessing the Tag property instead. It will mean that the tag property will also need to be set to the same as the Name property (or to some other identifier value). The advantage of the using the tag property is that you can loop through the controls and only process ones that have the Tag set.

I've done this successfully in an app.... I don't have it with me, or I'd share how I did it.

-tg
 
Tag property instead of Name property

I thought about using Tag property instead of Name property too, but it seems, that there isn't this property of MenuItem object. How can I set Tag property of MenuItem object?

my code:

VB.NET:
[SIZE=1]
[/SIZE][SIZE=1][COLOR=#0000ff][SIZE=1][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=1] Menu_pristupy()
[/SIZE][SIZE=1][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=1] Podmenu [/SIZE][SIZE=1][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=1] MenuItem
[/SIZE][SIZE=1][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=1] Menu [/SIZE][SIZE=1][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=1] Menu
[/SIZE][SIZE=1][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=1] Podmenu [/SIZE][SIZE=1][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=1].Menu.MenuItems
[/SIZE][SIZE=1][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=1] Prava_menu(Podmenu)
[/SIZE][SIZE=1][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=1] Podmenu
[/SIZE][SIZE=1][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
[SIZE=1][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=1] Prava_menu([/SIZE][SIZE=1][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=1] menu [/SIZE][SIZE=1][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=1] MenuItem)
[/SIZE][SIZE=1][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=1] submenu [/SIZE][SIZE=1][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=1] MenuItem
[/SIZE]

[SIZE=1][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=1] submenu [/SIZE][SIZE=1][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=1] menu.MenuItems[/SIZE][INDENT][SIZE=1][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=1] Prava_menu(submenu) [/SIZE][SIZE=1][COLOR=#008000]
[/COLOR][/SIZE][SIZE=1]MsgBox(podmenu.Text)
[/SIZE][SIZE=1][COLOR=#008000]'todo - if no rights then disable menu
[/COLOR][/SIZE][SIZE=1][COLOR=blue]If VB.Left(submenu.name, 4) = "mnu1" Then  [/COLOR]
[/SIZE][SIZE=1][COLOR=blue]submenu.Enabled = False[/COLOR]

[/SIZE][SIZE=1][COLOR=blue]If (mPrava.Overeni_menu(VB.Right(MyControl.Name, Len(MyControl.Name) - 4)) = True) Then [/COLOR][/SIZE][INDENT][SIZE=1][COLOR=blue]submenu.Enabled = True[/COLOR][/SIZE]

[/INDENT][SIZE=1]end if
[/SIZE][SIZE=1][COLOR=#008000][COLOR=blue]End If[/COLOR][/COLOR][/SIZE]

[/INDENT][SIZE=1][COLOR=#008000]
[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=1][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/COLOR][/SIZE]

Thanks. #JFK
 
Yes, it seems, that I'll have to do that this way. Are you dynamicly creating whole menus from DB records, or only permissions (enabling/disabling) of menus? Can you show me some (abbreviated) example? Thanks. #JFK
 
Thanks a lot!

I very appreciate your goodwill. Your example application is clear. By my opinion, it is pity, that only property of MenuItem, that you can use for identify particular MenuItem object is its Text property. In VB6, i think, it was better.

Your sample code was helpful for me in another thing too. I learned how to use #Region directive ...


Thanks.

Regards #JFK
 
All my problems began, becouse I have to convert my workmate application (written in VB6) and improve it in VB.NET.

He (my workmate) has already made functions for checking users rights. But by my opinion, I can idenfity specific MenuItem object only at design time. Is there realy way, how to get MenuItem object by its Name property (NOT Text, but Name!!!)???

I know, that I can write something like this:

MenuItem1.Enabled = False.

But I need go throug all MenuItems in MainMenu and for each ask for Name and then decide, if it can be Enabled, od not.

It seems, I have already solve this problem. I made my own MenuItem class, which has new property. In this property I'll store neccesary information for rights...


Regards. # JFK
 
I am glad that you resolved your problem.
Btw, notice that you can identify the menuitem by its index too ... but i do not see the difference and moreover, i don't see a reason why you don't want to use its text property as identifier (would you mind to explain why you don't like this approach). I'm using it all the time and i've never any problem :)
 
I cant use Text property, because I am using more than one MenuItems with the same Text value. Of course, I can use another Text for another MenuItems, but it is a limitation for me and I dont like it, because it is not a system solution. First of all I cant understand, why I'm not able to make:

dim submenu as MenuItem
for each submenu in MainMenu.MenuItems
msgbox submenu.Name
next submenu

????

In previous versions of VB was MenuItem as a member of Form.Controls collection and everything was OK....


Regards #JFK
 
Back
Top