Articles in this section
Category / Section

How to maintain different text for TabHeader in WinForms TabbedMDIManager and Item of ToolStripMenu in MenuStrip?

2 mins read

TabHeader text

By default, if we add any control as MDI Child that will be added as ToolStripMenuItem and the text of the Form will be displayed as text of the ToolStripMenuItem. But we can modify the text of ToolStripMenuItem while opening the subitems of ToolStripMenu. The following code example illustrate the same,

DropDownOpening event will be triggered while opening the drop down of ToolStripMenu and we must change text of the tool strip menu item here,

C#

private void Form1_Load(object sender, EventArgs e)
{  
  Test2 Form1 = new Test2();
  Form1.Text = "Tab1";
  Form1.Tag = "Location 1";
  Form1.MdiParent = this;
  Form1.Show();
 
  Test3 Form2 = new Test3();
  Form2.Tag = "Location 2";
  Form2.Text = "Tab2";
  Form2.MdiParent = this;
  Form2.Show();
  
  Test4 Form3 = new Test4();
  Form3.Text = "Tab3";
  Form3.Tag = "Location 3";
  Form3.MdiParent = this;
  Form3.Show();
}
 
private void DropDownOpening(object sender, EventArgs e)
{
  int i = 1;
  foreach (System.Windows.Forms.ToolStripItem item in WindowsToolStripMenuItem.DropDownItems)
  {
    ToolStripMenuItem toolStripItem = item as ToolStripMenuItem;
    if(toolStripItem != null && toolStripItem.IsMdiWindowListEntry)
    {
      if(toolStripItem != null)
      {
        string text_Renamed = toolStripItem.Text;
        string removedtext = text_Renamed.Remove(0, 3);
        if(!String.IsNullOrEmpty(removedtext))
        {
          string location_Renamed = LocationText(removedtext).ToString();
          toolStripItem.Text = text_Renamed + " " + location_Renamed;
        }
      }
      i = i + 1;
    }
  }
}
 
private object LocationText(string text)
{
  foreach (Form form in Application.OpenForms)
  {
    if(form.Text == text)
    {
      return form.Tag;
    }
  }
  return text;
}

Screenshots:

The following screenshot illustrates that the Text of the Form will be displayed as Text of the ToolStripMenuItem,

Text of form will be displayed as text of ToolStripMenuItem

The below screenshot is the output of this article,

Text of form will be displayed as text of ToolStripMenuItem

Samples:

VB: https://www.syncfusion.com/downloads/support/directtrac/general/ze/TabbedMDI_VB61062714.zip

C#: https://www.syncfusion.com/downloads/support/directtrac/general/ze/TabbedMDI_CSharp-136252931.zip

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied