Skip to content

Commit

Permalink
DateTimePicker has incorrect AccessKey and KeyboardShortcut accessibi…
Browse files Browse the repository at this point in the history
…lity properties when Text contains '&' character (#9414)

* fix DateTimePicker has incorrect AccessKey and KeyboardShortcut accessibility properties when Text contains '&' character #9281

* add comment, remove redundant code

* fix wrong case - Win32
  • Loading branch information
Epica3055 authored Jul 5, 2023
1 parent 62f7d01 commit f7dd99e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,8 @@ public override string? KeyboardShortcut
}
}

string? baseShortcut = base.KeyboardShortcut;

if (baseShortcut is null || baseShortcut.Length == 0)
{
char ownerTextMnemonic = WindowsFormsUtils.GetMnemonic(this.GetOwnerText(), convertToUpperCase: false);
if (ownerTextMnemonic != '\0')
{
return $"Alt+{ownerTextMnemonic}";
}
}

return baseShortcut;
// Win32 DTP does not interpret ampersand in its Text as an escape character for a mnemonic.
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,35 @@ public void DateTimePickerAccessibleObject_DoDefaultAction_IfHandleIsCreated_Ret
Assert.Equal((UiaCore.ExpandCollapseState)expected, accessibleObject.ExpandCollapseState);
Assert.True(dateTimePicker.IsHandleCreated);
}

// Unit Test for https://github.com/dotnet/winforms/issues/9281.
[WinFormsFact]
public void DateTimePickerAccessibleObject_KeyboardShortcut_ReturnsExpected()
{
using Form form = new Form();
using DateTimePicker dateTimePicker1 = new();
using Label label1 = new ();
using DateTimePicker dateTimePicker2 = new();

dateTimePicker1.CustomFormat = "'Date&Time' hh:mm dd/MM";
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.TabIndex = 0;

label1.Text = "&Date";
label1.TabIndex = 1;

dateTimePicker2.TabIndex = 2;

form.Controls.Add(dateTimePicker2);
form.Controls.Add(label1);
form.Controls.Add(dateTimePicker1);

string keyboardShortcut = dateTimePicker1.AccessibilityObject.KeyboardShortcut;

Assert.Null(keyboardShortcut);

keyboardShortcut = dateTimePicker2.AccessibilityObject.KeyboardShortcut;

Assert.Equal("Alt+d", keyboardShortcut);
}
}

0 comments on commit f7dd99e

Please sign in to comment.