Excel addTextBox C#

03-19-2005, 15:56 Claus Nielsen

Hello all

I'm trying to add a textBox to a chart in C#.

I add a label/textBox, using the following command:

private Excel.Chart selfRestChart;
Excel.Shape shape =
(Excel.Shape)selfRestChart.Shapes.AddLabel(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
offset, offset, 0, 0);

But it doesn't allow me to cast it to a textBox.
I even tried:
Excel.Shape shape =
(Excel.Shape)selfRestChart.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
offset, offset, 0, 0);

And that won't allow me to cast it either:
Excel.TextBox textBox1 = (Excel.TextBox)shape;

I get the following error:
Fejl: System.InvalidCastException: Specified cast is not valid.

I tried printing the Type of the shape object:
Type: msoTextBox

So it's all a little confusing.

Can anybody help?
Thanks!

Regards


 

Re: Excel addTextBox C#

03-20-2005, 13:36 Claus Nielsen

Hello again

I found a solution to the problem myself :) Always the best way to learn :)

Anyways, here goes:
private Excel.Chart selfRestChart;
Excel.Shape shape =
(Excel.Shape)selfRestChart.Shapes.AddLabel(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
chartRestWidth-200, offset, 0, 0);
shape.Select(Type.Missing);
shape.TextFrame.AutoSize = true;
shape.TextFrame.Characters(Type.Missing, Type.Missing).Text = "text";
shape.TextFrame.Characters(Type.Missing, Type.Missing).Font.ColorIndex = 56;
shape.TextFrame.Characters(Type.Missing, Type.Missing).Font.Size = 12;
shape.TextFrame.Characters(Type.Missing, Type.Missing).Font.Bold = true;
shape.TextFrame.AutoSize = false;


This is what I add. When I print the type of the shape, it outputs:
msoTextBox. When I try to cast the shape object to a TextBox, I get an
illegal cast exception.

So then I tried going through the object properties of a Chart, and here's
what I've found:
selfRestChart.ChartArea.AutoScaleFont = false;
Excel.TextBoxes textBoxes =
(Excel.TextBoxes)selfRestChart.TextBoxes(Type.Missing);
textBoxes.Font.Size = 10;
textBoxes.Font.Name = "Verdana";
textBoxes.AutoScaleFont = false;

I can return all the textboxes in a chart, and set the fonts and so forth.
Since my textBoxes are all alike, this is exactly what I was looking for.

Regards


"Claus Nielsen" wrote in message
news:eYtmjPJLFHA.3404@TK2MSFTNGP15.phx.gbl...
> Hello all
>
> I'm trying to add a textBox to a chart in C#.
>
> I add a label/textBox, using the following command:
>
> private Excel.Chart selfRestChart;
> Excel.Shape shape =
> (Excel.Shape)selfRestChart.Shapes.AddLabel(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
> offset, offset, 0, 0);
>
> But it doesn't allow me to cast it to a textBox.
> I even tried:
> Excel.Shape shape =
> (Excel.Shape)selfRestChart.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
> offset, offset, 0, 0);
>
> And that won't allow me to cast it either:
> Excel.TextBox textBox1 = (Excel.TextBox)shape;
>
> I get the following error:
> Fejl: System.InvalidCastException: Specified cast is not valid.
>
> I tried printing the Type of the shape object:
> Type: msoTextBox
>
> So it's all a little confusing.
>
> Can anybody help?
> Thanks!
>
> Regards
>
copy from http://thedotnet.com/nntp/211222/showpost.aspx