Glue textbox to upper left corner ?

Started by tommy, July 08, 2009, 12:31:44 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

tommy

Hi,

let's say I have a textbox which I would like to glue to upper left corner of the parent.

I dream about something like TextBox.BeginX = Parent.BeginX + 0.5 (similar for Y coordinate).
Unfortunately I can't achieve it. I've managed (somehow) to glue based on X coordinate
but whenever I try to do the same for Y its just does not work. Textbox constantly goes up
or down whenever I resize the parent shape.


Any help in this area ?
Thanks,
Tomasz

wapperdude

#1
In the shapesheet for the textbox you need the following entries:

sheet.id! = the parent shape id

PinX = sheet.id!PinX  ==>  You may want to "guard" these formulae.
PinY = sheet.id!PinY
LocPinX = Width*0
LocPinY = Height*1

In the shapesheet for the parent:
LocPinX = Width*0
LocPiny = Height*1

There may be some shifting due to textbox resizing.  You could cure this by either changing the justification or setting the textbox width and height to be controlled by the text, rather than the shape.  See http://msdn.microsoft.com/en-us/library/aa200986(office.10).aspx


That should do it.
Wapperdude


Visio 2019 Pro

Visio Guy

#2
This article deals with a similar issue, although the shape is positioned relative to the page:

  - Number Your Pages Visually

Inside of a group, you would replace "ThePage" with "Sheet.N", "PageWidth" with "Width", "PageHeight" with "Height".

For info about getting shape's id, read this article (especially "Tip #8 Get Shape Names and IDs"):

  - Top 9 Reasons for Turning-on Developer Mode

More examples:

  - Design Web Pages With This Visio Breadcrumbs Shape

It would be nice to have ShapeSheet functions that positioned shapes the way Visual Studios "Anchor" and "Dock" properties work. Or perhaps have functionality similar to WPF's (Windows Presentation Foundation) various stack and flow panels, which automatically arrange their children in useful ways.
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

aledlund

An interesting problem that includes where on the page to put the data, where the data comes from, what happens if the page size changes, etc.
Maybe this code examples might help. This is what I use to place page data in the upper left of drawings. I have not included all of the subroutines since they come from the visio sdk (things like setting cell values)
HTH,
al



   


