Krypton-Suite / Standard-Toolkit

An update to Component factory's krypton toolkit to support .NET Framework 4.6.2 - 4.8.1 to .NET 8 - 9
BSD 3-Clause "New" or "Revised" License
408 stars 62 forks source link

[Bug]: KryptonTextBox `Validate` / `Validating` / `KeyUp` events are invoked twice #666

Closed overlinejota closed 2 years ago

overlinejota commented 2 years ago

A simple form with a KrytonTextBox and a button. The next code wave twice when KryptoTextbox1 loses focus:

namespace AppForm{
    public partial class Form1 : Krypton.Toolkit.KryptonForm{
        public Form1() {
            InitializeComponent();
        }            
        private void kryptonTextBox1_Validated(object sender, EventArgs e) {
            MessageBox.Show("Error","Hello");
        }
    }
}

private void InitializeComponent() {
    ...
    this.kryptonTextBox1.Validated += new System.EventHandler(this.kryptonTextBox1_Validated);
    ...
}

Thanks;

giduac commented 2 years ago

KMaskedTextbox is also affected. Also happens with the "Validating" event.

When set to false non of the events is triggered. KryptonMaskedTextBox.CausesValidation = false; KryptonTextBox.CausesValidation = false;

This link could be helpful https://stackoverflow.com/questions/30759864/winforms-firing-enter-event-twice

PWagner1 commented 2 years ago

@Smurf-IV Is this linked to the fixes that were merged yesterday?

Smurf-IV commented 2 years ago

@Smurf-IV Is this linked to the fixes that were merged yesterday?

Do not think so, Did not look at any impact in this area as I was in the Dock and ribbon projects

PWagner1 commented 2 years ago

@Smurf-IV Is this linked to the fixes that were merged yesterday?

Do not think so, Did not look at any impact in this area as I was in the Dock and ribbon projects

Is this just missing the 'Focus()' call in the validate event?

thiagoraheem commented 2 years ago

I have the same situation and I created a project to demonstrate

https://github.com/thiagoraheem/KryptonTest

Smurf-IV commented 2 years ago

This also affects other controls like KDomainUD

It is because

This could take a while to fix, e.g. just "some" of the eents in the KDominUD:

        private void OnDomainUpDownTextChanged(object sender, EventArgs e) => OnTextChanged(e);

        private void OnDomainUpDownScroll(object sender, ScrollEventArgs e) => OnScroll(e);

        private void OnDomainUpDownSelectedItemChanged(object sender, EventArgs e) => OnSelectedItemChanged(e);

        private void OnDomainUpDownGotFocus(object sender, EventArgs e)
        {
            UpdateStateAndPalettes();
            PerformNeedPaint(true);
            InvalidateChildren();
            base.OnGotFocus(e);
        }

        private void OnDomainUpDownLostFocus(object sender, EventArgs e)
        {
            UpdateStateAndPalettes();
            PerformNeedPaint(true);
            InvalidateChildren();
            // ReSharper disable RedundantBaseQualifier
            base.OnLostFocus(e);
            // ReSharper restore RedundantBaseQualifier
        }

        private void OnDomainUpDownKeyPress(object sender, KeyPressEventArgs e) => OnKeyPress(e);

        private void OnDomainUpDownKeyUp(object sender, KeyEventArgs e) => OnKeyUp(e);

        private void OnDomainUpDownKeyDown(object sender, KeyEventArgs e) => OnKeyDown(e);

        private void OnDomainUpDownPreviewKeyDown(object sender, PreviewKeyDownEventArgs e) => OnPreviewKeyDown(e);

        private void OnDomainUpDownValidated(object sender, EventArgs e) => OnValidated(e);

        private void OnDomainUpDownValidating(object sender, CancelEventArgs e) => OnValidating(e);

Q: Should the OnDomainUpDownGotFocus be rewritten to not call the base class, because it will end up calling it twice ? A: Maybe; who knows for each Krypton control !!

PWagner1 commented 2 years ago

I was going round in circles for months trying to track down this bug!

@Smurf-IV What happens if you "disable" Krypton's implementation & use the standard WinForms validation calls?

Smurf-IV commented 2 years ago

@Smurf-IV What happens if you "disable" Krypton's implementation & use the standard WinForms validation calls?

It will work as expected.. It's just all the the other event handlers that will need consideration ! Trouble is when Krypton needs to do something with the event and then calls the base anyway..

Smurf-IV commented 2 years ago

Update: Darn it.. Keep going round in circles when trying to apply to all Krypton controls.. This bug is entrenched too deeply in some places, and I haven't even moved into Ribbons yet !

Smurf-IV commented 2 years ago

I'm going to focus on just this control, so that it can be validated by @overlinejota as soon as a fix is published. Otherwise there would be too many controls updated tat would remain untested fro a long time. @Wagnerp will need something on the landing page asking for instances of "Double events" to be raised ASAP so that they can be worked on in turn with active users ready to "Test" the result.

image

PWagner1 commented 2 years ago

@Smurf-IV Ok, will broadcast a message out on Discord too

Smurf-IV commented 2 years ago

FYI: Reason for separate controls updates; image

In the above by just "Removing things that just call through to the base" shows the the click events are no longer working in the KryptonTextBox

Smurf-IV commented 2 years ago

Done, with a few other events as well: TBClickValidate

PR coming soon