Question

I wanted to create Custom ToolStripItem that will have checkbox and Icon in it. I menage to do that by creating Custom Control with two picturebox and label. Left picturebox if for icon and right is acting as special checkbox to add position to favorites.

I added that control to ToolStripControlHost.

  1. The problem is the white spaces on left and right I wan't to remove them or move icon to left bar and remove right space. I tried to set Padding and Margin values to 0 but it doesn't work. I found post with similar problem but no were the correct answer. Is it possible to do that??

enter image description here

  1. I want to make whole line highlight on MouseEnter just like normal elements in ToolStripMenuItem. When I override OnMouseEnter and OnMouseLeave changing background proprieties it just change the color of hosted control not antirie ToolStripControlHost. Is it possible to simulate the same behavior as ToolStripMenuItem?

Right now it looks like Picture 'A' and I want it to look more like picture B but with star shape checkbox:

enter image description here

Basically i want to make My CustomToolStrip item as similar to ToolStripMenuItem as possible. With separate events for Click (if you click on text) and ChackChange (if you Click on star)

Any ideas how to do that?

That's part of My ToolStripControlHost Code:

    private void AddEvents()
    {
        this.ToolStripICItemControl.eMouseEnter += new EventHandler(On_MouseEnter);
        this.ToolStripICItemControl.eMouseLeave += new EventHandler(On_MouseLeave);
    }
    private void AutoSizeControl(Control control, int textPadding)
    {
        // Create a Graphics object for the Control.
        Graphics g = control.CreateGraphics();

        // Get the Size needed to accommodate the formatted Text.
        Size preferredSize = g.MeasureString(
           control.Text, control.Font).ToSize();

        // Pad the text and resize the control.
        this.Size = new Size(
           preferredSize.Width + 5 + 50 + (textPadding * 2),
           this.Size.Height /*+ (textPadding * 2)*/ );

        // Clean up the Graphics object.
        g.Dispose();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
    }
    protected override void OnSubscribeControlEvents(Control c)
    {
        // Call the base so the base events are connected. 
        base.OnSubscribeControlEvents(c);

        // Cast the control to a  ToolStripCheckBox control.
        ToolStripImageAndCheckBox checkBoxToolStrip = (ToolStripImageAndCheckBox)c;

        // Add the event.
        checkBoxToolStrip.LabelClick += new EventHandler(checkBoxToolStrip_LabelClick);
        checkBoxToolStrip.CheckedChanged += new EventHandler(checkBoxToolStrip_CheckedChanged);
    }
    protected override void OnUnsubscribeControlEvents(Control c)
    {
        // Call the base method so the basic events are unsubscribed. 
        base.OnUnsubscribeControlEvents(c);

        // Cast the control to a ToolStripCheckBox control.
        ToolStripImageAndCheckBox checkBoxToolStrip = (ToolStripImageAndCheckBox)c;

        // Remove the event.
        checkBoxToolStrip.LabelClick -= new EventHandler(checkBoxToolStrip_LabelClick);
    }
    protected override void OnMouseEnter(EventArgs e)
    {
        DefaultBackColor = this.BackColor;
        base.OnMouseEnter(e);
        this.BackColor = Color.Aquamarine;
    }
    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        this.BackColor = DefaultBackColor;
    }
    void checkBoxToolStrip_LabelClick(object sender, EventArgs e)
    {
        if (Click != null)
            Click(this, e);
    }
    void checkBoxToolStrip_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckedChange != null)
            CheckedChange(this, e);
    }
    void On_MouseLeave(object sender, EventArgs e)
    {
        OnMouseLeave(e);
        //throw new NotImplementedException();
    }
    void On_MouseEnter(object sender, EventArgs e)
    {
        this.Select();
        this.Focus();
        OnMouseEnter(e);
    }
Was it helpful?

Solution

I menage to do what I needed by driving MyControl from ToolStripMenuItem and override onPaint method

public class MyControl : ToolStripMenuItem
{
    public MyControl(){}
    public MyControl(string text, Image image ):base (text,image){}
    public MyControl(string text):base(text){}
    public MyControl(Image image):base(image){}
    public MyControl(string text,Image image,EventHandler onClick):base(text,image,onClick){}
    public MyControl(string text, Image image,int id):base(text,image){  this.ID = id;}
    public MyControl(string text,Image image,int id,EventHandler onClick):base(text,image,onClick){this.ID = id; }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (base.Checked == false)
        {
           Rectangle rect = new Rectangle(this.Width - 20, 1, 20, 20);               
            e.Graphics.DrawImage(Properties.Resources.BlackStar, rect);
        }
        else
        { 
            Rectangle rect = new Rectangle(this.Width - 20, 1, 20, 20);
            e.Graphics.DrawImage(Properties.Resources.YellowStar, rect);
        }
    }
    public int ID { get; set; }
    public bool StarClicked { get; set; }

    protected override void OnMouseDown(MouseEventArgs e)
    {            
        StarClicked = e.X > (this.Width - 20);
        if (StarClicked)
        {
            this.Checked = this.Checked == true ? false : true;
        }
        else
            base.OnClick(e);
        base.OnMouseDown(e);
    }

that's how it looks right now

http://i.stack.imgur.com/ZwESK.png

The form code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        AddMenuElements(7);
    }
    public void AddMenuElements(int licznik)
    {
        ToolStripMenuItem wszystkie = new ToolStripMenuItem("wszystkie", SystemIcons.Question.ToBitmap());
        manu1ToolStripMenuItem.DropDownItems.Add(wszystkie);
        menuStrip1.Renderer = new ToolStripSystemRenderer();
        for (int i = 0; i < licznik; i++)
        {
            ToolStripMenuItem element = new ToolStripMenuItem(String.Format("element {0}", i), GetIcon(i),element2_Click);
            element.Visible = i % 2 == 0 ? false:true;                
            manu1ToolStripMenuItem.DropDownItems.Add(element);
        }


        for (int i = 0; i < licznik; i++)
        {
            MyControl element2 = new MyControl(String.Format("element {0}",i), GetIcon(i),element2_Click);
            element2.ID = i;
            element2.CheckedChanged += (s, e) =>
            {                    
                MyControl control = s as MyControl;
                manu1ToolStripMenuItem.DropDownItems[control.ID + 1].Visible = control.Checked;                  
            };
            element2.Checked = i % 2 == 0 ? false : true;

            wszystkie.DropDownItems.Add(element2); 

        }
    }
    void element2_Click(object sender, EventArgs e)
    {
        MyControl kontener = (sender as MyControl);

        if (kontener.ForeColor == Color.Red)
            kontener.ForeColor = Color.Black;
        else
            kontener.ForeColor = Color.Red;

    }
    private static Image GetIcon(int i)
    {
        Image ikona = null;
        switch (i)
        {
            case 0: ikona = SystemIcons.Application.ToBitmap();
                break;
            case 1: ikona = SystemIcons.Asterisk.ToBitmap();
                break;
            case 2: ikona = SystemIcons.WinLogo.ToBitmap();
                break;
            case 3: ikona = SystemIcons.Exclamation.ToBitmap();
                break;
            case 4: ikona = SystemIcons.Hand.ToBitmap();
                break;
            case 5: ikona = SystemIcons.Warning.ToBitmap();
                break;
            default: ikona = SystemIcons.Shield.ToBitmap();
                break;
        }
        return ikona;
    }   
}

Basically it does What I want I just need to stop it from disappearing when clicked on star. Thanks for Help @LarsTech it gave me a hint how to do that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top