Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,901 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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:
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
Solved. Used rectangle instead of bounds.
Thx.