Skip to content

IsDefault on button makes it click itself on close #3913

@tznind

Description

@tznind

Describe the bug
When a View has 2 Button (A and B). Both have Accepting handler. One of which IsDefault then the default one is always fired even when clicking on the other button.

To Reproduce

Application.Init ();

Window w = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };

var btn1 = new Button ()
{
    Text = "A",
    IsDefault = true,
};
btn1.Accepting += (s, e) => {
                      Debug.WriteLine ("A pressed");
                      Application.RequestStop ();
                  };

var btn2 = new Button ()
{
    X = Pos.Right (btn1),
    Text = "B"
};
btn2.Accepting += (s, e) =>
                  {
                      Debug.WriteLine ("B pressed");
                      Application.RequestStop ();
                  };

w.Add (btn1);
w.Add (btn2);

Application.Run (w);
w.Dispose ();
Application.Shutdown ();

return 0;

When you click A you see

"Pressed A"

When you click B you see

"Pressed B"
"Pressed A"

Image

Expected behavior
Button should not click itself!

** Workaround **
As a workaround you can have all the buttons that are not IsDefault call e.Cancel on the accepting event.

btn2.Accepting += (s, e) =>
                  {
                      Debug.WriteLine ("B pressed");
+                    e.Cancel = true;
                      Application.RequestStop ();
                  };

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions