Share via


Visual Basic Form Currency Converter

Question

Sunday, November 24, 2019 2:50 PM

Hello,

I'm programming as a hobby and I'm making a currency converter (Windows Form) at the moment with the following currencies:

- Euro

- US Dollar

- Canadian Dollar

- Swiss Franc

In the windows form a user can click on the currency and a InputBox ask how much the value is 1 US Dollar = (insert Value) in Euro or Canadian Dollar or Swiss Franc. The InputBox is made with several variables, when someone click US Dollar, the string automatically says US Dollar.  

I have made the following ListBox:

  Private Sub Converting_Load(sender As Object, e As EventArgs) Handles MyBase.Load

       Currencylist.Items.Add(EUR)

       Currencylist.Items.Add(USD)

       Currencylist.Items.Add(CAND)

       Currencylist.Items.Add(SWISS)

    End Sub

The select case statement:

Select Case Currencylist.SelectedIndex
            Case 0, 1, 2, 3
                InputCurrency = InputBox("Give the currency 1 USD = & Currencylist.SelectedItem")                Currencyresult.Text = Currencyresult.test & InputCurrency & Currencylist.SelectedItemEnd Select
End Sub

For example I say: 1 dollar is 6 Euro, the result of this input has to be copied in the TextBox "Currencyresult" I made. The result have to be in the same order how the Currencylist is build up. Start with EUR and ending with SWISS:

1 USD = 2 EUR

1 USD = 1 USD

1 USD = 1.5 CAND

1 USD = 1.75 SWISS

It won't work. When I start for example with the currency of SWISS, the TextBox will start with:

1 USD = 1.75 SWISS.

What I'm doing wrong?

Thanks in advance.

Jonathan.

All replies (17)

Friday, November 29, 2019 9:14 AM ✅Answered

Hi Jonathan.12,

Thank you for feedback.

For your requirement, you need a list table to save input value, and you could refer this sample

Public Class Form1

    Private iList As New List(Of String)()
    Private count As Int32 = 0

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("Euro")
        ListBox1.Items.Add("US")
        ListBox1.Items.Add("Canadian")
        ListBox1.Items.Add("Swiss Franc")

        For Each area As String In ListBox1.Items
            iList.Add(area)
        Next

        TextBox1.Enabled = False
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim InputCurrency As String
        Select Case Me.ListBox1.SelectedIndex
            Case 0, 1, 2, 3
                InputCurrency = InputBox("How much" & ListBox1.SelectedItem & " is")
                TextBox1.Text = InputCurrency & vbCrLf

                iList(ListBox1.SelectedIndex) = InputCurrency
                count = count + 1
                If count = ListBox1.Items.Count Then
                    TextBox1.Clear()
                    For Each val As String In iList
                        TextBox1.Text += val & vbCrLf
                    Next
                    count = 0
                End If
        End Select
    End Sub

End Class

It Could show every input and after 4 inputs it will show all inputs in the same order as the ListBox in the TextBox.

Any feedback will be expected.

Best Regards,

Dylan

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected]


Saturday, November 30, 2019 2:24 AM ✅Answered

Hi Jonathan,

Sorry, I can hardly understand what you want to do.

I've made a sample by guess.    
and shared it via OneDrive.
Please download Convert_Currency.zip (zip file) and try it. 

Code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    With Me.ListBox1
        .SelectionMode = SelectionMode.MultiSimple
        .Items.Add("EUR")
        .Items.Add("USD")
        .Items.Add("CAD")
        .Items.Add("CHF")
    End With
End Sub
' 
Private Sub btn_Convert_Click(sender As Object, e As EventArgs) Handles btn_Convert.Click
    Dim InputCurrency As String
    Me.TextBox1.Text = ""
    For i As Integer = 0 To Me.ListBox1.Items.Count - 1
        If (Me.ListBox1.GetSelected(i)) Then
            InputCurrency = InputBox("Give the currency 1 USD = ??? " & Me.ListBox1.Items(i))
            Me.TextBox1.Text += Me.ListBox1.Items(i) & " = " & InputCurrency & vbCrLf
        End If
    Next
End Sub

Regards,

Ashidacchi -- http://hokusosha.com


Sunday, December 1, 2019 12:05 AM ✅Answered

Hi Jonathan,

Add this code to the code I shared yesterday.

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    Dim InputCurrency As String
    InputCurrency = InputBox("Give the currency 1 USD = ??? " & Me.ListBox1.SelectedItem)
    Me.TextBox1.Text += Me.ListBox1.SelectedItem & " = " & InputCurrency & vbCrLf
