Ver Mensaje Individual
  #4 (permalink)  
Antiguo 20/07/2011, 08:40
Avatar de HaverRamirez
HaverRamirez
 
Fecha de Ingreso: junio-2011
Ubicación: Guatemala
Mensajes: 273
Antigüedad: 13 años, 4 meses
Puntos: 33
Respuesta: vb.net como limitar label multilinea por caracteres

Un comentario, el codigo anterior, cuenta letras, pero solo las letras entonces el texto te querdaria cortado

Este te
xto deb
eria ir c
ortado
por pal
abra

para ello mira esto...

Código vb:
Ver original
  1. Public Function WrapText(ByVal Text As String, ByVal
  2. LineLength As Integer) As List(Of String)
  3.  
  4. Dim ReturnValue As New List(Of String)
  5. ' Remove leading and trailing spaces
  6. Text = Trim(Text)
  7.  
  8.  
  9. Dim Words As String() = Text.Split(" ")
  10.  
  11. If Words.Length = 1 And Words(0).Length > LineLength
  12. Then
  13. ' Text is just one big word that is longer than one line
  14. ' Split it mercilessly
  15. Dim lines As Integer = (Int(Text.Length / LineLength)
  16. + 1)
  17. Text = Text.PadRight(lines * LineLength)
  18. For i = 0 To lines - 1
  19. Dim SliceStart As Integer = i * LineLength
  20. ReturnValue.Add(Text.Substring(SliceStart,
  21. LineLength))
  22. Next
  23. Else
  24. Dim CurrentLine As New System.Text.StringBuilder
  25. For Each Word As String In Words
  26. ' will this word fit on the current line?
  27. If CurrentLine.Length + Word.Length <
  28. LineLength Then
  29. CurrentLine.Append(Word & " ")
  30. Else
  31. ' is the word too long for one line
  32. If Word.Length > LineLength Then
  33. ' hack off the first piece, fill out the
  34. current line and start a new line
  35. Dim Slice As String =
  36. Word.Substring(0, LineLength - CurrentLine.Length)
  37. CurrentLine.Append(Slice)
  38. ReturnValue.Add(CurrentLine.ToString)
  39. CurrentLine = New
  40. System.Text.StringBuilder
  41.  
  42. ' Remove the first slice from the word
  43. Word = Word.Substring(Slice.Length,
  44. Word.Length - Slice.Length)
  45.  
  46. ' How many more lines do we need for
  47. this word?
  48. Dim RemainingSlices As Integer =
  49. Int(Word.Length / LineLength) + 1
  50. For LineNumber = 1 To
  51. RemainingSlices
  52. If LineNumber = RemainingSlices
  53. Then
  54. 'this is the last slice
  55. CurrentLine.Append(Word & "
  56. ")
  57. Else
  58. ' this is not the last slice
  59. ' hack off a slice that is one
  60. line long, add that slice
  61. ' to the output as a line and
  62. start a new line
  63. Slice = Word.Substring(0,
  64. LineLength)
  65. CurrentLine.Append(Slice)
  66.  
  67. ReturnValue.Add(CurrentLine.ToString)
  68. CurrentLine = New
  69. System.Text.StringBuilder
  70.  
  71. ' Remove the slice from the
  72. word
  73. Word =
  74. Word.Substring(Slice.Length, Word.Length - Slice.Length)
  75. End If
  76. Next
  77. Else
  78. ' finish the current line and start a new
  79. one with the wrapped word
  80. ReturnValue.Add(CurrentLine.ToString)
  81. CurrentLine = New
  82. System.Text.StringBuilder(Word & " ")
  83. End If
  84. End If
  85. Next
  86.  
  87. ' Write the last line to the output
  88. If CurrentLine.Length > 0 Then
  89. ReturnValue.Add(CurrentLine.ToString)
  90. End If
  91. End If
  92. Return ReturnValue
  93. End Function