Results 1 to 9 of 9

Thread: [RESOLVED] Split Database data into label size

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Northampton, UK
    Posts
    49

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Northampton, UK
    Posts
    49

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Split Database data into label size

    Quote Originally Posted by AndyGable View Post
    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Northampton, UK
    Posts
    49

    Re: Split Database data into label size

    Hi everyone,

    I have been working on this problem for a few days and I think I have almost cracked it but I need someone to advice me on the last part



    As you can see I have managed to arrange the label over the "virtual shelf label" but for some reason instead of the code using the last label (number 4) it jumps to label5

    below is my code that i have come up with to this point

    Code:
            Dim i As Integer = 1
    
            Label1.Text = ListBox1.Items(0).ToString
    
            Me.Refresh()
            labelName = "label" & i
    
            For i = 1 To ListBox1.Items.Count - 1
    
                If Len(Me.Controls(labelName).Text) <= 20 Then
                    If (Len(Me.Controls(labelName).Text) + Len(ListBox1.Items(i).ToString) + 1) <= 20 Then
                        Me.Controls(labelName).Text += " " & ListBox1.Items(i).ToString
                        Me.Refresh()
                    Else
                        labelName = "label" & (i + 1)
                        Me.Controls(labelName).Text = ListBox1.Items(i).ToString
                        Me.Refresh()
                    End If
                End If
            Next
    If someone can spot where I have gone wrong I would most appreciate it
    Attached Images Attached Images  
    Last edited by AndyGable; Nov 6th, 2020 at 01:55 PM. Reason: Typo

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Northampton, UK
    Posts
    49

    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Split Database data into label size

    Quote Originally Posted by AndyGable View Post
    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.

  9. #9
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    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
  •  



Click Here to Expand Forum to Full Width