End Sub

Regards,

Ashidacchi -- http://hokusosha.com


Sunday, November 24, 2019 11:57 PM

Hi Jonathan,

I'm not sure if I can understand what you want to make. But...

If "Currencylist" is a ListBox, code in Converting_Load should be like this:

With Me.ListBox1
    .Items.Add("EUR")
    .Items.Add("USD")
    .Items.Add("CAND")
    .Items.Add("SWISS")
End With

And Select Case statement should be like this:

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    Select Case Me.ListBox1.SelectedIndex
        Case 0
            ' -- do something for EUR
        Case 1
            ' -- do something for USD
        Case 2
            ' -- do something for CAND
        Case 3
            ' -- do something for SWISS
    End Select
End Sub

Regards,

Ashidacchi -- http://hokusosha.com


Monday, November 25, 2019 12:07 AM

Your problem is that you are involving all items.

Instead of select, use

if item.index <> lbx.SelectedIndex then

...

end if

George Frias - AWWshop @: Wikidot, GitHub


Monday, November 25, 2019 5:59 AM

Thanks for the reply, is it also possible that I can use one InputBox in my code to ask for the different values? I want to optimalize my code. When someone select a currency like SWISS the InputBox ask for input. But the text of the InputBox is variable to the chosen currency. It is one InputBox code for 4 cases. That is the reason that I involved all items in my code. The result of the code: the given input value is stored in a TextBox for 4 different currencies in the some order as the Listbox, beginning with EUR and end with SWISS. The instruction book I use says that it is possible to use one InputBox for multiple cases. Thanks in advance, Jonathan


Monday, November 25, 2019 7:04 AM

Hi Jonathan,

If you want the ListBox to allow multiple selectioin, make "SelectionMode" = MultiSimple.
    

In Design View, 
    
In code,

With Me.ListBox1
    .SelectionMode = SelectionMode.MultiSimple ' --<<
    .Items.Add("EUR")
    .Items.Add("USD")
    .Items.Add("CAND")
    .Items.Add("SWISS")
End With

For my clear understanding, could you insert a screenshot of your Form in your post?
Or share it via cloud storage such as OneDrive, Dropbox, etc.

Regards,

Ashidacchi -- http://hokusosha.com


Tuesday, November 26, 2019 9:39 AM

Hi Jonathan.12,

Sorry for delay in reply.

As far as I know, we could not use one InputBox command to run multiple cases. Please refer this MSDN document about InputBox function.

So we suggest you could organize the string before inputting, please refer this sample:

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim statement As String
        For Each area As String In ListBox1.Items
            If statement Is Nothing Then
                statement = "Give the currency 1 USD =" & area
            Else
                statement = statement & vbCrLf & "Give the currency 1 USD =" & area
            End If
        Next

        Dim InputCurrency As String
        Select Case Me.ListBox1.SelectedIndex
            Case 0, 1, 2, 3
                InputCurrency = InputBox(statement)
                TextBox1.Clear()
                TextBox1.Text = InputCurrency & ListBox1.SelectedItem
        End Select
    End Sub

Hope it could help you, and any feedback will be expected.

Best Regards,

Dylan

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected]


Tuesday, November 26, 2019 3:57 PM

The multiple ListBox is working, thank you very much. I understand this now. One thing I don't understand. 

I have made this example to explain what I don't understand:

- I want for every case a InputBox in another currency program. 

- I want that the given Input is stored in the same way as the ListBox. Start with EUR and end with SWISS currency. For example: the input name for EUR is EURVALUE.

- The Input is stored in the textbox OutputCurrency. For example the string for EUR: OutputCurrency.text = "1 USD =" &  EURVALUE. 

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    Select Case Me.ListBox1.SelectedIndex
        Case 0
            ' -- do something for EUR
        Case 1
            ' -- do something for USD
        Case 2
            ' -- do something for CAND
        Case 3
            ' -- do something for SWISS
    End Select
End Sub

If someone start with the SWISS currency, then EUR,  then CAND and then USD. How can the program give the output in the OutputCurrency textbox in the same order as the ListBox:

- "1 USD =" &  EURVALUE. 

- "1 USD =" &  USDVALUE. 

- "1 USD =" &  CANDVALUE.

- "1 USD =" &  SWISSVALUE.

The OutputCurrency textbox have to update/refresh after every input.

Sorry for all the questions.

Thanks in advance,

Jonathan.


