Results 1 to 2 of 2

Thread: [RESOLVED] can't modify user control passed as parameter

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    Resolved [RESOLVED] can't modify user control passed as parameter

    In a VB.Net 2017 Windows Forms project, I've defined a user control (named "ExDatePicker") that includes a label, a textbox (named "txt") and a datetimepicker. In several places, I'm passing specific instances of that user control to the Sub shown below. This seems to work as intended for most of the instances, but for one of them it fails to change txt.BackColor. When I step through the code, it APPEARS to change the .BackColor value but that change is never reflected on the screen. At first, the user control parameter ("exdtp") was defined as ByVal and I thought that was the problem. But even after changing it to ByRef, the problem persists. Any ideas about what might be causing this?

    Code:
        Public Sub CheckDateTimePicker(ByRef exdtp As ExDatePicker, ByRef Message As String, Optional IsRequired As Boolean = False,
          Optional MinValue As Date = MinDateValue, Optional MaxValue As Date = MaxDateValue)
            Dim OldMessage As String, Temp As String, Dt As Date
            OldMessage = Message
            Temp = exdtp.txt.Text
            If Date.TryParse(Temp, Dt) Then
                If (Dt < MinValue) Or (Dt > MaxValue) Then
                    AddText(Message, exdtp.DisplayName & " must be between " & FormatDate(MinValue.ToString) & " and " & FormatDate(MaxValue.ToString) & ".")
                End If
            Else
                If IsRequired Then
                    AddText(Message, exdtp.DisplayName & " is required.")
                End If
            End If
            'set .BackColor to reflect whether any edit checks failed
            exdtp.txt.BackColor = If(Message = OldMessage, NormalBackColor, WarningBackColor)
        End Sub

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    Re: can't modify user control passed as parameter

    Nevermind, it turned out this Sub is working correctly, and txt.BackColor was being reset by some other subsequently executed code. And it even works correctly when exdtp is passed ByVal (I'm guessing because when you pass a reference type ByVal, its members may still be changed by the called code even though the reference itself may not be).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width