Graphics.StringHeight Woes

I have been using graphics.stringwidth for sometime now along with graphics.stringheight. However I always used a static wrap width of 150-200. I have a need now to have a dynamically sized window.width/window.height based on the graphics.stringheight results. The issue is that I cant figure out for the life of me how that stinking wrapwidth works. Yes I have read the LR about 20 times literally in the last few hours. Its just not acting the way it reads.

Graphics.StringHeight ( Text as String, WrapWidth as Integer ) As Integer
The WrapWidth parameter specifies the width (in pixels) at which text should wrap.

I have been intrepreting wrapwidth by “wrapping” at the specified number of pixels and no matter what I cant get it to scale dynamically. Meaning whatever wrap width looks great for say one line of text doesn’t work well for text 2 * the original line type of thing.

Any advice is super appreciated as I am ready to sledgehammer my mac while watching " National Lampoon’s Christmas Vacation" :slight_smile: Thanks again everyone .

  if DoubleClicked = True Then
    // SET THE HEIGHT AND WIDTH OF THE PopUpDetailWindow
    Dim thisCellContent as String = Me.Cell(me.ListIndex,0) + ":  " + Me.Cell(me.ListIndex,1)  
    
    // LOAD GRAPHICS PROPERTIES FOR PopUpDetailWindow.PopUpMessageLabel
    g.TextSize = AristaEAPI_Window.thePopUpDetailWindow.PopUpMessageLabel.TextSize
    g.TextFont =  AristaEAPI_Window.thePopUpDetailWindow.PopUpMessageLabel.TextFont
    Dim popupDetailWinWidth as Double = g.StringWidth(thisCellContent)
    Dim popupDetailWinHeight as Integer
    
    // DECIDE WHETHER TO GROW THE WIDTH OR HEIGHT OF THE PopUpDetailWindow
    Select Case popupDetailWinWidth
    Case is < Self.Width
      AristaEAPI_Window.thePopUpDetailWindow.Width = popupDetailWinWidth
    Case is > Self.Width
      Dim theWidth as Integer = Self.Width+6
      Dim theWrapVal as Integer = (Self.Width-14) // <-- UGH!
      popupDetailWinHeight  = g.StringHeight(thisCellContent,theWrapVal)
      AristaEAPI_Window.thePopUpDetailWindow.Width = theWidth
      AristaEAPI_Window.thePopUpDetailWindow.Height = popupDetailWinHeight
    End Select
    
    // LOAD THE PopUpDetailWindow.PopUpMessageLabel
    AristaEAPI_Window.thePopUpDetailWindow.PopUpMessageLabel.Text = thisCellContent
    // RESET GLOBAL PROPERTY
    DoubleClicked = False
  end if

My advice is…don’t wrap.
HTH :slight_smile:

[quote=147316:@Richard Summers]My advice is…don’t wrap.
HTH :)[/quote]
HA … HA :slight_smile: How so since its a mandatory value.

Glad I could help :slight_smile:

References For my Above Posting:

Dim thisCellContent as String = Me.Cell(me.ListIndex,0) + ":  " + Me.Cell(me.ListIndex,1) // <-- String value is "Command cannot be used over the API at this time. To see ASCII output, set format='text' in your request:"
    
Self.Width = 653
g.TextSize = AristaEAPI_Window.thePopUpDetailWindow.PopUpMessageLabel.TextSize // <-- (value is 13)
g.TextFont =  AristaEAPI_Window.thePopUpDetailWindow.PopUpMessageLabel.TextFont //<-- value is system for Yosemite

Also to note in the above values the wrapwidth of 200 looks proper Window Height wise. I just can’t turn that into a formula for when I have more/less text to measure.

Ie#1 Window at 200 Wrapwidth using the text above

Ie#2 Window at 200 Wrapwidth using above text Plus more text;

*** Notice the bottom height issue on #2

OR Am I wrong to try and use the stringheight as my window height??

Throw this into the paint event of a canvas.

  dim s as string 
  s= "Command cannot be used over the API at this time. " _
  + "To see ASCII output set format = text in your request."
  s= s+ " "+ s
  s= s+ " "+ s
  
  dim w, h as integer
  w = 350
  h= g.StringHeight(s, w)
  
  g.drawstring(s, 100,100, w)
  g.drawrect(100, 100-g.textascent, w, h)

The rectangle should perfectly encompass the string. If so, then textheight is working properly and your problem is elsewhere. I’ve been bit by things like locking before, where resizing one thing resizes other things.

a ha! Thank Tim. I am in the middle of converting that label to a canvas and thanks for the code snippet! Ill give that a whirl.

Tim excellent as the canvas paint event was the ticket. Apparently the Label has issues with g.StringHeight since I could never get any consistent results using the label. I instantly moved it over to the Canvas per original recommendation from the “Coder formerly known as BH” (CFKABH) and applied a few of Tim’s techniques and bam it works perfectly. (Thanks Tim!)

So if you are reading this sometime down the road and happen to have struggles trying to get the correct stringheight of the text from a label then I recommend using a canvas instead. Use the Canvas.Paint with Tim’s code from above to “fake” a label/textfield because the canvas.paint’s g.stringheight works perfectly when used with g.drawstring. Here is my working code from my Canvas.Paint:

Canvas1.Paint

  g.AntiAlias = true
  g.TextFont = "System"
  g.TextSize = 13
  g.DrawString(stringContent,0,10,me.Width,False)
  Dim theHeight as Integer = g.StringHeight(stringContent,me.Width)
  Self.Height =theHeight 

What I to do is wrap the string myself into a string array and draw it myself. I then use g.textheight and multiply. That approach lets me change line spacing as well being able to justify the text line by line.

  • Karen

[quote=147341:@Karen Atkocius]What I to do is wrap the string myself into a string array and draw it myself. I then use g.textheight and multiply. That approach lets me change line spacing as well being able to justify the text line by line.

  • Karen[/quote]
    Very Nice Karen and thank you for sharing! I will note that also and try it. :slight_smile: Thanks!!!

[quote=147316:@Richard Summers]My advice is…don’t wrap.
HTH :)[/quote]
I usually don’t
Plain paper or plastic bags is what they get

I read elsewhere in this forum how inconsistent the label was with line spacing. That probably explains a lot.