| 
| 
| 
| #209271 - 2014-07-16 08:13 PM  how to trim a textbox |  
| Sam_B   Getting the hang of it
 
 Registered:  2005-10-25
 Posts: 68
 Loc:  Bern, Switzerland
 | 
Easy for an expert, but not for me... I have a read only text field in a form of a given length. If the text of the field is shorter than the field itself, I'd like to trim the text field. So I have to modify the width of the text box control. But I can't figure out how I can determine the width of the text.
 Any suggestion from the community?
 
 |  
| Top |  |  |  |  
| 
| 
| #209282 - 2014-07-22 08:20 AM  Re: how to trim a textbox
[Re:  Jochen] |  
| It_took_my_meds   Hey THIS is FUN
 
       
 Registered:  2003-05-07
 Posts: 273
 Loc:  Sydney, Australia
 | 
Function PixelStrSize($sInput)
	
	;FUNCTION		PixelStrSize()
	;ACTION			Used for sizing text boxes based on number of cs
	;SYNTAX			RETCODE = PixelStrSize($sInput[,OPTIONAL $Fontsize])
	;PARAMETERS		strInput
	;					Text that will go in the text box
	;RETURN			Size of text box
	;REMARKS			Function to return a pixel length of a string that is used to facilitate window sizing
	;DEPENDENCIES	none
	;EXAMPLE			$DriveLength=PixelStrSize($MapArray[$MapNumber][0])
	
	$ = SetOption("CaseSensitivity", "On")
	Dim $i, $c
	For $i = 1 to Len($sInput)
		$c = SubStr($sInput, $i, 1)
		Select
			Case $c = "'"
				$PixelStrSize = CDbl($PixelStrSize) + 2
			Case InStr('fjt !,.:;-/\()"Jr', $c)
				$PixelStrSize = CDbl($PixelStrSize) + 3
			Case InStr("szkvxy?cL", $c)
				$PixelStrSize = CDbl($PixelStrSize) + 5
			Case InStr("ughdeK0123456789$BEFYZabnopqP", $c)
				$PixelStrSize = CDbl($PixelStrSize) + 6
			Case InStr("UVRSTNACDGH*+X<=>&#OQ", $c)
				$PixelStrSize = CDbl($PixelStrSize) + 8
			Case InStr("Mwm", $c)
				$PixelStrSize = CDbl($PixelStrSize) + 9
			Case InStr("@%W", $c)
				$PixelStrSize = CDbl($PixelStrSize) + 12
			Case 1
				$PixelStrSize = CDbl($PixelStrSize) + 10
		EndSelect
	Next
	$ = SetOption("CaseSensitivity", $)
	
EndFunction
 Edited by It_took_my_meds (2014-07-22 10:54 AM)
 Edit Reason: Changed setting case sensitivity back as per Jochen's suggestion
 |  
| Top |  |  |  |  
| 
| 
| #209342 - 2014-08-04 04:23 PM  Re: how to trim a textbox
[Re:  It_took_my_meds] |  
| Sam_B   Getting the hang of it
 
 Registered:  2005-10-25
 Posts: 68
 Loc:  Bern, Switzerland
 | 
Sorry, was on leave...
 Nice function, does exactly what I need, but there's always a but... When I call the function, it seems to truncates at least the last char of the text in the text box. See my small example and try it yourself:
 
 
 
;region ScriptForm Designer
;region Constructor
Break On
$System = CreateObject("KiXtart.System")
;endregion
;region Post-Constructor Custom Code
Dim $Ret
$Ret = SetOption("Explicit", "on")
Dim $
;endregion
;region Form Creation
;Warning: It is recommended that changes inside this region be handled using the ScriptForm Designer.
;When working with the ScriptForm designer this region and any changes within may be overwritten.
;~~< Form1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global $Form1
$Form1 = $System.Form()
$Form1.Text = "Just a simple test"
;~~< TextBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global $TextBox1
$TextBox1 = $Form1.Controls.TextBox()
$TextBox1.Size = 183, 20
$TextBox1.Location = 89, 93
;~~< Label1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global $Label1
$Label1 = $Form1.Controls.Label()
$Label1.Text = "My Box:"
$Label1.Size = 70, 23
$Label1.Location = 13, 96
;~~< Button1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global $Button1
$Button1 = $Form1.Controls.Button()
$Button1.Text = "Resize"
$Button1.Size = 75, 23
$Button1.Location = 61, 184
$Button1.OnClick = "Resize( $$Button1 )"
;~~< Label2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global $Label2
$Label2 = $Form1.Controls.Label()
$Label2.Text = "Enter some text and press "+Chr(34)+"Resize"+Chr(34)+" to resize the width of the textbox"
$Label2.Size = 259, 46
$Label2.Location = 13, 24
;endregion
;region Custom Code
$TextBox1.text = "123.25GB"
;endregion
;region Event Loop
$Form1.Show
While $Form1.Visible
	$=Execute($System.Application.DoEvents)