Friday, November 29, 2019 1:21 AM

Hi Jonathan,

I'm not sure if I can understand what you want to do.
So, I've made a sample by guess.
    

  1. define an array for currency in your class. 
Private aryCurrency() As String =
    {"2 EUR", "1 USD", "1.5 CAND", "1.75 SWISS"}

2) create a button [btn_Currency] and write code like this.

Private Sub btn_Currency_Click(sender As Object, e As EventArgs) Handles btn_Currency.Click
    For i As Integer = 0 To Me.ListBox1.Items.Count - 1
        If (Me.ListBox1.GetSelected(i)) Then
            MsgBox("1 USD = " & Me.aryCurrency(i))
        End If
    Next
End Sub

Regards,

Ashidacchi -- http://hokusosha.com


Friday, November 29, 2019 6:03 AM

Thanks for the reply. For every case in the ListBox I want a InputBox that asks the user for input. The input have to be stored in a normal TextBox where the TextBox is updated after every input. In the end (after 4 Inputs) the TextBox has to be in the same order as the ListBox: starting with EUR and ending with SWISS. Example: If I click on case 0 the program asks me how much 1 EUR is. I have a input for example of 1 USD = 1,25 EUR. The string 1 USD = 1,25 EUR is shown in the TextBox. In this form I don’t use a button. Only a ListBox and a TextBox for the output. The TextBox is read-only, it gives only output. Thanks in advance. Jonathan.


Friday, November 29, 2019 6:11 AM

Hi Jonathan,

I hope you will share your project via cloud storage such as OneDrive, Dropbox, etc.  That will help me to understand your requirements more clearly.

Regards,

Ashidacchi -- http://hokusosha.com


Friday, November 29, 2019 9:03 PM

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Listbox1.Items.Add("EUR")

        Listbox1.Items.Add("US")

        Listbox1.Items.Add("Canadian")

        Listbox1.Items.Add("SWISS")


    End Sub
    Private Sub Listbox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Listbox1.SelectedIndexChanged
        Dim InputCurrency As String
        Select Case Me.Listbox1.SelectedIndex
            Case 0
                InputCurrency = InputBox("Give the currency 1 USD = " & Listbox1.SelectedItem & " is")
                OutputCurrency.Text = OutputCurrency.Text & InputCurrency & " " & Listbox1.SelectedItem & vbCrLf
            Case 1
                InputCurrency = InputBox("Give the currency 1 USD = " & Listbox1.SelectedItem & " is")
                OutputCurrency.Text = OutputCurrency.Text & InputCurrency & " " & Listbox1.SelectedItem & vbCrLf
            Case 2
                InputCurrency = InputBox("Give the currency 1 USD = " & Listbox1.SelectedItem & " is")
                OutputCurrency.Text = OutputCurrency.Text & InputCurrency & " " & Listbox1.SelectedItem & vbCrLf
            Case 3
                InputCurrency = InputBox("Give the currency 1 USD = " & Listbox1.SelectedItem & " is")
                OutputCurrency.Text = OutputCurrency.Text & InputCurrency & " " & Listbox1.SelectedItem & vbCrLf
        End Select
    End Sub

End Class

Thank you very much for the reply. This is my code at the moment. Every case now have a Input option. It is a very basic code. It is input and output.

The code: OutputCurrency.Text = OutputCurrency.Text & InputCurrency & " " & Listbox1.SelectedItem & vbCrLf will update the TextBox after every input, but the not in the same order as the Listbox. .

Is the update of the OutputCurrency.text in the same order as the ListBox also possible without iList or is that impossible?

Sorry if my questions are not clear enough.

Thanks in advance.

Jonathan.


Saturday, November 30, 2019 9:09 AM

Hi Jonathan,

Thank you for your marking my post as an answer.
It made me happy, but I am still not sure if the post satisfies your needs. If not, please explain what you want to do with your program. A screenshot of Windows Form would be helpful for me to understand your requirements.

Regards,  

Ashidacchi -- http://hokusosha.com


Saturday, November 30, 2019 9:17 AM

I have uploaded my Form, InputBox and OutputBox on this site:

https://imgur.com/a/p3jEP0K

Your code is working, but I don't want to use a button. 


Saturday, November 30, 2019 10:29 AM

Hi Jonathan,

Maybe I could understand your needs.
I will try it tomorrow (it's dinner time in Japan).

Regards,

Ashidacchi -- http://hokusosha.com


Sunday, December 1, 2019 12:43 PM

Thank you very much, the code is working.