[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

Locate a space character in a textbox

I would like to preform the following steps, however I am unsure on how to complete and I'm having trouble executing my code.

I would like to break a textbox into multiple lines of 40 character's each line. I made a previous post about this topic but I had trouble implementing the code.

I have a textbox called txtinput where you would put in the larger text block to be broken up. I would like to print out the smaller segments of text into textboxes of 40 character's each. The code should be executed by a button push called btnExecute. The code should look for the last space on the first line of a textbox to determine whether to break up the code or not. It should repeat the same behavior for each subsequent line looking at the last space character of that line to determine whether to break it up or not.
0
mlupino
Asked:
mlupino
  • 22
  • 21
1 Solution
 
mlupinoAuthor Commented:
I sent the comment to fast and I wanted to say thanks for anyone's assistance with this.
0
 
Babycorn-StarfishCommented:
Hi,

what code did you try previously?
0
 
mlupinoAuthor Commented:
   Private Function SetLineLength(ByVal intext As String, ByVal LineLength As Integer, Optional ByVal PreserveBreaks As Boolean = True, Optional ByVal LineTerminator As String = vbCrLf) As String
        Dim result As String = ""
        Dim oldLines As String()
        If PreserveBreaks Then
            oldLines = Regex.Split(intext, LineTerminator)
        Else
            ReDim oldLines(0)
            oldLines(0) = Regex.Replace(intext, LineTerminator, " ")
        End If
        For i As Integer = 0 To oldLines.Length - 1
            Dim n As String = ""
            Dim t As String = ""
            Dim s As String = oldLines(i)
            Do
                If s.Length > LineLength Then
                    t = s.Substring(0, LineLength)
                    Dim p As Integer = t.LastIndexOf(" ")
                    n &= s.Substring(0, p).Trim & LineTerminator
                    s = s.Substring(p).Trim
                Else
                    n &= s.Trim
                    Exit Do
                End If
            Loop
            If result <> "" Then
                result &= LineTerminator
            End If
            result &= n
        Next
        Return result
    End Function
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Babycorn-StarfishCommented:
Hi,

read your previous post and not really sure what you're after.

Is it just the case that you want to enter a string of lenght L and then break it up into 40 character chunks or is there more to it?
0
 
Babycorn-StarfishCommented:
Hi,

i put this together pretty quickly, it basically takes a line of text splits it into chunks of a set lenght and puts the text into textboxes

Imports System.Collections.Generic
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim inText As String = "ABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJ"
        Dim textList As New List(Of TextBox)
        textList.Add(Me.TextBox1)
        textList.Add(Me.TextBox2)
        textList.Add(Me.TextBox3)
        textList.Add(Me.TextBox4)
        textList.Add(Me.TextBox5)
        textList.Add(Me.TextBox6)

        SetLineLength(inText, 40, textList, True)
    End Sub

    Public Sub SetLineLength(ByVal InText As String, ByVal LineLength As Integer, ByRef TextBoxes As List(Of TextBox), Optional ByVal Validate As Boolean = False)
        If TextBoxes.Equals(Nothing) Then
            Throw New NullReferenceException("textBoxes must be an array of text boxes")
        End If

        If (Validate And InText.Length / LineLength > TextBoxes.Count) Then
            'Throw New ArgumentException("Not enough textboxes to fit all text")
            MessageBox.Show("Not enough textboxes to hold all of text")
        End If

        If InText.Length <= LineLength Then
            TextBoxes(0).Text = InText
            Return
        End If

        Dim chunks As List(Of String) = New List(Of String)
        Dim chunkStart As Int32 = 0

        Dim sb As StringBuilder = New StringBuilder()

        For i As Int32 = 0 To InText.Length - 1
            sb.Append(InText(i))
            If (sb.Length >= LineLength) Then
                chunks.Add(sb.ToString())
                sb.Remove(0, LineLength - 1)
            End If
        Next

        For j As Int32 = 0 To chunks.Count
            If j < TextBoxes.Count Then
                TextBoxes(j).Text = chunks(j)
            End If
        Next
    End Sub
End Class
0
 
mlupinoAuthor Commented:
Yes, pretty much take a look at a string of x length and break it up into 40 character chucks based on where the last space is on the line (as a word is shifted to the next line). For the purposes of this program output them to textboxes so I can get a favor of how this is done and I can go from their.

I tried your program and had several build errors:
1) namespace or type 'generic' from the imports 'system.collections.generic' cannot be found
2) comma, ')' or a valid expression continuation expected
3) name textlist is not declared
4) type list is not defined
5) array bounds cannot appear in type specifiers
6) expression is not an array or method and canot have an argument list
7) name chunks is not declared

