Home > Mobile >  Extends ToolStripItem class in Windows Forms
Extends ToolStripItem class in Windows Forms

Time:02-01

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.

CodePudding user response:

Just use:

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

The Name and Text members will be there.

  •  Tags:  
  • Related