DateTimePicker:这个框会回到原来的日期吗?

时间:2015-09-30 22:51:15

标签: vb.net basic

嘿伙计们,所以我正在为vb上的生肖发现者做一个程序,该程序可以工作,除了我的问题是每次我运行它时,DateTimePickerBox回到当前日期而不是我输入的日期。

这是我未完成的代码;

Public Class HoroscopeSign

Private Sub HoroscopeSign_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'DateTimePicker.Format = DateTimePickerFormat.Custom
    'DateTimePicker.CustomFormat = "dd MMMM yyyy"
    txtDOB.Format = DateTimePickerFormat.Short

End Sub

Private Sub btnEnter_Click(sender As System.Object, e As System.EventArgs) Handles btnEnter.Click
    Dim DOB As Date = txtDOB.Value
    txtDOB.Text = Process(DOB)
End Sub
Public Function Process(dateOfBirth As Date) As String
    Dim sign As String
    Dim info As String
    Dim BirthYear As Integer = dateOfBirth.Year
    Dim BirthMonth As Integer = dateOfBirth.Month
    Dim BirthDay As Integer = dateOfBirth.Day


    If txtDOB.Value >= New Date(BirthYear, 1, 20) And txtDOB.Value <= New Date(BirthYear, 2, 18) Then
        PBHoroscope.Load("E:\Aquarius.jpg")
        sign = "Aquarius"
        info = ""
    Else txtDOB.Value >= New Date(BirthYear, 2, 19) And txtDOB.Value <= New Date(BirthYear, 3, 20) Then
        PBHoroscope.Load("E:\Pisces.jpg")
        sign = "Pisces"


    lblSign.Text = sign
    lblDescription.Text = info

End Function

Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
    Dim a As Integer
    a = MsgBox("Do you want to exit this application", vbYesNo, "Exit Application")
    If a = vbYes Then
        MsgBox("Thank you for using this application", vbOKOnly, "Exit Application")
        End
    Else
        Me.Show()
    End If
End Sub

Private Sub txtDOB_ValueChanged(sender As System.Object,e As System.EventArgs)处理txtDOB.ValueChanged         Dim DOB As Date = txtDOB.Value         DOB = txtDOB.Value 结束子 结束班

1 个答案:

答案 0 :(得分:0)

所以你已声明你的函数Process返回一个字符串,但你不能从该函数返回任何内容。当您尝试将该函数的返回值分配给txtDOB.Text属性时,就会出现问题。您不会返回任何内容,因此txtDOB会将自身重置为当前日期

您应该决定要返回的字符串(以日期格式)或更好地更改Process函数以返回DateTime并将该值分配给txtDOB.Value属性。或者只是将这些行更改为

Private Sub btnEnter_Click(sender As System.Object, e As System.EventArgs) Handles btnEnter.Click
    ' REMOVE THIS LINE Dim DOB As Date = txtDOB.Value
    ' REMOVE THIS LINE txtDOB.Text = Process(DOB)
     Process(txtDOB.Value)
End Sub