Pergunta

I have a ToolStrip object which has a few ToolStripButtons. I want to add a vertical button on the left side of this object. But since the ToolStrip is docked to the left, I can't seem to add anything on its left side.

Is there a way that this can be done? Thanks!

Note: I am trying to achieve something similar to the Visual Studio Toolbox, wherein if I hover over the button, the ToolStrip is displayed and if I leave the ToolStrip it hides. If there is another way to do this, please help.

Any advice would be greatly appreciated.

Foi útil?

Solução

Hoping that I understood your question correctly, you are trying to dock a button to the left of the ToolStrip Control as shown in the image below.

enter image description here

To achieve this you need to first set the Dock property of both the controls to Left

To have the button control as the first left docked control, you can achieve this by using the Document Outline window which is opened using the View > Other Windows > Document Outline menu and then setting the dock priority by dragging the button below the ToolStrip as shown in the image above.

To support multiple buttons you could replace the single button in the above sample, with a panel containing the needed buttons.

Also in order to avoid reinventing the wheel, you can have a look at the DockPanel Suite project that is available for free on SourceForge.

EDIT: As requested in comment.

You could link all the buttons that you want to support vertical text to the paint event as shown below

private void VerticalButtonTextEvent(object sender, PaintEventArgs e)
    {
        Button button = sender as Button;
        if (button == null) return;

        Graphics g = e.Graphics;
        g.FillRectangle(SystemBrushes.Control, button.ClientRectangle);
        using (Font f = new Font("Times New Roman", 8))
        {
            SizeF szF = g.MeasureString(button.Text, f);
            g.TranslateTransform(
                (float) ((Button) sender).ClientRectangle.Width/(float) 2 + szF.Height/(float) 2,
                (float) ((Button) sender).ClientRectangle.Height/(float) 2 -
                (float) szF.Width/(float) 2);
            g.RotateTransform(90);
            g.DrawString(button.Text, f, Brushes.Black, 0, 0);
        }
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top