'
   ' pass in an arraylist of property names and values
   ' to add to the page. then create rectangles in the
   ' upper left with text fields that reference the
   ' custom properties that we just created
   '
   Public Sub AddArrayPropertyBoxesToPage _
           (ByVal visPage As Visio.Page, _
           ByRef arrInput As Variant)

       
       Dim arrProp() As String
       arrProp = arrInput

       Dim objRect As Visio.Shape
       Dim blnResult As Boolean

       Dim iEnd As Double
       Dim iTop As Double
       Dim iStart As Double
       Dim iBottom As Double

       ' start .75 from left side of page
       iStart = 0.75
       ' the text box is 3in long
       iEnd = iStart + 3

       Dim visCell As Visio.Cell
       Dim visPageSheet As Visio.Shape
       Set visPageSheet = visPage.PageSheet
       Dim intArr As Integer
       intArr = 0
       Dim strProperty As String
       strProperty = "prop."
       Dim strField As String
       Dim strSpacer As String
       strSpacer = " : "
       Dim strPage As String
       strPage = "ThePage!Prop."

           Dim pagPageSheet As Visio.Shape
           Set pagPageSheet = visPage.PageSheet

           If pagPageSheet.SectionExists(visSectionProp, False) = False Then
               pagPageSheet.AddSection (visSectionProp)
           End If

           For intArr = 0 To UBound(arrProp)

               ' where do we want the text
               ' assumes initial page size of 8.5in and we
               ' put it .5in below the top, each box is
               ' .2in high
               iTop = 8 - (intArr * 0.2)
               iBottom = iTop - 0.2


               strField = strProperty & arrProp(intArr, 0)
                   ' create and initialize the custom properties
                   ' we will always add a page id property to the page
                   AddCustomPropertyToShape _
                       visPageSheet, _
                       arrProp(intArr, 0), _
                       arrProp(intArr, 0), _
                       arrProp(intArr, 0), _
                       visPropTypeString, "", "", _
                       False, False, "page"
                   ' if they don't pass a page name then take the
                   ' name from the page properties
                   If LCase(arrProp(intArr, 0)) = "pageid" _
                       And arrProp(intArr, 1) = "" Then
                       Set visCell = visPageSheet.Cells("prop.pageid")
                       SetCellValueToString visCell, visPage.NameU
                   Else
                       ' anything other than a blank pageid is handled here
                       Set visCell = visPageSheet.Cells(strField)
                       SetCellValueToString visCell, arrProp(intArr, 1)
                   End If

                   ' let the drawing catch up
                   DoEvents

                   ' now create a box and put a text field into it
                   ' pointed at the custom property
                   Set objRect = visPage.DrawRectangle(iStart, iTop, iEnd, iBottom)
                   TextAntiScaling objRect, intArr
                   objRect.Cells("Char.Size").Formula = "9Pt" ' this was so we could put in a guid
                   blnResult = AddTextFieldToShape(objRect, _
                                   arrProp(intArr, 0) & strSpacer, _
                                   strPage & arrProp(intArr, 0), _
                                   visFmtStrNormal)
 
                   DoEvents
           Next intArr

   End Sub





   '*********************************************************************
   '*********************************************************************
   '
   '               Text Field
   '
   '*********************************************************************
   '*********************************************************************

   Public Function AddTextFieldToShape _
                       (ByVal visShape As Visio.Shape, _
                       ByVal strFieldPrompt As String, _
                       ByVal strProperty As String, _
                       ByVal fldFormat As Visio.VisFieldFormats) _
                       As Boolean


       Dim visText As Visio.Characters

       On Error GoTo AddTextField_err

           Set visText = visShape.Characters
           visText.Text = strFieldPrompt

           ' Set the character color of the text to blue.
           visText.CharProps(visCharacterColor) = visBlue

           ' Set the font size of the text to 9.
           visText.CharProps(visCharacterSize) = 9

           ' Start a new run that will contain the appended string with
           ' different formatting.
           visText.Begin = visText.End

           ' Add the field
           visText.AddCustomField strProperty, fldFormat

           ' Keep the same formatting as set for the previous string,
           ' except set the character style to bold italic.
           visText.CharProps(visCharacterStyle) = _
               visBold + _
               visItalic
           ' Set the character color
           visText.CharProps(visCharacterColor) = visBlack

           AddTextFieldToShape = True
           Exit Function

AddTextField_err:

           AddTextFieldToShape = False


   End Function



   '*********************************************************************
   '*********************************************************************
   '
   '   this is where we initialize the page text rectangles with user
   '    cells and then change the necessary cells so that if
   '    the page size changes the rectangles will remain as
   '   they appeared before the scale change
   '
   '*********************************************************************
   '*********************************************************************

   Public Sub TextAntiScaling _
               (ByVal objRect As Visio.Shape, _
               ByVal intFieldNr As Integer)

       Dim objCell As Visio.Cell
       Dim blnResult As Boolean

           objRect.Name = "textfield_" & CStr(intFieldNr)

           blnResult = AddUserPropertyToShape(objRect, "width", "width", "width", _
                   objRect.Cells("Width").ResultIU, "width")
           blnResult = AddUserPropertyToShape(objRect, "height", "height", "height", _
                   objRect.Cells("height").ResultIU, "height")
           blnResult = AddUserPropertyToShape(objRect, "pinX", "pinX", "pinX", _
                   objRect.Cells("pinx").ResultIU, "pinX")
           blnResult = AddUserPropertyToShape(objRect, "piny", "piny", "piny", _
                   objRect.Cells("piny").ResultIU, "piny")


           Set objCell = objRect.Cells("width")
           objCell.Formula = "user.width*(ThePage!DrawingScale/ThePage!PageScale)"
           Set objCell = objRect.Cells("height")
           objCell.Formula = "user.height*(ThePage!DrawingScale/ThePage!PageScale)"
           Set objCell = objRect.Cells("pinx")
           objCell.Formula = "user.pinx*(ThePage!DrawingScale/ThePage!PageScale)"
           Set objCell = objRect.Cells("piny")
           objCell.Formula = "user.piny*(ThePage!DrawingScale/ThePage!PageScale)"

           objRect.Cells("Para.HorzAlign").Formula = visHorzLeft


   End Sub




   '*