-
Oct 29th, 2020, 09:06 AM
#1
Thread Starter
Member
[RESOLVED] Split Database data into label size
Hi All,
I Hope someone can advise me and what I need to do to get this to work.
I have a database that has a field called SELDescription and this can be upto 100 chrs long (hold the description of a product) for example
JOHNSONS BABY COTTON BUDS 100PK
At the moment I can get the first 18 chrs so I would have "JOHNSONS BABY COTT" but I would like it so if a word is cut off half way for that to be placed onto the next line
so the label would look like this
JOHNSONS BABY
COTTON BUDS 100PK
and not
JOHNSONS BABY COTT
ON BUDS 100PK
Any advice would be welcomed as the label code has 5 lines of text I can use. (they are called LabDescriptionLine1 - Line5)
Last edited by AndyGable; Nov 2nd, 2020 at 05:48 AM.
Reason: Typo
-
Oct 29th, 2020, 09:25 AM
#2
Re: Split Database data into label size
Why would you have five Labels? Just use one Label and it will wrap automatically exactly how you want. Just add a single Label to your form, set AutoSize to False and then set the Size appropriately. The rest is automatic when you set the Text.
-
Nov 2nd, 2020, 05:47 AM
#3
Thread Starter
Member
Re: Split Database data into label size
Thanks for the replay but I do not think I have explain myself clear enough
The Labels are not on the applications they are a Physical label that I print on a Zebra Thermal Printer (and it does not support Word wrapping)
So you can see why I have asked this question now
-
Nov 2nd, 2020, 05:54 AM
#4
Re: Split Database data into label size
Originally Posted by AndyGable
So you can see why I have asked this question now
It's amazing what a difference providing all the relevant details can make. You should look into the String.LastIndexOf method. It will enable you to find the last index of the space character before a specific index in the original String. That will tell you where to make the first cut, and you can then continue doing the same thing for the next set of characters. Before you throw your hands up and cry that that's too hard for someone of your experience level, try working out the logic first, which requires no programming experience, and then writing code to implement the logic. You'll have a much better chance of succeeding that way. If it doesn't work in the end, we can always help further but at least you can give it a go.
-
Nov 6th, 2020, 01:52 PM
#5
Thread Starter
Member
Re: Split Database data into label size
Last edited by AndyGable; Nov 6th, 2020 at 01:55 PM.
Reason: Typo
-
Nov 6th, 2020, 03:52 PM
#6
Re: Split Database data into label size
That last line has what appears to be one very long word. Could you give it a test where the - is replaced with a space?
I'd say what is happening is that you are seeing an artifact of the data you happen to be using. A very long word following a very long item is causing the inner condition to jump to the else when you didn't want it to. A bit of debugging will tell you that for certain. Just put a breakpoint on the For and step through the iterations of the loop. When you get to the iteration that is causing you the trouble, you can either step into the conditionals to see the path that is taken (which will not be the path you wanted it to take), or you can look at the variables to see why that path is taken...or both, of course.
This happens with word wrapping. Very long words cause ugly results.
Also, you might as well get rid of Len. That's a legacy from earlier VB. In .NET, the .Length property of the string is the preferred means of getting the length of a string.
My usual boring signature: Nothing
-
Nov 7th, 2020, 05:29 AM
#7
Thread Starter
Member
Re: Split Database data into label size
Hi Everyone
I have sorted the issue
I had to change
Code:
labelName = "label" & (i + 1)
to
Code:
Select Case labelName
Case "label1"
labelName = "label2"
Case "label2"
labelName = "label3"
Case "label3"
labelName = "label4"
Case "label4"
labelName = "label5"
End Select
-
Nov 7th, 2020, 11:09 AM
#8
Re: Split Database data into label size
Originally Posted by AndyGable
I had to change
Code:
labelName = "label" & (i + 1)
to
Code:
Select Case labelName
Case "label1"
labelName = "label2"
Case "label2"
labelName = "label3"
Case "label3"
labelName = "label4"
Case "label4"
labelName = "label5"
End Select
I'm not sure what you should have done - haven't looked closely enough - but I can tell you that it definitely wasn't that. That would not be the best way to do anything.
-
Nov 8th, 2020, 02:21 AM
#9
Re: [RESOLVED] Split Database data into label size
@ Andy
you should be using the Stringbuilder for that kind of stuff,
here a small sample
Code:
Option Strict On
Public Class Form2
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Debug.Print(myStringLenght(TextBox1.Text, 18))
End Sub
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TextBox1.Text = "JOHNSONS BABY COTTON BUDS 100PK"
TextBox1.Text = "Astonisch Antibacteriel Surface Cleaner Spray-750ml"
End Sub
Function myStringLenght(ByVal Text As String, ByVal nLenght As Integer) As String
Dim sb As New System.Text.StringBuilder
Dim xLenght As Integer = nLenght
For Each xWord As String In Text.Split({" "c}, StringSplitOptions.None)
If xWord.Length < xLenght Then
sb.AppendFormat(xWord & " ")
xLenght -= xWord.Length - 1
ElseIf xWord.Length = xLenght Then
sb.AppendFormat(xWord & vbCrLf)
xLenght = nLenght
Else
sb.AppendFormat(vbCrLf & xWord & " ")
xLenght = nLenght - xWord.Length - 1
End If
Next
Return sb.ToString
End Function
End Class
the debug output
Code:
Astonisch
Antibacteriel
Surface Cleaner
Spray-750ml
play a bit with it, and set the lenght from 18 to ....
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|