Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating menu using ContextMenuStrip and ToolStripMenuItem classes. I do not see how to create multiline text menu.

Any idea how to do it in winform ?
Posted

I see no way to enter it in Visual Designer, however
C#
Menu1Item1.Text = "Menu 1 Line 1\nMenu 1 Line 2"

works just fine.

So what you could do is enter a special character (say '#') in Visual Designer, then have some code fixing the texts; this takes recursion, like so:

C#
private void Form1_Load(object sender, EventArgs e) {
    foreach (ToolStripItem tsi in MainMenuStrip.Items) {
        fixToolStripMenuNewLines(tsi);
    }
}
private void fixToolStripMenuNewLines(ToolStripItem tsi) {
    log("tsi="+tsi.Text);
    ToolStripMenuItem tsmi=tsi as ToolStripMenuItem;
    if (tsmi!=null) {
        foreach (ToolStripItem tsi2 in tsmi.DropDownItems) {
            log("tsi2="+tsi2.Text);
            tsi2.Text=tsi2.Text.Replace("#", "\n");
            fixToolStripMenuNewLines(tsi2);
        }
    }
}



:)
 
Share this answer
 
I haven't tried this in C#, but in VB this can be achieved by coding this, rather than in the designer. simply insert a CRLF in the text. You could do this in the form load event.

e.g.

Menuitem1.text = "Menu 1 Line 1" & vbcrlf & "Menu 1 Line 2"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900