By Randy Jean - 4/17/2008
Scenario:
MDI application where in my launchform method I have:
Private Sub LaunchForm(ByVal FormType As System.Type)
'-- Establish Locals
For Each f As Form In Me.MdiChildren
If Not String.IsNullOrEmpty(f.Name) Then
If f.Name.ToUpper = FormType.Name.ToUpper Then
MessageBox.Show("Form """ & f.Text.Trim() & """ already running.")
Exit Sub
End If
End If
Next
Dim loForm As Form
'-- Create the form
Try
loForm = CType(Activator.CreateInstance(FormType), Form)
'-- Set the MDI parent
loForm.MdiParent = Me
'-- Add a handler to the form close
' AddHandler loForm.FormClosed, AddressOf HandlefFormClosed
'-- Show the form
loForm.Show()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
As we do not want users to be able to launch the same form twice. This works well except when a user does not have access to a form based on the ViewSecurityKey and then another user signs in after a call to SessionLock.LockSession using F11 - this new user may have access to the same form the other user did not, but when they try to launch it it says it's already running because its still in the MDIChildren collection even though it is not visible anymore - So, when a user gets "access denied" what really happens to the form as it doesn't appear to completely get released.
Thanks,
Randy
|
By Trent L. Taylor - 4/17/2008
Test the security key in the LaunchForm method so that you can determine if the user has the permissions prior to ever showing the form. As for the form hanging around, we do not close the form, we place a locked panel on the form. So if the form is going away you have other logic that is causing this to happen. You can see what I am talking about in the Role Based Security sample that comes with the framework.
|
By Randy Jean - 4/17/2008
Trent L. Taylor (04/17/2008) Test the security key in the LaunchForm method so that you can determine if the user has the permissions prior to ever showing the form.
I will try this. Thanks.
As for the form hanging around, we do not close the form, we place a locked panel on the form.
Well, the form is not already active or visible in this case. User 1 does not have access, tries to launch. Gets "Access Denied" (not my code- coming from framework) Form never shows up. Thats good. User 2 comes up, hits F11 and logs in with different credentials. User 2 has access to same form User 1 was denied access to. Tries to launch and my code in the launchform method tells them its already running. Odd thing is, if I set a breakpoint and check the form properties of "f" object it shows Visible = True, etc. But it is nowhere to be seen. I get what your saying about the locked panel and that is cool assuming User 1 hits F11 when User 2 already has form running. That's a good thing and I assume User 1 can then close the locked form.
Anyway, your first suggestion may solve the problem and make this moot so I'll try that.
Thanks,
Randy
|
By Edhy Rijo - 4/17/2008
Hi Randy,I use a different version of the LaunchForm which check if the form exist in the MDI collection and then will bring it to front, it may help you out in your situation, but I am not sure Private Sub LaunchForm(ByVal FormType As System.Type) '-- Establish Locals Dim loForm As Form '-- Create the form loForm = CType(Activator.CreateInstance(FormType), Form) '-- Check if the form exist in the MDI collection and if so activate it. For Each currentForm As Form In Me._MDI.Controls If currentForm.Name = loForm.Name Then '-- IF the form is minimized then restore it. If currentForm.WindowState = FormWindowState.Minimized Then currentForm.WindowState = FormWindowState.Normal End If '-- Show the form on top of the others. currentForm.BringToFront() Exit Sub End If Next '-- Set the MDI parent loForm.MdiParent = Me '-- Show the form loForm.Show() End Sub
|
By Randy Jean - 4/17/2008
Thanks Edhy,
This may be something I can use. I originally was not displaying a message but they asked for a message saying form was already running. But bringing it to front makes sense if you're not displaying a message.
So, I'm still trying to figure out how to generically check the security on a form before I show it. Seems I can check the specific permission if I know its name but I hate hard-coding that somewhere. I think the ViewSecurityKey is a hidden/private property.
So, my other question is: how the heck do I put code in here so it still looks like code with coloring, etc. I just used the code tag but obviously that didn't work well...
Thanks,
Randy
|
By Edhy Rijo - 4/17/2008
Randy Jean (04/17/2008)
So, my other question is: how the heck do I put code in here so it still looks like code with coloring, etc. I just used the code tag but obviously that didn't work well... Use the [codesnippet][/codesnippet] link from the left of the message panel and for the tabs I manually add 5 spaces to format it nicely. I copy the code from the VS IDE and that is pasted here with the colors.
|
By Randy Jean - 4/17/2008
Thanks Edhy. OK, I figured out how to test the ViewSecurityKey in a generic way before showing the form. Its working great! Any thoughts on if there is a better way are appreciated.
loForm = CType(Activator.CreateInstance(FormType), Form)
If TypeOf loForm Is MicroFour.StrataFrame.UI.Windows.Forms.StandardForm Then
If SecurityBasics.CurrentUser.GetPermission(CType(loForm, _
MicroFour.StrataFrame.UI.Windows.Forms.StandardForm).ViewSecurityKey.Trim()).Action = _
PermissionAction.Deny Then
MessageBox.Show("Access Denied", "Access has been denied to " & loForm.Text.Trim() & ".", _
MessageBoxButtons.OK, MessageBoxIcon.Stop)
Exit Sub
End If
End If
Still can't figure out why my code looks so bad when I copy/paste. I'm using the codesnippet tags now.
|
By Trent L. Taylor - 4/17/2008
It depends on how you have your IDE setup to copy from the code window. However, the CodeSnippet tag just strips formatting with a PRE tag so that we don't have other tags bring down the forum...this happened a while back.
|
|