A copy of the code I am using is below:
Imports System.Collections.Generic
Imports System.Text
Public Class Form1
    Inherits System.Windows.Forms.Form
 Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecute.Click
        Dim inText As String = "ABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJ"
        Dim textList As New List(Of TextBox)
        textList.Add(Me.TextBox1)
        textList.Add(Me.TextBox2)
        textList.Add(Me.TextBox3)
        textList.Add(Me.TextBox4)
        textList.Add(Me.TextBox5)
        textList.Add(Me.TextBox6)

        SetLineLength(inText, 40, textList, True)
    End Sub
    Public Sub SetLineLength(ByVal InText As String, ByVal LineLength As Integer, ByRef TextBoxes As List(Of TextBox), Optional ByVal Validate As Boolean = False)
        If TextBoxes.Equals(Nothing) Then
            Throw New NullReferenceException("textBoxes must be an array of text boxes")
        End If

        If (Validate And InText.Length / LineLength > TextBoxes.Count) Then
            'Throw New ArgumentException("Not enough textboxes to fit all text")
            MessageBox.Show("Not enough textboxes to hold all of text")
        End If

        If InText.Length <= LineLength Then
            TextBoxes(0).Text = InText
            Return
        End If

        Dim chunks As List(Of String) = New List(Of String)
        Dim chunkStart As Int32 = 0

        Dim sb As StringBuilder = New StringBuilder

        For i As Int32 = 0 To InText.Length - 1
            sb.Append(InText(i))
            If (sb.Length >= LineLength) Then
                chunks.Add(sb.ToString())
                sb.Remove(0, LineLength - 1)
            End If
        Next

        For j As Int32 = 0 To chunks.Count
            If j < TextBoxes.Count Then
                TextBoxes(j).Text = chunks(j)
            End If
        Next
    End Sub
End Class
0
 
mlupinoAuthor Commented:
I would like to substiute the following line in my code:
 Dim inText As String = "ABCDEFGHJIKLHKGMDJIFDHFJEHDHDHSDJEDJDJDJDDDDDJDJABCDEFGHJIKLHKGMD" to

Dim intext As String = TxtInput.Text

Txtinput.Text is equal to a multiline textbox.
0
 
mlupinoAuthor Commented:
Ok, I've been doing some more research online and I found a peice of code that will split a string based on a space. This is what I am looking for however I want to split a string based off of the last space. Here's an example:

Welcome to my program it splits a string of (represented in txtInput.text)

The program will locate the last space right before "of" and send of to the next line.

Here's some of the code I found:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Str As String = txtInput.Text
        Dim TempStr() As String = Str.Split(" ")
        For i As Integer = 0 To TempStr.Length - 1
            MessageBox.Show(TempStr(i))
        Next
    End Sub
0
 
Babycorn-StarfishCommented:

try this:


    Public Sub SetLineLength(ByVal InText As String, ByVal LineLength As Integer, ByRef TextBoxes As List(Of TextBox), Optional ByVal Validate As Boolean = False)
        Dim chunks As String() = InText.Split(" ")
        Dim line As StringBuilder = New StringBuilder(LineLength)

        Dim textIndex As Int32 = 0
        For index As Int32 = 0 To chunks.Length - 1
            If (line.Length + chunks(index).Length + " ".Length) > LineLength Then
                TextBoxes(textIndex).Text = line.ToString()
                line.Remove(0, line.Length - 1)
                textIndex += 1
                If textIndex >= TextBoxes.Count Then
                    'no more textboxes available
                    Return
                End If
            Else
                line.Append(chunks(index) + " ")

            End If
        Next
End Sub

You'll need to work in some code to validate the parameters passed to the method - null references etc

incidentally are you using .Net1.1 or 2.0?
0
 
Babycorn-StarfishCommented:
this basically splits everything on the space like you want and builds a string that will be assigned to the textbox.text property, it checks whether adding the next word/chunk plus a space will take it over the desired length and keeps adding to it until this is reached, it then outputs the text and resets the stringbuilder
0
 
