Sturgelose
Sturgelose

Reputation: 163

How to write Shape's properties in Visio C#?

well, I've been trying to create a new custom property in a shape and I somehow managed, however, when I try to change the name of the Label I can only write numbers. Could you provide me how to do it in C# or maybe in VB so I can get a hint?

My code is:

//First I create the row
shape.AddRow((short)VisSectionIndices.visSectionProp,(short) (iRow + 1), (short) VisRowTags.visTagDefault);

//And now I try to write the Label
shape.CellsSRC[(short)VisSectionIndices.visSectionProp, (short)(iRow + 1), (short)VisCellIndices.visCustPropsLabel].Result[VisUnitCodes.visNoCast] = 123456789

However, when the Result method only accepts boolean as input, and I don't know how to write a string overthere...

Thanks in advance!

Upvotes: 2

Views: 3050

Answers (3)

Ilan Bar
Ilan Bar

Reputation: 11

You can use the following code:

private void AddCustomProperty(Microsoft.Office.Interop.Visio.Shape shape, string PropertyName, String propertyValue)
        {
            try
            {

                short customProps = (short)VisSectionIndices.visSectionProp;      

                short rowNumber = shape.AddRow(customProps, (short)VisRowIndices.visRowLast, (short)VisRowTags.visTagDefault);

                shape.CellsSRC[customProps, rowNumber, (short)VisCellIndices.visCustPropsLabel].FormulaU = "\"" + PropertyName + "\"";

                shape.CellsSRC[customProps, rowNumber, (short)VisCellIndices.visCustPropsValue].FormulaU = "\"" + propertyValue + "\"";


            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Error: " + e.Message);
            }

        }

Upvotes: 1

Jon Fournier
Jon Fournier

Reputation: 4327

The Result isn't meant to be read/write. What you want to do is set the FormulaU property of the cell to the label name. The Result property just calculates the formula for the cell and returns the result, which is why you have to provide a unit for the return value.

Also, the AddRow method returns the actual row number for the added row, which isn't necessarily the row you specified. For shapesheet sections with nameable rows, like the Custom Properties section, Visio may ignore the row you requested and just stick it at the bottom.

Upvotes: 0

Todd
Todd

Reputation: 139

I've also been looking into how to set the string value of a custom shape data property. Just got it to work like this:

var newPropertyValue = "cool new value";
tstShape.Cells["Prop.SuperCustomPropertyName"].FormulaU = "\"" + newPropertyValue + "\"";

Disclaimer that I am no expert with Visio Automation, but it works in my circumstance. I'm using visio 2010 and studio 2010 Hopefully it helps.

Upvotes: 2

Related Questions