Sam_B
(Getting the hang of it)
2014-07-16 08:13 PM
how to trim a textbox

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?


LonkeroAdministrator
(KiX Master Guru)
2014-07-16 09:41 PM
Re: how to trim a textbox

Calculate. Font size times characters.

Glenn BarnasAdministrator
(KiX Supporter)
2014-07-17 02:23 AM
Re: how to trim a textbox

That won't work for most proportional fonts - you'd need a table of char-widths. A period, for example, is about 1/5th the size of an "M". You could group chars into collections based on similar widths.. skinny (,!:.) narrow (li-), normal (most alpha & digits), and wide (wmQ&$). Or, you could use a fixed-width font like Courier.

Better question is why change the textbox size? Explain that and we might offer alternative solutions.

Glenn


LonkeroAdministrator
(KiX Master Guru)
2014-07-17 05:15 AM
Re: how to trim a textbox

Or just use courier and call it done.

JochenAdministrator
(KiX Supporter)
2014-07-17 02:43 PM
Re: how to trim a textbox

If it is readonly, why not just set the BorderStyle to 0 make BackgroundColor = Form.BackgroundColor and forget about the width of it ?

I may be completely wrong though. Can we see a screenshot of the form?


Mart
(KiX Supporter)
2014-07-21 09:48 AM
Re: how to trim a textbox

 Originally Posted By: Jochen
If it is readonly, why not just set the BorderStyle to 0 make BackgroundColor = Form.BackgroundColor and forget about the width of it ?

I may be completely wrong though. Can we see a screenshot of the form?


Switching to a label instead of a textbox would be easier then. No need to set the color and borders for a label to "hide" the empty space.


JochenAdministrator
(KiX Supporter)
2014-07-21 01:18 PM
Re: how to trim a textbox

true

It_took_my_meds
(Hey THIS is FUN)
2014-07-22 08:20 AM
Re: how to trim a textbox

 Code:
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


JochenAdministrator
(KiX Supporter)
2014-07-22 09:37 AM
Re: how to trim a textbox

Hi,

nice one..
One remark though: In order to not change the previous set CaseSensitivity you should change the second SetOption line to:

$ = SetOption("CaseSensitivity", $)


It_took_my_meds
(Hey THIS is FUN)
2014-07-22 10:55 AM
Re: how to trim a textbox

Thanks Jochen, I've updated the function :-)

Sam_B
(Getting the hang of it)
2014-08-04 04:23 PM
Re: how to trim a textbox

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:

 Code:
;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


ShaneEP
(MM club member)
2014-08-04 05:54 PM
Re: how to trim a textbox

Try just adding another 10 like so...

 Code:
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


ShaneEP
(MM club member)
2014-08-04 06:02 PM
Re: how to trim a textbox

Also, I would change the Resize() function so that it always scrolls to the left of the entered text when it resizes. Like so...

 Code:
Function Resize( $object )
  $TextBox1.width = PixelStrSize($TextBox1.text)
  $TextBox1.SelectionStart = 0
  $TextBox1.SelectionLength = 0
EndFunction


Sam_B
(Getting the hang of it)
2014-08-05 07:41 AM
Re: how to trim a textbox

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?

LonkeroAdministrator
(KiX Master Guru)
2014-08-05 01:42 PM
Re: how to trim a textbox

Screen size won't matter. But you bring up a good question about font size. Since windows allows users to make text larger (for readability) without changing resolution that might have an effect

ShaneEP
(MM club member)
2014-08-05 09:10 PM
Re: how to trim a textbox

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.

 Code:
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