mlupinoAuthor Commented:
I have some errors that popped up in that last Public Sub you gave me: (10.17.2007 at 04:22PM EDT, ID: 20096731)

Type "list is not defined"
Array bounds cannot have type specifers
Type StringBuilder is not defined

I am not sure how to clear them, its been awhile since I programmed and I'm slowly trying to get back into the groove.

I have .Net Framework 2.0 installed but I am programming in VB.Net 2003. Not sure how to determine what framework it is using? I believe its 2 but not sure.
0
 
mlupinoAuthor Commented:
I don't want to split every space just the last space on a single line of 40. I am trying to use the code I found online and modify it but I'm not having much luck
0
 
Babycorn-StarfishCommented:
If you're using VB.Net 2003 it only uses .Net 1.1.

The stuff you mentioned is exclusive to .Net 2.0, the generics namespace containts the list type etc.

Okay the workaround would be....
Change:
ByRef TextBoxes As List(Of TextBox)
to
ByRef List As TextBox() 'i.e. an array

Change:
If textIndex >= TextBoxes.Count Then
'no more textboxes available
      Return
End If

to

If textIndex >= TextBoxes.Length Then
      'no more textboxes available
       Return
End If

and add imports System.Text to the top of your class/module so you can use the StringBuilder class.
0
 
Babycorn-StarfishCommented:

mlupino:
I don't want to split every space just the last space on a single line of 40

Using the split function will split on every space or whatever character(s) you ask it to split on.
0
 
mlupinoAuthor Commented:
I have an error message that says Text boxes is not declared, what did I forget to do?
0
 
Babycorn-StarfishCommented:
Sorry, dumb error:

when i said:
Change:
ByRef TextBoxes As List(Of TextBox)
to
ByRef List As TextBox() 'i.e. an array

it should have been to
ByRef TextBoxes As TextBox()

sry
0
 
mlupinoAuthor Commented:
Not a problem. Thanks for the tip about .net Framework. The last programming course I took was two years ago so I'm trying to slowly get back into a groove. I hope you don't mind but I have a few questions regarding the code you provided. I thank you for your patience and assistance thus far. I placed some comments in the code below and I want to make sure Im on the same page. If Im wrong at all, would you mind correcting me?

I moved the code into a button click event and placed the variables in their that need to be called and executed. I am still unsure of a few things in the code and how it works.

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'declare variables
        Dim intext As String
        Dim LineLength As Integer
        'declare arrays
        Dim TextBoxes As TextBox()
        Dim chunks As String() = intext.Split(" ")
        ' I am not sure how this array is being used ??
        Dim line As StringBuilder = New StringBuilder(LineLength)
        ' I have never used StringBuilder so I'm not sure how this fits in

        intext = txtInput.Text
        'copy text of textinput.text to string that will be broken up
        LineLength = 40
'declare that the max line length must be 40 characters to be read before reading the next 40 characters

        Dim textIndex As Int32 = 0
        'setup index for number of textboxes to be created
        For index As Int32 = 0 To chunks.Length - 1
            ' this will run until the number of remaining text is equal to zero
            If (line.Length + chunks(index).Length + " ".Length) > LineLength Then
                ' If the length is greater than 40 characters then execute below if block
                TextBoxes(textIndex).Text = line.ToString()
                'assign first chunk to textbox1
                line.Remove(0, line.Length - 1)
                'increment to next line
                textIndex += 1
                'add 1 to TextIndex so that data will be appended TextBox2 etc...
                If textIndex >= TextBoxes.Length Then
                    'no more textboxes available
                    Return
                End If
            Else
                line.Append(chunks(index) + " ")
                'append data after space to next line

            End If
        Next
        'repeat For Next until no more text

    End Sub
End Class
0
 
Babycorn-StarfishCommented:
Hi again,

please ask as much as you like, no problems with it at all :)

I took the above and recommented it to explain it, hopefully it will but first i'll explain why the stringbuilder is used. Whenever you create a string, a new string object is creatred, if you add something to that string a new string is created, so every time you alter it a new string is created using memory and processor time. With a stringbuilder it store the data in a manner that can be changed without the need to create a new instance of the object, so in the code below it is used to add the chunks until we reach the desired length.

I added an extra if block to first check whether the chunk on its own would exceed the length

