Search code examples
vb.netmy.settings

How do you change the value in my.settings in a form when you enter numbers in a textbox in VB.Net


I was wondering if you could help me? My question is that, is there a way of changing the value in my.Settings in a form if you enter a number/decimal in a textbox and click a button and then update in the settings to be then changed in another from which is linked to my.Settings in a variable?!

Form 1:

Public Class frmConverter

Dim input As String
Dim result As Decimal

Dim EUR_Rate As Decimal = My.Settings.EUR_Rates
Dim USD_Rate As Decimal = 1.6
Dim JYP_Rate As Decimal = 179.65

Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

    input = txtInput.Text

    Try

        If ComboBox1.Text = "£" Then
            Pounds()

        ElseIf ComboBox1.Text = "€" Then
            Euros()

        ElseIf ComboBox1.Text = "$" Then
            Dollars()

        ElseIf ComboBox1.Text = "¥" Then
            Yen()

        End If

    Catch es As Exception
        MsgBox("Error!")
    End Try

End Sub

Private Sub btnSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSettings.Click

    Me.Hide()
    frmExchange.Show()

End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click

    txtInput.Text = ""
    lblResult.Text = ""

End Sub

Function Pounds()

    If ComboBox1.Text = "£" And ComboBox2.Text = "€" Then
        result = (input * EUR_Rate)
        lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text

    ElseIf ComboBox1.Text = "£" And ComboBox2.Text = "$" Then
        result = (input * USD_Rate)
        lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text

    ElseIf ComboBox1.Text = "£" And ComboBox2.Text = "¥" Then
        result = (input * JYP_Rate)
        lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text

    End If

    Return 0
End Function

Form 2: Public Class frmExchange

Private Sub frmExchange_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    My.Settings.EUR_Rates = (txtinput.Text)

    My.Settings.Save()
    My.Settings.Reload()

End Sub

Public Sub SetNewRate(ByVal rate As Decimal)

    txtinput.Text = rate.ToString

End Sub

Private Sub btnchange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnchange.Click

    If ComboBox1.Text = "€" Then

        My.Settings.USD_Rates = (txtinput.Text)
        frmConverter.SetNewRate(txtinput.Text)

    End If

End Sub

End class


Solution

  • It sounds like you are trying to use My.Settings as some sort of set of global reference variables. Thats not what they are for, not how they work and not what they are.

    First, turn on Option Strict as it looks like it may be Off. This will store the decimal value of a textbox to a Settings variable which is defined as a Decimal:

    My.Settings.USD_Rates = CDec(SomeTextBox.Text)
    

    Thats all it will do. It wont save the value and it wont pass it around or share it with other forms and variables.

    My.Settings.Save            'saves current settings to disk for next time
    My.Settings.Reload          'Load settings from last time
    

    This is all covered on MSDN. There is no linkage anywhere. If you have code in another form like this:

    txtRate.Text = My.Settings.USD_Rates.ToString
    

    txtRate will not automatically update when you post a new value to Settings. There are just values not Objects (see Value Types and Reference Types). To pass the new value to another form:

    ' in other form:
    Public Sub SetNewRate(rate As Decimal)
        ' use the new value:
        soemTextBox.Text = rate.ToString
    End Sub 
    

    in form which gets the change:

    Private Sub btnchangeRate(....
        ' save to settings which has nothing to do with passing the data  
        My.Settings.USD_Rates = CDec(RateTextBox.Text)
        otherForm.SetNewRate(CDec(RateTextBox.Text))
    End Sub
    

    You may run into problems if you are using the default form instance, but that is a different problem.


    You botched the instructions. The 2 procedures are supposed to go in 2 different forms - one to SEND the new value, one to RECEIVE the new value. With the edit and more complete picture, there is an easier way.

    Private Sub btnSettings_Click(...) Handles btnSettings.Click
        ' rather than EMULATE a dialog, lets DO a dialog:
        'Me.Hide()
        'frmExchange.Show()
    
        Using frm As New frmExchange         ' the proper way to use a form
            frm.ShowDialog
    
            ' Im guessing 'result' is the xchg rate var
            result = CDec(frm.txtInput.Text)
        End Using          ' dispose of form, release resources
    
    End Sub
    

    In the other form

    Private Sub btnchange_Click(....)
        ' do the normal stuff with Settings, if needed then:
        Me.Hide
    End Sub