Help removing menu item.

I have code that scans a config file and when it finds a certin line it adds a new drop down menu item to File --> Configure --> "New Item Added"

For example in the config I have a line that equals:

Test = "Sample1"

When the program loads and find the line with Test = "Sample1" it will add that item to the drop down menu under File --> Configure --> Sample1

this works perfectly. Now I also have an option to delete this item out of the config file and I am trying to get it to remove from the drop down menu. When you click the delete option you get a dialog box with a drop down that lists the items you can delete from the drop down menu that were loaded from the config file. When you select the item you want to delete and click the delete button I can remove the line from the config but cannot figure out how to remove it from the drop down menu. This is the code I have but wont work so its commented out:

Form1.ConfigureToolStripMenuItem.DropDownItems.Remove(Me.ChainDelete.Text)

With the line above I get the error Error: Value of type 'String' cannot be converted to 'System.Windows.Forms.ToolStripItem'.


ChainDelete = the drop down box that is in the dialog that comes up when you click on the delete option.

ConfigureToolStripMenuItem = Menu bar on the main form that is loaded when opening the program.

I am not sure how/what this needed to be converted to to have it remove from the drop down menu. If you need more info let me know.



Answer this question

Help removing menu item.

  • Chintan Jhaveri

    I use text constants for supplying the text and for comparisons. Use of constants precludes case errors.



  • Nilay

    ok ill rephrase that. Thanx EVERYONE for the help. :-P

  • Paarul

    You're welcome and weveryone here is not a guy.



  • maurocam

    it would be recommended to use the index rather than the text chosen - text can be in upper/lower cases so it may not deleted - having the index chosen and removing the index is better overall and is best practice to do so - so try to get the index chosen on the item clicked to remove, if at all possible, using the RemoteAt() method.

  • Magicboy

    Thanx for the help guys. I got it working perfect with your help :)

  • PublicError

    I think you are going a have to read the dropdown menu item collection and find the item.

    It'll look something like this:

    For each itm as System.Windows.Forms.ToolStripItem in ConfigureToolStripMenuItem.DropDownItems

    if itm.text = "<your text>" then

    ConfigureToolStripMenuItem.DropDownItems.Remove(itm)
    itm.dispose

    exit for

    end if

    next

    And your done. You may have to use .name instead of .text



  • Help removing menu item.