'declare variables used
        Dim intext As String
        Dim LineLength As Integer
        Dim TextBoxes As TextBox()
        Dim line As StringBuilder = New StringBuilder(LineLength)

        'get text from txtInput and store in string
        intext = txtInput.Text

        'take contents of intext and split into array, this results in an array of strings
        'equal in size to the number of separate words e.g."Hello everyone how are you today"
        ' would give an array of 6 strings "Hello", "everyone", "how", "are", "you", "today"
        Dim chunks As String() = intext.Split(" ")

        'set the maximum length of a line
        LineLength = 40

        'index into first element of array of TextBox, i.e. TextBoxes
        Dim textIndex As Int32 = 0

        'iterate over each string in chunks
        For index As Int32 = 0 To chunks.Length - 1
            ' If the current length of line (the stringbuilder)plus the current chunk or plus a space  
            'is greater than 40 characters then execute below if block
            If (line.Length + chunks(index).Length) > LineLength Then
                'assign first line of 40 characters extracted to current textbox
                TextBoxes(textIndex).Text = line.ToString()
                'reset the stringbuilder by removing everything it currently holds
                line.Remove(0, line.Length - 1)
                'increment to next textbox the will be used
                textIndex += 1
                If textIndex >= TextBoxes.Length Then
                    'no more textboxes available
                    Return
                End If
            ElseIf (line.Length + chunks(index).Length + " ".Length) > LineLength Then
                'assign first line of 40 characters extracted to current textbox
                TextBoxes(textIndex).Text = line.ToString()
                'reset the stringbuilder by removing everything it currently holds
                line.Remove(0, line.Length - 1)
                'increment to next textbox the will be used
                textIndex += 1
                If textIndex >= TextBoxes.Length Then
                    'no more textboxes available
                    Return
                End If
            Else
                'append current data plus a space current line
                line.Append(chunks(index) + " ")
            End If
        Next
        'repeat For Next until no more text

    End Sub
End Class
0
 
mlupinoAuthor Commented:
I had a feeling that we were both on different pages I was looking for a function more like this:

your comments:
        'get text from txtInput and store in string
        intext = txtInput.Text

        'take contents of intext and split into array, this results in an array of strings
        'equal in size to the number of separate words e.g."Hello everyone how are you today"
        ' would give an array of 6 strings "Hello", "everyone", "how", "are", "you", "today"
        Dim chunks As String() = intext.Split(" ")

My comments:
        'get text from txtInput and store in string
        intext = txtInput.Text

        'take contents of intext and split into array, this results in an array of strings
        'take the first 40 characters of a string "Hello everyone how are you today my name is Michael"
        ' the 40th character is between m & e, find space character at 37, copy 1-37 to textbox1 return and copy character 37-77 to textbox 2. Repeat function from 77-117 if text is present.
       
        'result for example would be two strings of 40 characters each.

' to validate, it would be nice to see on the form the textbox output. For some reason, I can't see what happens after the last comment you sent me but i suspect the program is executing OK.

.
0
 
Babycorn-StarfishCommented:
I see what you mean, so basically:

start at the beginning of a string, (positionStart= 0) and go to the 40th character (i.e.39), if it isn't a space back track until you get to a space (positionEnd = n) now chop from positionStart to positionEnd, update positionStart to positionEnd + 1 and loop until you either get to end of string or run out of output textboxes

0
 
mlupinoAuthor Commented:
Yep, basically. I should of been more clearer and explained that from the beginning.
0
 
Babycorn-StarfishCommented:
no probs, i guess i just had an idea and went for it lol, anyway try this. Please note though that
>>>  Dim textBoxes As TextBox() = textList.ToArray()
on the right of the equal you need to change that to an array of textboxes that you're using, the textList is only if you used a List collection.

 'declare variables used
        Dim intext As String = "Detail from a Conservative poster urging Gordon Brown to call a referendum over the EU treaty. Gordon Brown arrived in Lisbon today to put the finishing touches to an EU treaty that he hopes will avert the need for a British referendum" 'txtInput.Text
        Dim lineLength As Integer = 40
        Dim textBoxes As TextBox() = textList.ToArray()

        'Dim line As StringBuilder = New StringBuilder(lineLength)
        'index into first element of array of TextBox, i.e. TextBoxes
        Dim textIndex As Int32 = 0
        Dim startPos As Int32 = 0
        Dim endPos As Int32 = 0
        Dim processing As Boolean = True

        Dim line As StringBuilder = New StringBuilder(lineLength)
        While processing
            endPos = startPos + lineLength - 1

            'check we wont go over the end
            If endPos >= intext.Length Then
                If textIndex >= textBoxes.Length Then
                    'no more textboxes available
                    Return
                Else
                    'set text to string
                    textBoxes(textIndex).Text = intext.Substring(startPos)
                    Return
                End If

            End If
            'back up if needed
            While Not intext(endPos) = " " And endPos <> startPos
                endPos -= 1
            End While
            line.Append(intext.Substring(startPos, endPos - startPos))

            If textIndex >= textBoxes.Length Then
                'no more textboxes available
                Return
            Else
                'set text to string
                textBoxes(textIndex).Text = line.ToString()
                line.Remove(0, line.Length)
            End If
            'update start and end positions
            startPos = endPos + 1
            endPos = startPos
            textIndex += 1
            If startPos > intext.Length - 1 Then
                processing = False
            End If
        End While
    End Sub
0
 
mlupinoAuthor Commented:
I got a message about TextList is not declared and a message about

while not intext saying that expression is not an array or method and cannot have an argument list

What did you mean by changing it to the array I'm using, a bit confused?
0
 
Babycorn-StarfishCommented:
hmmm,

okay i assumed you would be passing an array of textboxes as previously, so if you have say 6 textboxes on your form, create an array and fill the array with references to the textboxes.

intext should be a string and therefore by passing an int in brackets you access the character at a particular position, if that is'nt working see whether intext.charAt(endPos) works, this has the same effect of getting a specific character.

If you're not using textboxes anymore, post your code for me to look at.

hth
0
 
Babycorn-StarfishCommented:
any joy with my suggestions?
0
 
mlupinoAuthor Commented:
I wanted to post my code from friday but I didn't have the chance to get around to it today, I am going to take a fresh look at it tomorrow and I'll post what if I have or where I am at.

Thanks again for your assistance.
0
 
mlupinoAuthor Commented:
Hello. Sorry for the delay its been a busy week. Here is where I am stumbling at:
Name TextList is not declared
Expression is not an array or method and cannot have an arguement list

Here is my most recent code from Button2Click.

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'declare variables used
        Dim intext As String = "Detail from a Conservative poster urging Gordon Brown to call a referendum over the EU treaty. Gordon Brown arrived in Lisbon today to put the finishing touches to an EU treaty that he hopes will avert the need for a British referendum" 'txtInput.Text
        Dim lineLength As Integer = 40
        Dim textBoxes As TextBox() = textList.ToArray()

        'Dim line As StringBuilder = New StringBuilder(lineLength)
        'index into first element of array of TextBox, i.e. TextBoxes
        Dim textIndex As Int32 = 0
        Dim startPos As Int32 = 0
        Dim endPos As Int32 = 0
        Dim processing As Boolean = True

        Dim line As StringBuilder = New StringBuilder(lineLength)
        While processing
            endPos = startPos + lineLength - 1

            'check we wont go over the end
            If endPos >= intext.Length Then
                If textIndex >= textBoxes.Length Then
                    'no more textboxes available
                    Return
                Else
                    'set text to string
                    textBoxes(textIndex).Text = intext.Substring(startPos)
                    Return
                End If

            End If
            'back up if needed
            While Not intext(endPos) = " " And endPos <> startPos
                endPos -= 1
            End While
            line.Append(intext.Substring(startPos, endPos - startPos))

            If textIndex >= textBoxes.Length Then
                'no more textboxes available
                Return
            Else
                'set text to string
                textBoxes(textIndex).Text = line.ToString()
                line.Remove(0, line.Length)
            End If
            'update start and end positions
            startPos = endPos + 1
            endPos = startPos
            textIndex += 1
            If startPos > intext.Length - 1 Then
                processing = False
            End If
        End While
    End Sub
End Class
0
 
Babycorn-StarfishCommented:
Hi,

okay what you need is an array of textboxes for this code to work.
In my original code i stuck them in a generic list and then call the ToArray() method giving me an array of TextBox:
Dim textBoxes As TextBox() = textList.ToArray()

So first things first, does your form have textboxes on it?
0
 
mlupinoAuthor Commented:
Not yet, I probably need to add them first.
0
 
mlupinoAuthor Commented:
Am I correct?
0
 
Babycorn-StarfishCommented:
Yeah,

