views:

207

answers:

6

I'm using visual studios 2008, VB9 and I am trying to write an app that basically performs calculations on a set of data input by a user. During the calculations, I want to display the data at each step, and have it retained in the display area on the GUI (not overwritten by the next data being displayed).

For example:

UserInput = 1

Do

  UserInput += 1

  OutputLabel.Text = "UserInput " & UserInput

Loop Until UserInput = 5

and the output would look like

UserInput 1 UserInput 2 UserInput 3 UserInput 4 UserInput 5

I tried this, and other loop structures and can't seem to get things right. The actual app is a bit more sophisticated, but the example serves well for logical purposes.

Any tips are welcome, thanks!

+2  A: 

You need to concatenate the value in OutputLabel.Text.

OutputLabel.Text &= "UserInput " & UserInput

You might also want to reset it before the loop: OutputLabel.Text = ""

Ahmad Mageed
+2  A: 

This is the simple version:

Dim delimiter as String = ""
For UserInput As Integer = 1 To 5
    OutputLabel.Text &= String.Format("{0}UserInput {1}", delimiter, UserInput)
    delimiter = " "
Next

However, there are two problems with it and others like it (including every other answer given so far):

  1. It creates a lot of extra strings
  2. Since it's in a loop the label won't be able to process any paint events to update itself until you finish all of your processing.

So you may as well just do this:

Dim sb As New StringBuilder()
Dim delimiter As String = ""
For UserInput As Integer = 1 To 5
    sb.AppendFormat("{0}UserInput {1}", delimiter, UserInput)
    delimiter = " "
Next
OutputLabel.Text = sb.ToString()

And if you really want to have fun you can just do something like this (no loop required!):

OutputLabel.Text = Enumerable.Range(1, 5).Aggregate(Of String)("", Function(s, i) s & String.Format("UserInput {0} ", i))
Joel Coehoorn
I don't think I want to have that much fun :) I will try some of these and see what they look like
Jmichelsen
I ended up doing it very similar to your way here, thanks for the help!
Jmichelsen
A: 

If you need an iterated index you can try something like the following

For I As Integer = 1 To 5
     If I > 1 Then OutputLabel.Text &= " "
     OutputLabel.Text &= "UserInput " & I.ToString()
End For

If you have user inputs in a collection, you might better be served by using ForEach loop.

Joel Etherton
A: 

Do you need to do it in a GUI? If it is simply processing and putting out rows like that, maybe you should consider a console application, in which case it becomes REALLY easy, in simply calling

Console.WriteLine("my string")
eidylon
It does need to be in a GUI, I will try some of these methods and see what happens
Jmichelsen
A: 

I'd use a more appropriate control, like richtextbox

    Dim UserInput As Integer = 0
    RichTextBox1.Clear()
    Do
        UserInput += 1
        RichTextBox1.AppendText("UserInput " & UserInput.ToString.PadRight(3, " "c))
        RichTextBox1.Refresh() ''#for long loops add this
    Loop Until UserInput = 5
dbasnett
A: 

All of these ways actually work really well but the one that fit my situation the best was this:

Do
  Dim OutputString as String
  Application.DoEvents() 'to make things paint actively
  UserInput += 1
  OutputString = String.Format("{0}", UserInput)
  ListBox.Items.Add(OutputString)


Loop Until UserInput = 5

I changed things to a listbox but tried this same method with textboxes and labels, with some tweaks, they all worked very well. Thanks for all your help!

Jmichelsen