Form class cannot get measurestring to work

Ran into a snag with the MeasureString- seems as if the class isn’t registering the TEXT being passed to it:


WinForm-Dynamic-Height.dyn (12.4 KB)

Found this great form to pull from and developing my own forms in python. Didn’t realize Python also did classes- pouring more gas on the fire as I learn!

import clr
import sys
import math


clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

##https://forum.dynamobim.com/t/reporting-to-user-at-the-end-of-dynamo-player-script/37421/6
from System.Windows.Forms import Form,Label,Button,FormBorderStyle,FormStartPosition

##https://forum.dynamobim.com/t/winform-python-with-sharpdevelop/75872/2
class ShortForm(Form):
    def __init__(self, strtext):               ##Class initialize on new or call
        self.out = []                       ##Empty output field
        self.InitializeComponent(strtext)      ##Passs text to Initialize
		
    def InitializeComponent(self, strtext):    ##

        form = Form() 
        form.Width = 300                    #Width
        form.Height = 200                   ##Height
        form.Text = "Script Result:"        ##Form Title
    
        form.FormBorderStyle  = FormBorderStyle.FixedDialog ##Remove the maximize box.
        form.MaximizeBox = False            ## Set the MinimizeBox to false to remove the minimize box.
        form.MinimizeBox = False            ## Set the accept button of the form to button1.
        form.StartPosition = FormStartPosition.CenterScreen
        form.AutoScroll = True
    
        label = System.Windows.Forms.Label()
        label.Parent = form 
        label.Text = strtext 
        label.Width = form.Width - 55
        ##https://stackoverflow.com/questions/388937/determine-label-size-based-upon-amount-of-text-and-font-size-in-winforms-c
        DynamicHt = System.Drawing.Graphics.MeasureString(str(strtext), label.Font, (int)(form.Width-55) )
        label.Height =  math.ceil(DynamicHt)
        label.AutoSize=False
        #label.Size = System.Drawing.Size(Form.Width - 55,0)    ##<<<<<<<<<<DEBUG
        label.Left=10
        label.Top= 10
        form.ShowDialog()

#################################################################

Text="".join(IN[0]) 
form = ShortForm(Text)          ##INITIALIZE SHORT FORM
Application.Run(form())

OUT=form.out

From the .Net doc MeasureString is not a Static Method

there are some examples here

Thanks- Saw those - and the python seems to be ignoring the overload of ,,

It only reports the font and width in the error- making me thing it isn’t seeing the text. Really odd.

The original post uses “SharpDevelop” which may? solve the issues?

here an example

MeasureString_test

import sys
import clr
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')
import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *
import math

class Form20(Form):
	def __init__(self):
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._label1 = System.Windows.Forms.Label()
		self._buttonFill = System.Windows.Forms.Button()
		self.SuspendLayout()
		# 
		# label1
		# 
		self._label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
		self._label1.Font = System.Drawing.Font("Microsoft Sans Serif", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0)
		self._label1.Location = System.Drawing.Point(12, 25)
		self._label1.Name = "label1"
		self._label1.Size = System.Drawing.Size(368, 83)
		self._label1.TabIndex = 0
		self._label1.Text = "MyText"
		# 
		# buttonFill
		# 
		self._buttonFill.Location = System.Drawing.Point(126, 130)
		self._buttonFill.Name = "buttonFill"
		self._buttonFill.Size = System.Drawing.Size(142, 38)
		self._buttonFill.TabIndex = 1
		self._buttonFill.Text = "Rearrange Text Rectangle"
		self._buttonFill.UseVisualStyleBackColor = True
		self._buttonFill.Click += self.ButtonFillClick
		# 
		# Form20
		# 
		self.ClientSize = System.Drawing.Size(392, 180)
		self.MaximizeBox = False
		self.MinimizeBox = False
		self.FormBorderStyle = FormBorderStyle.FixedDialog
		self.Controls.Add(self._buttonFill)
		self.Controls.Add(self._label1)
		self.Name = "Form20"
		self.Text = "Form20"
		self.ResumeLayout(False)


	def ButtonFillClick(self, sender, e):
		#
		with self.CreateGraphics() as g:
			size = g.MeasureString(self._label1.Text, self._label1.Font)
			# add margin
			size += System.Drawing.SizeF(5.0, 5.0)
			# ajust label size
			self._label1.Height = math.ceil(size.Height)
			self._label1.Width = math.ceil(size.Width)

		
objForm = Form20()
objForm.ShowDialog()
1 Like

Ok found the means to wrap text and scroll vertically only in a fixed size dialog. This wraps the text to the form width -40px allowing the auto-wrap to be constrained to that width with unlimited height for a scroll…

I maybe wasn’t clear before. @c.poupin - I had seen the class wrapper here in the youtube python net tutorials but the spokesperson is just following the examples; yours was quite helpful with the basic class wrapper to use for comparison!

image

forms are sooo much easier editing graphically!

import clr
import sys

clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

import System
from System import Drawing
import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *
import math

##https://forum.dynamobim.com/t/reporting-to-user-at-the-end-of-dynamo-player-script/37421/6
from System.Windows.Forms import Form,Label,Button,FormBorderStyle,FormStartPosition

#################################################################################
class popup(Form):
    ##
    def __init__(self,text):
        self.InitializeComponent(text)
        
    def InitializeComponent(self,text): 
        #form = Form() 
        self.ClientSize = System.Drawing.Size(300, 200)                 ##Height
        self.Text = "Result:"        ##Form Title
    
        self.FormBorderStyle  = FormBorderStyle.FixedDialog ##Remove the maximize box.
        self.MaximizeBox = False            ## Set the MinimizeBox to false to remove the minimize box.
        self.MinimizeBox = False            ## Set the accept button of the form to button1.
        self.StartPosition = FormStartPosition.CenterScreen
        self.AutoScroll = True
        self.ScrollBars = ScrollBars.Vertical
       
        ########Label for text#####
        
        self.label = Label() 
        self.label.Parent = self 
        self.label.Text = text 
        self.label.TextAlign = ContentAlignment.TopLeft
        self.label.Font = System.Drawing.Font("Tahoma", 10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0)
        
        self.label.AutoSize = True
        
        ##https://stackoverflow.com/questions/1204804/word-wrap-for-a-label-in-windows-forms
        ##This was the key to the wrapping inside the label in the form...
        self.label.MaximumSize  = System.Drawing.Size(self.Width-40,0)
        self.label.WordWrap=True
        
        self.label.Left=10
        self.label.Top=10 
        self.ResumeLayout(False)

text="".join(IN[0]) 

oForm=popup("FOO" + text)   ##Set the form with the text value
oForm.ShowDialog()          ##Show the form

For anyone else looking to use this here is a dyn with the examples enclosed:
Legends-Delete-Unused-V01-00.dyn (16.0 KB)

1 Like