Question Use numericupdown to multiply string in richtextbox

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
Ok here is what I have:

2 forms, on the first form I have a listbox which recieves it's items from a datagridview, when I click on a button all the items (in the listbox) are converted to a string and add to a richtextbox on the second form.

On the second form I have also a numericupdown, and here is what I run into.

With the numericupdown I want to multiply or remove blocks of strings from the richtextbox.

For example, when clicked on the button (form1) a string is add to the richtextbox, when I click (on form2) on the upbutton of the numericupdown I want to add the same string again below the existing one with 2 blank lines between them (this can be down a lot of times).
When I click the downbutton, a string should be removed.

In short term, the amount in the numericupdown should be the amount of string blocks in the richtextbox.

Here is the code I use to copy listbox.items to string and add them to the richtextbox:
VB.NET:
Expand Collapse Copy
Public Sub SGLists()
        Dim i As String
        For Each i In Me.ListBox1.Items
            printsonglist.RichTextBox1.Text = printsonglist.RichTextBox1.Text + i.ToString() & Environment.NewLine
        Next
    End Sub

Private Sub ButtonItem22_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonItem22.Click
        SGLists()
        printsonglist.ShowDialog()
    End Sub

Thanks
 
This is one way of doing it, in your second form have a property to hold the source string (that is to be duplicated x times)
VB.NET:
Expand Collapse Copy
Public Property SourceText As String
in your build up you address this instead of the RTB.Text, eg:
VB.NET:
Expand Collapse Copy
printsonglist.SourceText &= i.ToString & vbNewLine
then for ValueChanged event handler of the NUD in second form you can duplicate the SourceText accordingly, eg:
VB.NET:
Expand Collapse Copy
Dim copies(Me.NUDcount.Value - 1) As String
Me.RTB.Text = String.Join(vbNewLine, Array.ConvertAll(copies, Function() Me.SourceText))
 
Thanks for the reply,

I noticed that when I click the button, the RTB appears empty, I would like the first string to be already in the RTB.

Thanks again
 
Then you set it initially to the value currently in SourceText property, or better yet, set NUDcount.Value to 1 to have it generate the first value changed.
 
Do you mean by: set NUDcount.value to 1, in code or in design, because I have it already in design view set to 1.
 
I meant in code, to change its value to cause the text to be set. You understand that when the value changes the event handler set the text?
You can also use code to set the text manually initially if you prefer that.
 
I meant in code, to change its value to cause the text to be set. You understand that when the value changes the event handler set the text?
You can also use code to set the text manually initially if you prefer that.

Thanks yes, I understand it.

I tried to set the value (in the code) to - 0 or = 0, I still didn't get the result I would like to.
So maybe I miss something.

Please help me out.
 
Would assigning SourceText to the RTB.Text do it for you?
 
JohnH, i run into a problem,

The code worked for a while, when I click on the button on form 1, the listbox items are indeed added to the richtextbox of form2, but when I close form2 and reclick on button of form1, the items are added again to the richtextbox but are multiplied (see attachment), the items should be a single.

On formclosing the richtextbox is being emptied so that is not the problem.

Any idea what is wrong?
 

Attachments

  • Knipsel.JPG
    Knipsel.JPG
    15.2 KB · Views: 37
Since you initially assign SourceText to the RTB.Text I can only imagine that is the current content of SourceText.
 
yeah, I think it keeps the content of sourcetext in memory and when clicked the button it just adds the items extra.

Don't know how to solve that one yet.
 
I remember in first post you called ShowDialog. When you use this method it is your responsibility to Dispose dialog when returned.If you want to reuse same instance (your addressing the form through default instance now), ie choosing to not dispose it, you have to take measures to initialize the forms state yourself every time you call it.
 
Back
Top