Advertisement
ASP_Volume3 String Manipulation #47758

Autosize a Label Caption

This small and very simple sub will format the caption of a Label control if the text is too big to display in the control. The sub will trucate the text and append "..." to the end of the text (indicating to the user that they are not seeing the full text). VB automatically wordwraps the caption of a label if it is too big, however, this results in the caption being truncated only where there is a space. Also, you can see the top of the next line of the caption. Example Make and Model: Cadillac becomes: Make and Model: Cadillac Eldor... I find this extremely useful when I don't know the maximum length of the text the label will contain, or if I don't have enough screen real estate to make the Label big enough. Just pass a label to this sub for formatting.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
'This small and very simple sub will format the
'caption of a Label control if the text is too
'big to display in the control. The sub will
'trucate the text and append "..." to the end
'of the text (indicating to the user that they
'are not seeing the full text). VB automatically
'wordwraps the caption of a label if it is too
'big, however, this results in the caption being
'truncated only where there is a space. Also,
'you can see the top of the next line of the caption.
'Example
'Make and Model: Cadillac
'becomes:
'Make and Model: Cadillac Eldor...
'I find this extremely useful when I don't know the
'maximum length of the text the label will contain,
'or if I don't have enough screen real estate to
'make the Label big enough.

Private Sub AutoSizeCaption(lbl As Label)
  Dim i      As Integer
  Dim iLabelWidth As Integer
  Dim sText    As String
  Const kMore = "..."
  ' store orignal caption and width
  sText = lbl.Caption
  
  ' numeric or date? Don't format.
  If IsNumeric(lbl.Caption) Or IsDate(lbl.Caption) Then Exit Sub
  iLabelWidth = lbl.Width
  ' allow label to "spring" to it's actual width
  lbl.AutoSize = True
  ' is required width of label < actual width?
  If lbl.Width > iLabelWidth Then
    i = Len(sText) - 1
    Do
      lbl.Caption = Left(sText, i) & kMore
      i = i - 1
    Loop Until (lbl.Width <= iLabelWidth) Or (i = 0)
  End If
Exit_Sub:
  lbl.AutoSize = False
  lbl.Width = iLabelWidth
  Exit Sub
  
ErrorHandler:
  ' something went wrong ... put everything back
  lbl.Caption = sText
  Resume Exit_Sub
End Sub

<%
' hit highliter
' this was not user load tested
' have fun - masswebsites@yahoo.com
option explicit
Response.Expires=0
%>
<html>
<head>
<title></title>
</head>
<body>
	<%
	' check querystring
	if request("qs") = "" then
		Response.Write "No querystring supplied. <a href='hitliter.asp?qs=asp%20loop%20example'>Try this one</a>"		
	else
		dim sQueryString
		dim sSummary
		dim sDisplay
		
		sQueryString = request("qs")
		
		' example summary - could also be from a database or other data source
		sSummary = "I hope you find this ASP code sample useful. It uses nested for next loops , strcomp , arrays , join , split , and some other fun things. If you can comment, criticize, or optimize this code example, please post a reply."
		
		' display with matching words highlighted. Could also set a variable = hitLite(sSummary,sQueryString)
		Response.Write hitLite(sSummary,sQueryString) & "<br><br>"
	end if
	%>
</body>
</html>

<%
' **************************************
' put it in a global include file
' **************************************
function hitLite (sSummary,sQueryString)
	dim arrQueryString
	dim iQs
	dim i
	dim arrSummary
	dim sArrQueryStringTmp
	dim iComp
	dim sArrSummaryTmp
	dim sRoot
	dim sLastChar
	dim sLength
	
	' break the variable we want to compare querystring to into an array
	arrSummary = split(sSummary," ")	
		
	' break querystring into an array, use space for delimiter
	arrQueryString = split(sQuerystring," ")
		
	' for every word in the querystring
	for iQs = 0 to ubound(arrQuerystring)
			
		' assign the value to a temp variable
		sArrQueryStringTmp = arrQuerystring(iQs)
			
		' don't include common search words, you can take this out. I was using this for a search engine
		if (sArrQueryStringTmp <> "and") and (sArrQueryStringTmp <> "or") and (sArrQueryStringTmp <> "+") then
				
			' for each element in the variable array, replace querystring word in varaible array with the word plus bgcolor ( highlight )	
			for i = 0 to ubound(arrSummary)
				
				sArrSummaryTmp = arrSummary(i)
					
				' if the 2 strings compare, stick the display value in a span with style! 
				if strcomp(sArrSummaryTmp,sArrQueryStringTmp,1) = 0 then
					arrSummary(i) = "<span style=background:yellow;font-weight:bold;>" & sArrSummaryTmp & "</span>"		
				else
					' check "s", comma, period
					' must end "s" or comma or period AND be greater than 1 character
					sLastChar = right(sArrSummaryTmp,1)
					sLength = len(sArrSummaryTmp)
				
					if (sLastChar = "s") or (sLastChar = ".") or (sLastChar = ",") and (sLength > 1) then
						
						'the word minus last letter
						sRoot = left(sArrSummaryTmp,sLength-1)
						
						' if the root comapres to the querystring
						' replace that element of the array with the root highlited and the last character in regular display
						if strcomp(sRoot,sArrQueryStringTmp,1) = 0 then
							' don't include the comma or period in the lite
							arrSummary(i) = "<span style=background:yellow;font-weight:bold;>" & sRoot & "</span>" & sLastChar & "&nbsp;"		
						end if
					end if
				end if
				
			next
		
		end if
		
	next
	hitLite = join(arrSummary, " ")
		
end function
%>
Original Comments (3)
Recovered from Wayback Machine