Page 1 of 1 1
Topic Options
#209271 - 2014-07-16 08:13 PM how to trim a textbox
Sam_B Offline
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
#209272 - 2014-07-16 09:41 PM Re: how to trim a textbox [Re: Sam_B]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
Calculate. Font size times characters.
_________________________
!

download KiXnet

Top
#209273 - 2014-07-17 02:23 AM Re: how to trim a textbox [Re: Lonkero]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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
_________________________
Actually I am a Rocket Scientist! \:D

Top
#209276 - 2014-07-17 05:15 AM Re: how to trim a textbox [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
Or just use courier and call it done.
_________________________
!

download KiXnet

Top
#209277 - 2014-07-17 02:43 PM Re: how to trim a textbox [Re: Lonkero]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
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?


Edited by Jochen (2014-07-17 02:45 PM)
_________________________



Top
#209280 - 2014-07-21 09:48 AM Re: how to trim a textbox [Re: Jochen]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
 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.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#209281 - 2014-07-21 01:18 PM Re: how to trim a textbox [Re: Mart]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
true
_________________________



Top
#209282 - 2014-07-22 08:20 AM Re: how to trim a textbox [Re: Jochen]
It_took_my_meds Offline
Hey THIS is FUN
*****

Registered: 2003-05-07
Posts: 273
Loc: Sydney, Australia
 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


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
#209283 - 2014-07-22 09:37 AM Re: how to trim a textbox [Re: It_took_my_meds]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
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", $)
_________________________



Top
#209285 - 2014-07-22 10:55 AM Re: how to trim a textbox [Re: Jochen]
It_took_my_meds Offline
Hey THIS is FUN
*****

Registered: 2003-05-07
Posts: 273
Loc: Sydney, Australia
Thanks Jochen, I've updated the function :-)
Top
#209342 - 2014-08-04 04:23 PM Re: how to trim a textbox [Re: It_took_my_meds]
Sam_B Offline
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:

 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

Top
#209343 - 2014-08-04 05:54 PM Re: how to trim a textbox [Re: Sam_B]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
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

Top
#209344 - 2014-08-04 06:02 PM Re: how to trim a textbox [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
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...

 Code:
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 Offline
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
#209354 - 2014-08-05 01:42 PM Re: how to trim a textbox [Re: Sam_B]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
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
_________________________
!

download KiXnet

Top
#209357 - 2014-08-05 09:10 PM Re: how to trim a textbox [Re: Lonkero]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
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.

 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

Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.068 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org