简体   繁体   English

在VB.Net的文本框中输入数字时,如何更改表单中my.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?! 我的问题是,如果您在文本框中输入数字/小数并单击一个按钮,然后更新要更改的设置,然后在另一个链接的链接中进行更改,是否可以更改表单中my.Settings中的值到my.Settings中的变量?

Form 1: 表格1:

Public Class frmConverter 公共类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 表格2:公共类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 结束班

It sounds like you are trying to use My.Settings as some sort of set of global reference variables. 听起来您正在尝试将My.Settings用作某种全局引用变量集。 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. 首先,打开Option Strict,因为它看起来像是Off。 This will store the decimal value of a textbox to a Settings variable which is defined as a Decimal: 这会将文本框的十进制值存储到设置为Decimal的Settings变量中:

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 . 这在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. 当您向“设置”中发布新值时, txtRate不会自动更新。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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