question

chrisharper-2006 avatar image
0 Votes"
chrisharper-2006 asked ·

Why does ToolStripMenuItem OnPaint draws only the first item?

Hello,
I'm trying to add white space within the item's text in a ContextMenu for that the string after the space is always left aligned and always stands under each other in each item.

What I'm trying to do in the following code:

  • I add the items to the ContextMenu and wants to make it invisible by the foreground and the selection foreground color.

  • I split the string in the XToolStripMenuItem class, calculate the width of the first string and add the padding to the 2nd string.

Problem: The OnPaint gets the 3 items but only draws the first item. I can't see why.

Showing Context menu:

     private void sfButton1_Click(object sender, EventArgs e)
     {
         XContextMenuStrip cms = new XContextMenuStrip();
         cms.ForeColor = cms.BackColor; // Todo later: change foreground selection color for that the text is invisible
    
         var item1 = new XToolStripMenuItem();
         var item2 = new XToolStripMenuItem();
         var item3 = new XToolStripMenuItem();
         item1.Text = "Test1, padded text";
            
         item2.Text = "Test2, padded text";
         item3.Text = "Test3, padded text";
    
         cms.Items.Add(item1);
         cms.Items.Add(item2);
         cms.Items.Add(item3);
         cms.Show(sfButton1, sfButton1.Location);// Todo later: Doesn't work properly (Not at button position)
     }

ContextMenuStrip class inheritance:

 internal class XContextMenuStrip : ContextMenuStrip
 {
     public XContextMenuStrip() 
     {
         this.AutoSize = false;
         this.Width = 301;
         this.Height = 300; // Todo later          
     }
 }


ToolStripMenuItem inheritance:

 class XToolStripMenuItem : ToolStripMenuItem
 {
     public XToolStripMenuItem() {
         this.AutoSize = false;
         this.Width = 300;
     }
    
     private static int count;
     protected override void OnPaint(PaintEventArgs e)
     {
         base.OnPaint(e);
    
         Font font = new Font("Segoe UI", this.Font.Size);
         SolidBrush brush = new SolidBrush(Color.IndianRed);//Todo later: logic for adding the white space between the splitted Text from "this.Text" by DrawString
    
         e.Graphics.DrawString("test",
                               font, 
                               brush,
                               this.Bounds.X + 34.4f, 
                               this.Bounds.Y);
    
         e.Graphics.DrawString("padded Text",
                              font,
                              brush,
                              this.Bounds.X + 195.4f,
                              this.Bounds.Y, null);
     }
 }


Regards

dotnet-csharpwindows-forms
10 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

chrisharper-2006 avatar image
0 Votes"
chrisharper-2006 answered ·

Solved. Used rectangle instead of bounds.
Thx.

10 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.