Results 1 to 15 of 15

Thread: Enabling menu items on another form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159

    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

  2. #2
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    Why are you declaring frmMainMenu again in your code? I think the following will work...

    VB Code:
    1. Private Sub frmEmployee_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
    2.  
    3.            frmMainMenu.MenuItemEmployeeInfo.Enabled = True
    4.  
    5.     End Sub

    Regards,

    Prakash

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159
    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

  4. #4
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    Instead of Closed event, try using Closing event.

    Still I dont understand why you want to declare your menu item !!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159
    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

  6. #6
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    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:
    1. module module1
    2.  class myapp
    3.   public  fmain as frmMainMenu
    4.   public property frmMainMenu() as frmMainMenu
    5.    get
    6.     return fmain
    7.    end get
    8.    set(byval value as frmMainMenu)
    9.     fmain =value
    10.    end get
    11.   end property
    12.  end class
    13. end module

    then in the form_load of frmMainMenu
    write this line
    VB Code:
    1. module1.myapp.fmain = me


    you can now reffer from any form, to any control or property of frmMainMenu by calling :
    VB Code:
    1. module1.myapp.fmain.[control].[property] 'like module1.myapp.fmain.textbox1.text = "some text"
    2. 'or just
    3. module1.myapp.fmain.[property] ' like module1.myapp.fmain.backcolor  = system.drawing.color.white
    4.  
    5. 'in your example this would be
    6. 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:
    1. 'declare the frmemployee variable in the  form from which you are opening it:
    2. friend withevents employeeInfo as frmemployee
    3. 'then on menuclick use this code:
    4. Private Sub MenuItemEmployeeInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    5.  Handles MenuItemEmployeeInfo.Click
    6.  if employeeinfo is nothing orelse employeeinfo.isdisposed then
    7.   employeeinfo = new frmeployee
    8. 'make the new form mdichild:
    9.   employeeinfo.MdiParent = Me 'leave the .owner = me line, as .mdiparent suffices
    10.  end if
    11. 'the form is shown regardless if it has been disposed previously and recreated ,
    12. ' or newly created, or if the form is just hidden
    13.  employeeinfo.show()
    14. end sub
    disableing the menuitem is not a pretty elegant thing to do:
    take a look at other windows applications.

    hope this helped.
    ehmm...

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159
    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

  8. #8
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    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:
    1. 'declare the frmemployee variable in the  form from which you are opening it:
    2. friend withevents employeeInfo as frmemployee
    3. 'then on menuclick use this code:
    4. Private Sub MenuItemEmployeeInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    5.  Handles MenuItemEmployeeInfo.Click
    6.  if employeeinfo is nothing orelse employeeinfo.isdisposed then
    7.   employeeinfo = new frmeployee
    8. 'make the new form mdichild:
    9.   employeeinfo.MdiParent = Me 'leave the .owner = me line, as .mdiparent suffices
    10.  end if
    11. 'the form is shown regardless if it has been disposed previously and recreated ,
    12. ' or newly created, or if the form is just hidden
    13.  employeeinfo.show()
    14. end sub
    disableing the menuitem is not a pretty elegant thing to do:
    take a look at other windows applications.

    hope this helped.
    ehmm...

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159
    oh i get it now

    cheers mate makes sense now

    thanks

  10. #10
    Lively Member mindloop's Avatar
    Join Date
    Mar 2004
    Posts
    64
    np
    ehmm...

  11. #11
    Addicted Member Codehammer's Avatar
    Join Date
    Aug 2004
    Posts
    164

    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

  12. #12
    New Member
    Join Date
    Sep 2004
    Posts
    11
    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.

  13. #13
    Addicted Member Codehammer's Avatar
    Join Date
    Aug 2004
    Posts
    164

    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

  14. #14
    Addicted Member Codehammer's Avatar
    Join Date
    Aug 2004
    Posts
    164

    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

  15. #15
    New Member
    Join Date
    Sep 2004
    Posts
    11
    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
  •  



Click Here to Expand Forum to Full Width