populate TextBoxes base on date value selected in DatePicker

zleug 51 Reputation points
2024-10-07T22:53:26.74+00:00

Hi All. In form of WPF VB.Net project I would like to populate 3 TextBoxes base on selected in DatePicker date value of Monday. The first TextBox named txbWeek will display the number of week of the month depending on the selected date in the DatePicker; the second TextBox named txbMonDate will display Monday date depending on the selected date in the DatePicker; the third TextBox named txbFriDate will display Friday date depending on the selected date in the DatePicker. If that is possible, How it to do? I'll appreciate for detail explanation.

Thanks

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,733 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 2,305 Reputation points Microsoft Vendor
    2024-10-08T07:47:43.0866667+00:00

    Hi,@zleug. Welcome to Microsoft Q&A. 

    You could refer to the following code to achieve the function you want.

     

    MainWindow.xaml

    
    <Grid>
    
        <Grid.RowDefinitions>
    
            <RowDefinition/>
    
            <RowDefinition/>
    
        </Grid.RowDefinitions>
    
        <DatePicker x:Name="MyDatePicker" SelectedDateChanged="MyDatePicker_SelectedDateChanged"></DatePicker>
    
        <StackPanel Orientation="Horizontal" Grid.Row="1">
    
        <TextBox x:Name="txbWeek"   Margin="10" Width="100" Height="50"></TextBox>
    
            <TextBox x:Name="txbMonDate"  Margin="10" Width="100" Height="50"></TextBox>
    
            <TextBox x:Name="txbFriDate"  Margin="10" Width="100" Height="50"></TextBox>
    
        </StackPanel>
    
    </Grid>
    
    

     

    MainWindow.xaml.vb

    
    Class MainWindow
    
        Private Sub MyDatePicker_SelectedDateChanged(sender As Object, e As SelectionChangedEventArgs)
    
            Dim value As String = MyDatePicker.Text
    
     
    
            If Not String.IsNullOrEmpty(value.ToString()) Then
    
                'The time value obtained by DatePicker Is converted from string type to DateTime
    
                Dim mydate = value.ToString().Split("/")
    
                Dim dateValue As New DateTime(mydate(2), mydate(0), mydate(1))
    
                'Get Week
    
                txbWeek.Text = GetWeekOfMonth(dateValue)
    
     
    
                'Get the week number Of the current day
    
                Dim Week = dateValue.DayOfWeek
    
                'Sunday 's enumeration is 0, set it to 7 to facilitate subsequent calculation of time difference
    
                If Week = 0 Then
    
                    Week = 7
    
                End If
    
                'Get Monday
    
                Dim addDate = DayOfWeek.Monday - Week
    
                txbMonDate.Text = dateValue.AddDays(addDate)
    
                'Get Friday
    
                addDate = DayOfWeek.Friday - Week
    
                txbFriDate.Text = dateValue.AddDays(addDate)
    
            Else
    
                txbWeek.Text = ""
    
                txbMonDate.Text = ""
    
                txbFriDate.Text = ""
    
            End If
    
        End Sub
    
     
    
     
    
        Function GetWeekOfMonth(dateValue As DateTime) As Integer
    
            ' Get the first day of the month
    
            Dim firstDayOfMonth As New DateTime(dateValue.Year, dateValue.Month, 1)
    
     
    
            'Get the week number for this date and the first day of the month
    
            Dim firstWeekOfYear As Integer = DatePart(DateInterval.WeekOfYear, firstDayOfMonth, FirstDayOfWeek.System)
    
            Dim currentWeekOfYear As Integer = DatePart(DateInterval.WeekOfYear, dateValue, FirstDayOfWeek.System)
    
     
    
            ' Calculate the week number of the month that the date falls in
    
            Return currentWeekOfYear - firstWeekOfYear + 1
    
        End Function
    
    End Class
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.