Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to assign combo box text which like 100 characters. and I am having two textboxes so in each textbox should display 50,50 characters respectfully.

below is my function..where I need to change kindly help...I am new for vb.net
Public Sub Addpayee2()
       Try
           Dim payee2 As String = ""
           payee2 = cboAccounts.Text.ToString()

           Dim g As Graphics = cboAccounts.CreateGraphics
           Dim payee2word As String, oneLine As String
           Dim test As String = "", test1 As String = ""
           Dim p2words() As String

           If g.MeasureString(payee2, txtPayAgainst.Font).Width >= txtPayAgainst.Width Then
               oneLine = payee2
               p2words = oneLine.Split(" ")
               For Each word In p2words
                   test = test & word
                   If g.MeasureString(test, txtPayAgainst.Font).Width >= txtPayAgainst.Width Then
                       txtPayAgainst.Text = test1
                       'GoTo l1
                   Else
                       test = test & " "
                       test1 = test
                   End If
               Next


               'l1:             txtPayAgainst.Text = test1
               Exit Sub
               'txtpayee2.Text = payee2.Substring(test1.Length)

               txtPayAgainst.Text = payee2.Substring(0, 10)
               txtpayee2.Text = payee2.Substring(10)

           Else
               txtPayAgainst.Text = payee2
               txtpayee2.Text = ""
           End If

       Catch ex As Exception
           MsgBox(ex.Message, , "Cheque Print")
       End Try

   End Sub
Posted
Comments
Ralf Meier 10-Jun-15 3:39am    
Your code is not complete clear for me.
Your Text-source is 'cboAccounts' - OK ...
But why 'cboAccounts.text' ? This must be changed to 'cboAccounts.selectedItem.toString'
But now my question :
to where should the 2 divided strings be written / assigned ?
Meer Wajeed Ali 10-Jun-15 4:10am    
In textbox Payee2
Ralf Meier 11-Jun-15 8:57am    
Have you seen my posted Solution ?
If Yes - does it work like you want ?
Meer Wajeed Ali 14-Jun-15 2:49am    
Yes it works for me...thanx

1 solution

I would do the inner part of your code like this :
VB
Dim myItem As String = cboAccounts.SelectedItem.ToString()
Dim p2words As String() = myItem.Split(" ")

Dim testText As String = ""
Dim partText1 As String = ""
Dim partText2 As String = ""

For Each word As String In p2words
    testText += " " + word
    If g.MeasureString(testText.Trim, txtPayAgainst.Font).Width >= txtPayAgainst.Width Then
        partText1 += " " + word
    Else
        partText2 += " " + word
    End If
Next

txtPayAgainst.Text = partText1.Trim
txtpayee2.Text = partText2.Trim


Please try it ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900