Visual studio 2010 如何在单独的类中使用文本框中输入的数据

Visual studio 2010 如何在单独的类中使用文本框中输入的数据,visual-studio-2010,Visual Studio 2010,我试图获取在3个单独的文本框中输入的数据,通过dateinformation类传递这些数据,然后打印出来重新格式化。我似乎不知道如何引用输入到文本框中的数据。我以为我写得对,但打印出来的都是0/0/0 这是我表格1中的代码 Public Class Form1 Private Sub btnFormat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFormat.Click Di

我试图获取在3个单独的文本框中输入的数据,通过dateinformation类传递这些数据,然后打印出来重新格式化。我似乎不知道如何引用输入到文本框中的数据。我以为我写得对,但打印出来的都是0/0/0

这是我表格1中的代码

Public Class Form1

Private Sub btnFormat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFormat.Click
    Dim enteredDate As New DateInformation
    txtBoxResult.Text = ""
    txtBoxResult.AppendText(enteredDate.ToString)
End Sub
这是DateInformation类

Public Class DateInformation

Private month As Integer
Private day As Integer
Private year As Integer
Private frmone As New Form1

Public Sub New(ByVal initialmonth As Integer, ByVal initialday As Integer, ByVal initialyear As Integer)
    month = initialmonth
    day = initialday
    year = initialyear
End Sub

Sub New()
    ' TODO: Complete member initialization 
End Sub

Public Property sourceForm() As Form1
    Get
        Return frmone
    End Get
    Set(ByVal Value As Form1)
        frmone = Value
    End Set
End Property

Public Property newmonth() As Integer
    Get
        Return month
    End Get
    Set(ByVal value As Integer)
        Try
            value = Convert.ToInt32(frmone.txtBoxMonth.Text)
        Catch ex As Exception
            MessageBox.Show("Value entered must be an integer")
        End Try
        value = month
    End Set
End Property


Public Overrides Function ToString() As String
    Return month & "/" & day & "/" & year
End Function

您的代码正在调用DateInformation的无参数构造函数(一个没有参数且已用TODO注释标记的构造函数)。在此上下文中,内部变量month、day和year被设置为其初始默认值,即整数为0。当然,ToString覆盖打印0/0/0

要解决您的问题,您需要调用带有3个参数的DateInformation构造函数

因此,假设您有3个名为txtMonth、txtDay和txtYear的文本框,正确的代码是

Private Sub btnFormat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFormat.Click
    Dim year as Integer
    Dim month as Integer
    Dim day as Integer

    if Not int32.TryParse(txtYear, year) Then
       MessageBox.Show("Enter a number for year")
       return
    End If 
    if Not int32.TryParse(txtMonth, month) Then
       MessageBox.Show("Enter a number for month")
       return
    End If 
    ' other checks for a valid month to follow here...

    if Not int32.TryParse(txtYear, day) Then
       MessageBox.Show("Enter a number for days")
       return
    End If 
    ' other checks for a valid days to follow here...

    Dim enteredDate As New DateInformation(month, day, year)
    txtBoxResult.Text = ""
    txtBoxResult.AppendText(enteredDate.ToString)
End Sub

在这里,我对类外部的天、月和年进行了检查,因为在调用类构造函数之前,需要检查用户输入的文本(字符串)是否真的是整数。您可以为有效月份和有效天数添加其他检查。

谢谢您的回答,这确实解决了问题,但这似乎使dateinformation类几乎毫无用处。如何修复构造函数?我有随机的“todo”注释,只是为了消除错误。只需删除空构造函数。您不需要它,因为如果没有值,这个类就没有意义。我不知道这是否是你为这个类编写的所有代码。实际上,这里没有什么价值,因为标准DateTime类内置了许多功能,如果您想将日期打印为格式化字符串,您可以通过使用标准或自定义模式来实现