winforms 存储来自文本框www.example.com的用户输入vb.net

fkaflof6  于 2023-06-24  发布在  .NET
关注(0)|答案(1)|浏览(96)

使用你给我的getters和setters解决方案,我得到一个错误“Purchase1在它被赋值之前被使用”,当我调用这个类以便我可以将数据添加到示例方法中时,这是我写的代码

Private Sub btnRecord_Click(sender As Object, e As EventArgs) Handles btnRecord.Click

    Dim purchase1 As Purchase

    purchase1.description = txtitem.Text
    purchase1.price = txtprice.Text
    purchase1.quantity = txtquantity.Text

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim purchase1 As Purchase

    txtitem.Text = purchase1.description()
    txtprice.Text = purchase1.price()
    txtquantity.Text = purchase1.quantity()
End Sub
ars1skjm

ars1skjm1#

看起来像是试图创建一个对象,作为Form1上的文本框的中介。如果是这种情况,你应该在创建Purchase对象时传递一个对Form1的引用:

Public Class Purchase

    Private _form As Form1

    Public Sub New(form As Form1)
        _form = form
    End Sub

    Public Property description() As String
        Get
            Return _form.txtitem.Text
        End Get
        Set(value As String)
            _form.txtitem.Text = value
        End Set
    End Property
    Public Property quantity() As Integer
        Get
            Return Ctype(_form.txtquantity.Text, Integer)
        End Get
        Set(value As Integer)
            _form.txtquantity.Text = value.ToString
        End Set
    End Property
    Public Property price() As Double
        Get
            Return CType(_form.txtprice.Text, Double)
        End Get
        Set(value As Double)
            _form.txtprice.Text = value.ToString
        End Set
    End Property

End Class

更好的方法是在Purchase类上添加事件,并在事件引发时更新Form:

Public Class Purchase

    Private _description As String
    Private _quantity As Integer
    Private _price As Double

    Public Event HasBeenUpdated(sender As Object, e As EventArgs)

    Public Property description() As String
        Get
            Return _description
        End Get
        Set(value As String)
            If _description <> value Then
                _description = value
                RaiseEvent HasBeenUpdated(Me, EventArgs.Empty)
            End If
        End Set
    End Property
    Public Property quantity() As Integer
        Get
            Return _quantity
        End Get
        Set(value As Integer)
            If _quantity <> value Then
                _quantity = value
                RaiseEvent HasBeenUpdated(Me, EventArgs.Empty)
            End If
        End Set
    End Property
    Public Property price() As Double
        Get
            Return _price
        End Get
        Set(value As Double)
            If _price <> value Then
                _price = value
                RaiseEvent HasBeenUpdated(Me, EventArgs.Empty)
            End If
        End Set
    End Property

End Class

Form1中,您可以创建Purchase的示例并处理其HasBeenUpdated事件:

Private WithEvents _purchase As New Purchase

Private Sub _purchase_HasBeenUpdated(sender As Object, e As EventArgs) Handles _purchase.HasBeenUpdated
    txtDescription.Text = _purchase.description
    txtQuantity.Text = _purchase.quantity.ToString
    txtPrice.Text = _purchase.price.ToString
End Sub

当数据输入完成时,您还需要更新Purchase对象,可能需要使用保存按钮:

Private Sub SavePurchaseData()
    With _purchase
        .description = txtDescription.Text
        .quantity = CType(txtQuantity.Text, Integer)
        .price = CType(txtPrice.Text, Double)
    End With
End Sub

相关问题