简体   繁体   中英

Vb.net appending lines of a textbox

        Case 0
            MyValues(0) = InputBox("Geef de wisselkoers (> 0 en < 500) voor de Euro tov Amerikaanse dollars (1 Eur = .... Amerikaanse Dollars")
            Do Until MyValues(0) >= 0 And MyValues(0) <= 500
                MsgBox("De parameters vallen buiten de wisselkoers. Lees de text nog maals in het volgende scherm")
                MyValues(0) = InputBox("Geef de wisselkoers (> 0 en < 500) voor de Euro tov Amerikaanse dollars (1 Eur = .... Amerikaanse Dollars")
            Loop
            Wisselkoersenlistbox.Text = String.Join(Environment.NewLine, MyValues)
        Case 2
            MyValues(1) = InputBox("Geef de wisselkoers (> 0 en < 500) voor de Euro tov Britse Ponden (1 Eur = .... Britse Ponden")
            Do Until MyValues(1) >= 0 And MyValues(1) <= 500
                MsgBox("De parameters vallen buiten de wisselkoers. Lees de text nog maals in het volgende scherm")
                MyValues(1) = InputBox("Geef de wisselkoers (> 0 en < 500) voor de Euro tov Amerikaanse dollars (1 Eur = .... Amerikaanse Dollars")
            Loop
            Wisselkoersenlistbox.Text = String.Join(Environment.NewLine, MyValues)

This works now :- ) Thank you very much!!!!! I've clicked a plus but can't give out till I reach 15 :(

As mentioned by @the_lotus in comments, you're trying to store date in your text box and manipulate it there. This presents a lot of issues. Much easier to store the information in an array, update it as required and then rebuild the textbox contents any time the info changes...

' Declare a variable to hold your messages. I've gone with 5 here but it can be any reasonable number (or you can use ReDim to change size later if you don't know from the start)
Dim MyMessages(5) As String {"Default Values go here (if any)", "Line2", "Line3", String.Empty, String.Empty}

' Update them as required in your existing Select Case statement

Case 0
    ' ... Other code ...
    ' Update message at index 0 (Line #1) to be whatever they input
    MyMessages(0) = InputBox("Geef de wisselkoers (> 0 en < 500) voor de Euro tov Amerikaanse dollars (1 Eur = .... Amerikaanse Dollars")
Case 1
    ' ... Other code ...
    ' Update message at index 1 (Line #2) to be "Something"...
    MyMessages(1) = "Something"

' ...etc...


' Finally, update the textbox so all messages are shown one-per-line

MessageBox1.Text = String.Join(Environment.NewLine, MyMessages)

Re: Comments on formatting

The correct syntax for what you're trying is this...

Wisselkoersenlistbox.Text = String.Join(" amerikaanse Dollar" & Environment.NewLine & "1 Euro = ", MyValues)

Unfortunately, that's not very helpful as it only puts the string between values, not in front of the first one or after the last one.

More useful would be to simply format the variables as they're added to the array...

Dim format = "1 Euro = {0:0.00} amerikaanse Dollar"
Dim input = Double.Parse(InputBox("..."))
MyMessages(x) = String.Format(format, input)

The format string we're providing shows where to put the number and also that it should always show 2 decimal places (no more/less).

It's worth noting that working with currencies using Single or Double data types is not recommended - as the numbers get larger, the accuracy decreases (just a side-effect of how floating point arithmetic works ). It might be fine for displaying info / working out approximations, but you should not use it for anything serious, especially when results are used together and errors can compound.

As an example, try doing something like:

Dim Total as Double
For i = 1 to 1000000
    Total = Total + 1 / 1000000
Next i

Intuitively, Total should be exactly 1 at the end of this loop. In fact, it will be a little above or below i .

For currencies, use the Decimal data type and specify the appropriate degree of accuracy required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM