Change the value during the TextChanged event

Posted by: nguyenhuyhoangbkhn on 9 September 2024, 8:08 pm EST

    • Post Options:
    • Link

    Posted 9 September 2024, 8:08 pm EST - Updated 9 September 2024, 8:36 pm EST

    Hello everybody!

    In old version, the value will be updated immediately when the text changes. In C1DateEdit, this change will be made when it loses focus.

    In TextChanged event, i want to get Value, but Value don’t change

    I want to know if C1DateEdit has any support regarding this issue.

  • Posted 10 September 2024, 4:53 am EST

    Hi,

    you probably use the .NET6 version, and you miss the “ValueChangedBehaviorEnum.FireOnAnyChange” enum value?

    I ran into the same problem (https://developer.mescius.com/forums/winforms-edition/c1dateedit-migration-to-net6), and C1 support suggested to add a timer that is started in the “TextChanged” event. When the timer fires, it parses the current C1DateEdit text and applies it to the “Value” - see code in “timerC1DateEdit_Elapsed” in the sample.

    Doing this every time the text changes might also be possible, but this raises too many “ValueChanged” events.

    Here is an updated sample, based on the old post:

    C1DateEdit_WithTimer.zip

    Of course, this only works if you subclass C1DateEdit and use the subclass on your forms.

    Hope this helps

    Wolfgang

  • Posted 10 September 2024, 9:25 am EST

    Hello,

    We tried to test the behavior with the latest version and it seems to work fine. Please refer to the attached sample, Input_DateEdit.zip.

    In the sample, we used the Test property to get the updated value. If you want to use the Value property instead, then you’ll have to validate the text first using the C1DateEdit.ValidateText() method to update the value with current text.

    You can also refer to Wolfgang’s comment if your issue aligns with theirs.

    Please share more information on the issue you are facing by updating the attached sample or attaching the stripped-down version of your project so that we can observe the behavior and assist you accordingly.

    Also, please test your sample with the latest version of C1 controls to see if the behavior persists and let us know the version you are using.

    Regards,

    Uttkarsh.

    P.S.: Thank you, Wolfgang, for your valuable input.

  • Posted 16 September 2024, 10:58 pm EST

    To wknauf and uttkarsh.matiyal

    Thanks for your reply. I also tried it, and in the sample project, there was no problem.

    But I have a concern about whether calling too many ValueChanged events will affect the project. Can you give me some advice

    my problem

    I have two C1DateEdit controls: startDate and endDate.

    When I edit the startDate, I want the endDate value to be automatically updated(endDate is always 1 month after the startDate).

    In the current version all processing is done in TextChanged

    Public Class DateEditEx
        Inherits C1.Win.Calendar.C1DateEdit
    
        Private _oldValue As Object = Nothing
        Protected Overrides Sub OnValueChanged(e As EventArgs)
            If _oldValue <> Value Then
                MyBase.OnValueChanged(e)
            End If
        End Sub
        Protected Overrides Sub OnTextChanged(e As EventArgs)
            Try
                Select Case CustomFormat
                    '日本語の字のケース
                    Case "yyyy/M/d(ddd)"
                    Case "yyyy/MM/dd(ddd)"
                        Dim cleanedString As String = Text.Substring(0, Text.IndexOf("("c))
                        Value = Date.ParseExact(cleanedString, "yyyy/MM/dd", Globalization.CultureInfo.InvariantCulture)
                        'Case "yyyy年"
                        '    Value = Date.ParseExact(Text, "yyyy年", Globalization.CultureInfo.InvariantCulture)
                        'Case "yyyy年M月"
                        '    Value = Date.ParseExact(Text, "yyyy年M月", Globalization.CultureInfo.InvariantCulture)
                        'Case "yyyy年MM月"
                        '    Value = Date.ParseExact(Text, "yyyy年MM月", Globalization.CultureInfo.InvariantCulture)
                    Case Else
                        Me.Value = Date.ParseExact(Text, Me.CustomFormat, Globalization.CultureInfo.InvariantCulture)
                End Select
                _oldValue = Value
            Catch ex As Exception
            Finally
                MyBase.OnTextChanged(e)
            End Try
        End Sub
    End Class
  • Posted 17 September 2024, 5:54 am EST

    Hi,

    I am not sure whether your “ParseExact” code works if leading zeros in e.g. month or day numbers are omitted. But you could rely on “C1DateEdit.ParseContent” to handle this (hope the C# code is OK for you):

    if (this.ParseContent(out object objParsed, ErrorInfo) == ErrorReason.None)
    {
       DateTime datParsed = (DateTime)objParsed;
       ...
    }
    

    Also, setting the value in “TextChanged” might break the current caret position, so that the user cannot continue typing. Did you check my sample? There is code in the timer tick handler that restored the edit position after setting the “Value”.

    Best regards

    Wolfgang

  • Posted 17 September 2024, 6:20 am EST

    Hello,

    The TextChanged event is fired whenever the text is changed and before the value is validated/parsed. As per your use case, we think using the ValueChanged event would be better as there’ll be no need to manually parse the text string. The ValueChanged event is fired only when the text is changed and the value is parsed successfully.

    Please refer to the attached working sample and let us know if it fulfills your requirement. (see Input_DateEditSample.zip)

    We checked your code; the TextChanged and ValueChanged events are fired only once whenever the value is changed. So, we do not think it should cause any issues. However, if you observe any discrepancies, please let us know too.

    Regards,

    Uttkarsh.

    P.S.: Thank you, Wolfgang, for your valuable input.

  • Posted 18 September 2024, 12:01 am EST

    Thank you for helping me.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels