2 questions related to listbox and richtextbox

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I'm working on a program in vb2010.

A while ago I've created the same program in Delphi 2009, but wanted to use vb 2010 instead, so I try to recreate the same program in vb 2010.
Most of the things I was able to solve, but I run into 2 problems.

Let me first explain what the program does in a nutshell. I have a gridview from where I can copy, records into a listbox (works), from there I want to send those entries into a richtextbox on a second form (works also).

1 problem:
On the second form I also have a spinedit (from devexpress), with that when I press the increase button the same text just added to the richtextbox is copied (the number of times I press the increase button will determen the amount of copies), every copy must have 2 blank line between them (when I have multiple lines, these lines should be copied). For example:

test
test
test

test /these are the copies
test
test

Now the second problem, I also have a checkedcomboedit, when I check an item I want the value of that checked item to be placed inside the richtextbox at a certain line, in delphi I used the following line but it doesn't work here:

Richedit.Lines.Strings[2] := Format('Worshipservice: %s', [CheckListEdit1.Text])

Any help would be great.

Thanks
 
btw, I forgot to tell the code I have to copy the lines from the listbox to the richtextbox (another form).

VB.NET:
For x As Integer = 0 To ListBox1.Items.Count - 1
            printsonglist.RichTextBox1.Text &= (ListBox1.Items.Item(x)).ToString & vbCrLf
        Next x

Printsonglist is the other form.

I tried this code in a public sub to I could use it with the other form (trying to add more lines to the richtextbox, depending on the value of the spinedit).
 
Adding to the Text property of a control repeatedly is very inefficient. As you're using .NET 4.0, I suggest that you use a bit of LINQ. Here's how I would copy items from a ListBox to a RichTextBox multiple times:
VB.NET:
'Get the lines from the RichTextBox in editable form.
Dim lines As List(Of String) = Me.RichTextBox1.Lines.ToList()

'Get the items from the ListBox as Strings.
Dim newLines As String() = (From item In Me.ListBox1.Items
                            Select Me.ListBox1.GetItemText(item)).ToArray()

For count = 1 To copyCount
    'Add two empty lines.
    lines.AddRange({String.Empty, String.Empty})

    'Add the items from the ListBox.
    lines.AddRange(newLines)
Next

'Copy the new data back to the RichTextBox in a single batch.
Me.RichTextBox1.Lines = lines.ToArray()
 
As for the second question, you would again get the Lines from the RTB into a List, edit it and then convert it back to an array and assign it back to the Lines property. This time you would use the Insert method of the List.
 
Example code is just example code. Noone told you you could just copy and paste. How about you read the code and understand what it does? Then you will know how to change the code to work in your project.
 
Back
Top