我正在尝试将数据输入3个单独的文本框中,通过我的日期信息类和重新格式化的打印传递该数据。我似乎无法弄清楚如何引用输入文本框的数据。我以为我写得正确,但所有打印出的都是0/0/0。
以下是我的form1
中的代码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
答案 0 :(得分:0)
您的代码正在调用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
这里我已经对你的类外部的日,月和年进行了检查,因为你需要在调用类构造函数之前检查用户输入的文本(字符串)是否真的是一个整数。您可以为有效的月份和日期添加其他检查。