the code above assumes you want to output to textboxes, if you want some other kind it can be modified. Assuming your happy with the textboxes, add them to the form, then add them to an array of textboxes called textBoxes, make this globally visible and remove the following line:

Dim textBoxes As TextBox() = textList.ToArray()
0
 
mlupinoAuthor Commented:
I asked a VB programmer for some help and she instructed me on how to create an Array. However she is using VB6 and previously showed me that if I named a control i.e. TxtOutput (the name I'm going to use) and use copy and paste, it would ask me to create a control array. It does not provide the same functionality in VB.net 2003. How would I do it their?
0
 
Babycorn-StarfishCommented:
Yeah, there's no control arrays in VB.Net. To replicate this you would need to:

1)Create N textboxes beforehand
2)Create the array of length N
3) Assign textboxes to the array starting at index 0

e.g.
        Dim N As Integer = 2
        Dim textBoxes(N) As TextBox
        textBoxes(0) = Me.TextBox1
        textBoxes(1) = Me.TextBox2
        'textboxes can now be accessed through array
        textBoxes(0).Text = "Hello"
        textBoxes(1).Text = "World"
0
 
mlupinoAuthor Commented:
Ok, I feel like I'm almost there. I still have an error on this line:

 While Not intext(endPos) = " " And endPos <> startPos

Expression is not an array or method, and cannot have an argument list.

Do I need to create another array for intext. However intext is what is getting passed in and broken up into textboxes.
0
 
Babycorn-StarfishCommented:
From earlier

"intext should be a string and therefore by passing an int in brackets you access the character at a particular position, if that is'nt working see whether intext.charAt(endPos) works, this has the same effect of getting a specific character."

did you try this?
0
 
mlupinoAuthor Commented:
I tried typing in intext.charAT in VB.net 2003 and I can't seam to find CharAT. I did find Chars which i believe might be different. I assume you want me to modify this line with CharAT but i could be wrong.

         While Not intext.charAT(endPos) = " " And endPos <> startPos
                endPos -= 1
            End While
0
 
Babycorn-StarfishCommented:
0
 
mlupinoAuthor Commented:
It doesn't recongize AT so I assume I leave it off. I ran into an error so I put it into debug mode. I see it calculated the number of characters as 33. What i assume will happen is that it will copy it to textbox(1).text however it is not. I think it could be because I started my array with 1 and not zero however I am not so sure. I did start N at 12 which could be 1 for the starting position however it ran into another problem their where it disappeared if I decleared the array size of 12. I am getting this error message: Object reference not set to an istance of an object on the line with  textBoxes(textIndex).Text = line.ToString()

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        'creation of array of textboxes
        Dim N As Integer = 12
        Dim textBoxes(N) As TextBox
        textBoxes(1) = Me.TextBox1
        textBoxes(2) = Me.TextBox2
        textBoxes(3) = Me.TextBox3
        textBoxes(4) = Me.TextBox4
        textBoxes(5) = Me.TextBox5
        textBoxes(6) = Me.TextBox6
        textBoxes(7) = Me.TextBox7
        textBoxes(8) = Me.TextBox8
        textBoxes(9) = Me.TextBox9
        textBoxes(10) = Me.TextBox10
        textBoxes(11) = Me.TextBox11
        textBoxes(12) = Me.TextBox12

        'textboxes can now be accessed through array

        'declare variables used
        Dim intext As String = "Detail from a Conservative poster urging Gordon Brown to call a referendum over the EU treaty. Gordon Brown arrived in Lisbon today to put the finishing touches to an EU treaty that he hopes will avert the need for a British referendum" 'txtInput.Text
        Dim lineLength As Integer = 40

        'Dim line As StringBuilder = New StringBuilder(lineLength)
        'index into first element of array of TextBox, i.e. TextBoxes
        Dim textIndex As Int32 = 0
        Dim startPos As Int32 = 0
        Dim endPos As Int32 = 0
        Dim processing As Boolean = True

        Dim line As StringBuilder = New StringBuilder(lineLength)
        While processing
            endPos = startPos + lineLength - 1
            'check we wont go over the end
            If endPos >= intext.Length Then
                If textIndex >= textBoxes.Length Then
                    'no more textboxes available
                    Return
                Else
                    'set text to string
                    textBoxes(textIndex).Text = intext.Substring(startPos)
                    Return
                End If

            End If

            'back up if needed
            While Not intext.Chars(endPos) = " " And endPos <> startPos
                endPos -= 1
            End While
            line.Append(intext.Substring(startPos, endPos - startPos))

            If textIndex >= textBoxes.Length Then
                'no more textboxes available
                Return
            Else
                'set text to string
                textBoxes(textIndex).Text = line.ToString()
                line.Remove(0, line.Length)
            End If
            'update start and end positions
            startPos = endPos + 1
            endPos = startPos
            textIndex += 1
            If startPos > intext.Length - 1 Then
                processing = False
            End If
        End While
    End Sub
