|
-
Mar 30th, 2004, 11:59 AM
#1
Thread Starter
Addicted Member
Enabling menu items on another form
when i close a form i want to enable a menu item
i have attempted to do this with the code below, but each time i try and close the form i keep getting an error
"Object reference not set to an instance of an object"
can any one tell me where im going wrong?
Code:
Private Sub frmEmployee_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Dim frmMainMenuItem As frmMainMenu
frmMainMenuItem.MenuItemEmployeeInfo.Enabled = True
End Sub
here's my code when the user clicks the menu item on another form.
Code:
Private Sub MenuItemEmployeeInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemEmployeeInfo.Click
Dim frmEmployeeOpen As New frmEmployee
frmEmployeeOpen.Owner = Me
frmEmployeeOpen.MdiParent = Me
MenuItemEmployeeInfo.Enabled = False
frmEmployeeOpen.Show()
End Sub
-
Mar 31st, 2004, 10:43 AM
#2
Fanatic Member
Why are you declaring frmMainMenu again in your code? I think the following will work...
VB Code:
Private Sub frmEmployee_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
frmMainMenu.MenuItemEmployeeInfo.Enabled = True
End Sub
Regards,
Prakash
-
Apr 1st, 2004, 07:28 AM
#3
Thread Starter
Addicted Member
Let me explain in detail
I have a Form called frmMainMenu this has a menu item called Employee information. when i click this it will disable the menu item employee information and load the frmEmployee form
When i have finished with the frmEmployee form i want to enable the menu item Employee information when the frmEmployee form closes.
so the code behind the frmMainMenu, Menu item 'Employee Information' is:
Code:
Private Sub MenuItemEmployeeInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemEmployeeInfo.Click
Dim frmEmployeeOpen As New frmEmployee
frmEmployeeOpen.Owner = Me
frmEmployeeOpen.MdiParent = Me
MenuItemEmployeeInfo.Enabled = False
frmEmployeeOpen.Show()
End Sub
and the code behind the frmEmployee is:
Code:
Private Sub frmEmployee_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Dim frmMainMenuItem As frmMainMenu
frmMainMenuItem.MenuItemEmployeeInfo.Enabled = True
End Sub
hope this helps
Thanks
-
Apr 1st, 2004, 07:33 AM
#4
Fanatic Member
Instead of Closed event, try using Closing event.
Still I dont understand why you want to declare your menu item !!
-
Apr 1st, 2004, 08:25 AM
#5
Thread Starter
Addicted Member
i'm not declaring the main menu item im declaring the name of the new main menu as frmMainMenuItem
if i dont declare this i cant view the objects in the frmMainMenu
-
Apr 2nd, 2004, 06:35 AM
#6
Lively Member
the problem in your code is that you are not setting refference to your original form.
this you can do by pasting the following code in a class in a module:
VB Code:
module module1
class myapp
public fmain as frmMainMenu
public property frmMainMenu() as frmMainMenu
get
return fmain
end get
set(byval value as frmMainMenu)
fmain =value
end get
end property
end class
end module
then in the form_load of frmMainMenu
write this line
you can now reffer from any form, to any control or property of frmMainMenu by calling :
VB Code:
module1.myapp.fmain.[control].[property] 'like module1.myapp.fmain.textbox1.text = "some text"
'or just
module1.myapp.fmain.[property] ' like module1.myapp.fmain.backcolor = system.drawing.color.white
'in your example this would be
module1.myapp.fmain.MenuItemEmployeeInfo.Enabled = True
But why would you disable the menuitem while the frmemployee is shown ?
shouldn't you better call frmemployee by using frmEmployeeOpen.showdialog(me) method ?
this would assure you can't do anything in the mainform until you don't close frmEmployeeOpen .
Anyway, if you are just trying to make sure there is only one instance of frmemployee opened at a time,
just use this code to open the frmemployee, and don't use the first one :
VB Code:
'declare the frmemployee variable in the form from which you are opening it:
friend withevents employeeInfo as frmemployee
'then on menuclick use this code:
Private Sub MenuItemEmployeeInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MenuItemEmployeeInfo.Click
if employeeinfo is nothing orelse employeeinfo.isdisposed then
employeeinfo = new frmeployee
'make the new form mdichild:
employeeinfo.MdiParent = Me 'leave the .owner = me line, as .mdiparent suffices
end if
'the form is shown regardless if it has been disposed previously and recreated ,
' or newly created, or if the form is just hidden
employeeinfo.show()
end sub
disableing the menuitem is not a pretty elegant thing to do:
take a look at other windows applications.
hope this helped.
-
Apr 2nd, 2004, 07:14 AM
#7
Thread Starter
Addicted Member
hey mindloop
The reason why I’m disabling the menu items because my frmMainMenu is a MDIParent and I don’t want the user opening more than one instance of a form
E.g. I don’t want the user to be able to open more than one employee information form.
thanks
-
Apr 2nd, 2004, 07:22 AM
#8
Lively Member
read this again:
Originally posted by mindloop
shouldn't you better call frmemployee by using frmEmployeeOpen.showdialog(me) method ?
this would assure you can't do anything in the mainform until you don't close frmEmployeeOpen .
Anyway, if you are just trying to make sure there is only one instance of frmemployee opened at a time,
just use this code to open the frmemployee, and don't use the first one :
VB Code:
'declare the frmemployee variable in the form from which you are opening it:
friend withevents employeeInfo as frmemployee
'then on menuclick use this code:
Private Sub MenuItemEmployeeInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MenuItemEmployeeInfo.Click
if employeeinfo is nothing orelse employeeinfo.isdisposed then
employeeinfo = new frmeployee
'make the new form mdichild:
employeeinfo.MdiParent = Me 'leave the .owner = me line, as .mdiparent suffices
end if
'the form is shown regardless if it has been disposed previously and recreated ,
' or newly created, or if the form is just hidden
employeeinfo.show()
end sub
disableing the menuitem is not a pretty elegant thing to do:
take a look at other windows applications.
hope this helped.
-
Apr 2nd, 2004, 07:26 AM
#9
Thread Starter
Addicted Member
oh i get it now
cheers mate makes sense now
thanks
-
Apr 2nd, 2004, 07:29 AM
#10
Lively Member
-
Sep 15th, 2004, 06:22 AM
#11
Addicted Member
Same Problem
The Exact Same Question as Above, But I Dont want to have it just not open, I Want it totally disabled. I Dont Think it is Untidy
Curiosity SKILLED the cat
Google Talk from your Mobile phone
Chat from your mobile or get an emulator like J2ME Wireless Toolkit 2.2
-
Sep 15th, 2004, 06:33 AM
#12
New Member
If u want to disable a menuItem you can set its enabled property to False. If your problem is getting a reference to the form that contains the menu-item, then u can use 2 methods :
1.Keep a global variable that holds a reference to the Parent form (that contains the Menu). Be careful to set the reference (and later set it to nothing, to be safe).
2.Make the parent form pass a reference to itself in the constructor of the child form when the child form is created.
If I didnt understand your problem well or if u are still confused, please let us know.
-
Sep 15th, 2004, 08:44 AM
#13
Addicted Member
Is this Cool
Something like this:
Set the Property of the Menu item to Public.
or
On the parent write: Public ABC as mainmenu
ABC=menuitemEmployee
Then Somewhere in the Child if the State needs to Change Say
ABC.enabled=false
is doing this Dim a as int
Originally posted by pradeepc
If u want to disable a menuItem you can set its enabled property to False. If your problem is getting a reference to the form that contains the menu-item, then u can use 2 methods :
1.Keep a global variable that holds a reference to the Parent form (that contains the Menu). Be careful to set the reference (and later set it to nothing, to be safe).
2.Make the parent form pass a reference to itself in the constructor of the child form when the child form is created.
If I didnt understand your problem well or if u are still confused, please let us know.
Curiosity SKILLED the cat
Google Talk from your Mobile phone
Chat from your mobile or get an emulator like J2ME Wireless Toolkit 2.2
-
Sep 17th, 2004, 03:04 AM
#14
Addicted Member
Around?
You Still around man, The Above Message Shows What I Tried, Does Not work. Can You Give an Example, Please.
Originally posted by pradeepc
If u want to disable a menuItem you can set its enabled property to False. If your problem is getting a reference to the form that contains the menu-item, then u can use 2 methods :
1.Keep a global variable that holds a reference to the Parent form (that contains the Menu). Be careful to set the reference (and later set it to nothing, to be safe).
2.Make the parent form pass a reference to itself in the constructor of the child form when the child form is created.
If I didnt understand your problem well or if u are still confused, please let us know.
Curiosity SKILLED the cat
Google Talk from your Mobile phone
Chat from your mobile or get an emulator like J2ME Wireless Toolkit 2.2
-
Oct 4th, 2004, 04:01 AM
#15
New Member
Sorry pal, I was not around for some days.
1.Keep a global variable that holds a reference to the Parent form (that contains the Menu). Be careful to set the reference (and later set it to nothing, to be safe).
Eg:
Code:
'In a module
Public ParentForm as frmParent
'In constructor of frmParent
ParentForm = Me
'In frmChild
'Disable ith Menu Item
ParentForm.MainMenu1.MenuItems(i).Enabled= False
2.Make the parent form pass a reference to itself in the constructor of the child form when the child form is created.
Code:
'In top level declaration of childform
Dim ParentForm as frmParent
'Constructor of child form
Sub New(ByRef frm as frmParent)
ParentForm = frm
End Sub
'Create and showing child form from Parent Form
Dim ChildForm as new frmChild(Me)
ChildForm.Show()
'In frmChild
'Disable ith Menu Item
ParentForm.MainMenu1.MenuItems(i).Enabled= False
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|