1

I would like to add my own class to ToolStripMenuItem object in windows forms.

According to this question: Custom ToolStripItem I tried this:

using System.Windows.Forms;
// ...
public class CultureItem : ToolStripItem
{
    public new string Text { get; set; }
    public new string Name { get; set; }
    public CultureInfo CultureInfo { get; set; }
}
// ...
public partial class View : Form
{
    public View()
    {
        var item = new CultureItem
        {
            Text = "Italy", Name = "IT", CultureInfo = new CultureInfo("it-IT")
        };
        languageToolStripMenuItem.DropDownItems.Add(item);
    }
}

and

using System.Windows.Forms;
// ...
public class CultureItem : ToolStripMenuItem
{
    public new string Text { get; set; }
    public new string Name { get; set; }
    public CultureInfo CultureInfo { get; set; }
}
// ...
public partial class View : Form
{
    public View()
    {
        var item = new CultureItem
        {
            Text = "Italy", Name = "IT", CultureInfo = new CultureInfo("it-IT")
        };
        languageToolStripMenuItem.DropDownItems.Add(item);
    }
}

Using code above I do not get any errors but ToolStripMenuItem show one value but with no text.

mikolaj semeniuk
  • 2,030
  • 15
  • 31
  • Add the Constructors you think you need to initialize your ToolStripItems and call `base()` passing the expected standard values (the `Text` and `Name`, for example). – Jimi Jan 30 '22 at 23:04
  • It should look something like this: [ToolStripLabel not updated with application settings using PropertyBinding](https://stackoverflow.com/a/65580860/7444103) -- That extends ToolStripLabel, which derives from ToolStripItem. It also adds the custom ToolStripItem to the ToolStripItems selector. – Jimi Jan 30 '22 at 23:14
  • 1
    Why do you create `new` properties for `Text` and `Name`? Just remove them from your `CultureItem` class. The base class has both properties. – Reza Aghaei Jan 31 '22 at 00:04

1 Answers1

1

Just use:

class CultureItem : System.Windows.Forms.ToolStripMenuItem
{
     public CultureInfo CultureInfo { get; set; }
}

The Name and Text members will be there.