0
 
Babycorn-StarfishCommented:
This works:

I just started the textboxes being added from element 0 up to 11, this means that the following line:
'textBoxes(textIndex).Text = intext.Substring(startPos)' will work properly as on the first pass through index = 0, so  it gets the object at element 0 which is Null and tries to do something with it.

    'creation of array of textboxes
        Dim N As Integer = 12
        Dim textBoxes(N) As TextBox
        textBoxes(0) = Me.TextBox1
        textBoxes(1) = Me.TextBox2
        textBoxes(2) = Me.TextBox3
        textBoxes(3) = Me.TextBox4
        textBoxes(4) = Me.TextBox5
        textBoxes(5) = Me.TextBox6
        textBoxes(6) = Me.TextBox7
        textBoxes(7) = Me.TextBox8
        textBoxes(8) = Me.TextBox9
        textBoxes(9) = Me.TextBox10
        textBoxes(10) = Me.TextBox11
        textBoxes(11) = Me.TextBox12

        'textboxes can now be accessed through array

        'declare variables used
        Dim intext As String = "Detail from a Conservative poster urging Gordon Brown to call a referendum over the EU treaty. Gordon Brown arrived in Lisbon today to put the finishing touches to an EU treaty that he hopes will avert the need for a British referendum" 'txtInput.Text
        Dim lineLength As Integer = 40

        'Dim line As StringBuilder = New StringBuilder(lineLength)
        'index into first element of array of TextBox, i.e. TextBoxes
        Dim textIndex As Int32 = 0
        Dim startPos As Int32 = 0
        Dim endPos As Int32 = 0
        Dim processing As Boolean = True

        Dim line As System.Text.StringBuilder = New System.Text.StringBuilder(lineLength)
        While processing
            endPos = startPos + lineLength - 1
            'check we wont go over the end
            If endPos >= intext.Length Then
                If textIndex >= textBoxes.Length Then
                    'no more textboxes available
                    Return
                Else
                    'set text to string
                    textBoxes(textIndex).Text = intext.Substring(startPos)
                    Return
                End If

            End If

            'back up if needed
            While Not intext.Chars(endPos) = " " And endPos <> startPos
                endPos -= 1
            End While
            line.Append(intext.Substring(startPos, endPos - startPos))

            If textIndex >= textBoxes.Length Then
                'no more textboxes available
                Return
            Else
                'set text to string
                textBoxes(textIndex).Text = line.ToString()
                line.Remove(0, line.Length)
            End If
            'update start and end positions
            startPos = endPos + 1
            endPos = startPos
            textIndex += 1
            If startPos > intext.Length - 1 Then
                processing = False
            End If
        End While
    End Sub
0
 
Babycorn-StarfishCommented:
as an aside, set the value of N to one less than it should be. VB seems to create create elemenets between zero and N, thus in our case giving us 13 elements 0..12, one more than we need
0
 
mlupinoAuthor Commented:
I had a feeling that is what it was. I am still glad that after 3 years of not programming, I still understand whats going on.
0
 
mlupinoAuthor Commented:
I will assign you all the points. I only wish i could assign more to you since you were patient and extremely helpful. Thank you so much for your assistance. It is clear that I need to continue to brush up so I think I am going to buy a good book on VB.net.
0
 
Babycorn-StarfishCommented:
Hey no problem!!

There's lots of good stuff on VB.Net on the internet, although wading through the rubbish is a bit annoying. I've given up on books and just think of something i want to do and try and google around the subject, books are just too dear plus i find that there's always at least 50% stuff i already know before i get to the good stuff.

Oh, download Visual Basic.Net 2005 express, it's free and has a lot of useful stuff.
http://msdn2.microsoft.com/en-us/express/aa718406.aspx


Good luck
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 22
  • 21
Tackle projects and never again get stuck behind a technical roadblock.
Join Now