Loop
;endregion
;endregion
;region Event Handlers
Function Resize( $object )
  $TextBox1.width = PixelStrSize($TextBox1.text)
EndFunction
;endregion
;region Script Settings
;<ScriptSettings xmlns="http://tempuri.org/ScriptSettings.xsd">
;  <ScriptPackager>
;    <process>kix32.exe</process>
;    <arguments />
;    <extractdir>%TEMP%</extractdir>
;    <files />
;    <usedefaulticon>true</usedefaulticon>
;    <showinsystray>false</showinsystray>
;    <altcreds>false</altcreds>
;    <efs>true</efs>
;    <ntfs>true</ntfs>
;    <local>false</local>
;    <abortonfail>true</abortonfail>
;    <product />
;    <version>1.0.0.1</version>
;    <versionstring />
;    <comments />
;    <company />
;    <includeinterpreter>false</includeinterpreter>
;    <forcecomregistration>false</forcecomregistration>
;    <consolemode>false</consolemode>
;    <EnableChangelog>false</EnableChangelog>
;    <AutoBackup>false</AutoBackup>
;    <snapinforce>false</snapinforce>
;    <snapinshowprogress>false</snapinshowprogress>
;    <snapinautoadd>2</snapinautoadd>
;    <snapinpermanentpath />
;    <cpumode>1</cpumode>
;    <hidepsconsole>false</hidepsconsole>
;  </ScriptPackager>
;</ScriptSettings>
;endregion
Break on
$ret = SetOption("explicit", "on")
Function PixelStrSize($sInput)
  Dim $, $i, $c
  $ = SetOption("CaseSensitivity", "On")
  For $i = 1 to Len($sInput)
    $c = SubStr($sInput, $i, 1)
    Select
      Case $c = "'"
        $PixelStrSize = CDbl($PixelStrSize) + 2
      Case InStr('fjt !,.:;-/\()"Jr', $c)
        $PixelStrSize = CDbl($PixelStrSize) + 3
      Case InStr("szkvxy?cL", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 5
      Case InStr("ughdeK0123456789$BEFYZabnopqP", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 6
      Case InStr("UVRSTNACDGH*+X<=>&#OQ", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 8
      Case InStr("Mwm", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 9
      Case InStr("@%W", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 12
      Case 1
        $PixelStrSize = CDbl($PixelStrSize) + 10
    EndSelect
  Next
  $ = SetOption("CaseSensitivity", $)
EndFunction
 |  
| Top |  |  |  |  
| 
| 
| #209343 - 2014-08-04 05:54 PM  Re: how to trim a textbox
[Re:  Sam_B] |  
| ShaneEP   MM club member
 
       
   Registered:  2002-11-29
 Posts: 2127
 Loc:  Tulsa, OK
 | 
Try just adding another 10 like so...
 
 Function PixelStrSize($sInput)
  Dim $, $i, $c
  $ = SetOption("CaseSensitivity", "On")
  For $i = 1 to Len($sInput)
    $c = SubStr($sInput, $i, 1)
    Select
      Case $c = "'"
        $PixelStrSize = CDbl($PixelStrSize) + 2
      Case InStr('fjt !,.:;-/\()"Jr', $c)
        $PixelStrSize = CDbl($PixelStrSize) + 3
      Case InStr("szkvxy?cL", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 5
      Case InStr("ughdeK0123456789$BEFYZabnopqP", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 6
      Case InStr("UVRSTNACDGH*+X<=>&#OQ", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 8
      Case InStr("Mwm", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 9
      Case InStr("@%W", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 12
      Case 1
        $PixelStrSize = CDbl($PixelStrSize) + 10
    EndSelect
  Next
  $PixelStrSize = $PixelStrSize + 10
  $ = SetOption("CaseSensitivity", $)
EndFunction |  
| Top |  |  |  |  
| 
| 
| #209344 - 2014-08-04 06:02 PM  Re: how to trim a textbox
[Re:  ShaneEP] |  
| ShaneEP   MM club member
 
       
   Registered:  2002-11-29
 Posts: 2127
 Loc:  Tulsa, OK
 | 
Also, I would change the Resize() function so that it always scrolls to the left of the entered text when it resizes.  Like so...
 
 Function Resize( $object )
  $TextBox1.width = PixelStrSize($TextBox1.text)
  $TextBox1.SelectionStart = 0
  $TextBox1.SelectionLength = 0
EndFunction |  
| Top |  |  |  |  
| 
| 
| #209352 - 2014-08-05 07:41 AM  Re: how to trim a textbox
[Re:  ShaneEP] |  
| Sam_B   Getting the hang of it
 
 Registered:  2005-10-25
 Posts: 68
 Loc:  Bern, Switzerland
 | 
Thanks ShaneEP for your hint, good point. Regarding the size... I'm not an expert but isn't the size of a char dependent of the screen resolution and font size of users machine?
 |  
| Top |  |  |  |  
| 
| 
| #209357 - 2014-08-05 09:10 PM  Re: how to trim a textbox
[Re:  Lonkero] |  
| ShaneEP   MM club member
 
       
   Registered:  2002-11-29
 Posts: 2127
 Loc:  Tulsa, OK
 | 
I created a small function to adjust element sizes based off of the computers DPI, some time ago.  Have used in multiple kixforms apps and it seems to keep all the spacing correct, even if someone has changed their size settings.
 Give it a shot and see how it works.  Try changing your font sizes and resolution and see if it still works well enough.
 
 
 Global $dpi
$dpi = CDbl(ReadValue("HKCU\Control Panel\Desktop\WindowMetrics","AppliedDPI"))
;region ScriptForm Designer
;region Constructor
Break On
$System = CreateObject("KiXtart.System")
;endregion
;region Post-Constructor Custom Code
Dim $Ret
$Ret = SetOption("Explicit", "on")
Dim $
;endregion
;region Form Creation
;Warning: It is recommended that changes inside this region be handled using the ScriptForm Designer.
;When working with the ScriptForm designer this region and any changes within may be overwritten.
;~~< Form1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global $Form1
$Form1 = $System.Form()
$Form1.Text = "Just a simple test"
;~~< TextBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global $TextBox1
$TextBox1 = $Form1.Controls.TextBox()
$TextBox1.Size = 183, 20
$TextBox1.Location = 89, 93
;~~< Label1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global $Label1
$Label1 = $Form1.Controls.Label()
$Label1.Text = "My Box:"
$Label1.Size = 70, 23
$Label1.Location = 13, 96
;~~< Button1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global $Button1
$Button1 = $Form1.Controls.Button()
$Button1.Text = "Resize"
$Button1.Size = 75, 23
$Button1.Location = 61, 184
$Button1.OnClick = "Resize( $$Button1 )"
;~~< Label2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global $Label2
$Label2 = $Form1.Controls.Label()
$Label2.Text = "Enter some text and press "+Chr(34)+"Resize"+Chr(34)+" to resize the width of the textbox"
$Label2.Size = 259, 46
$Label2.Location = 13, 24
;endregion
;region Custom Code
$TextBox1.text = "123.25GB"
;endregion
;region Event Loop
$Form1.Show
While $Form1.Visible
	$=Execute($System.Application.DoEvents)
Loop
;endregion
;endregion
;region Event Handlers
Function Resize( $object )
  $TextBox1.width = DPIAdjust(PixelStrSize($TextBox1.text))
  $TextBox1.SelectionStart = 0
  $TextBox1.SelectionLength = 0
EndFunction
Function PixelStrSize($sInput)
  Dim $, $i, $c
  $ = SetOption("CaseSensitivity", "On")
  For $i = 1 to Len($sInput)
    $c = SubStr($sInput, $i, 1)
    Select
      Case $c = "'"
        $PixelStrSize = CDbl($PixelStrSize) + 2
      Case InStr('fjt !,.:;-/\()"Jr', $c)
        $PixelStrSize = CDbl($PixelStrSize) + 3
      Case InStr("szkvxy?cL", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 5
      Case InStr("ughdeK0123456789$BEFYZabnopqP", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 6
      Case InStr("UVRSTNACDGH*+X<=>&#OQ", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 8
      Case InStr("Mwm", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 9
      Case InStr("@%W", $c)
        $PixelStrSize = CDbl($PixelStrSize) + 12
      Case 1
        $PixelStrSize = CDbl($PixelStrSize) + 10
    EndSelect
  Next
  $PixelStrSize = $PixelStrSize + 10
  $ = SetOption("CaseSensitivity", $)
EndFunction
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;  SETS SIZES DYNAMICALLY ACCORDING TO DPI.     ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function DPIAdjust($n)
   If Len($dpi)=0
      $dpi = CDbl(96)
   Endif
   $DPIAdjust = ($dpi/96.000)*$n
EndFunction |  
| Top |  |  |  |  
 Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
 
 | 
| 
 
| 0 registered
and 456 anonymous users online. 
 | 
 |  |