Skip to content

Commit

Permalink
fix DateTimePicker has incorrect AccessKey and KeyboardShortcut acces…
Browse files Browse the repository at this point in the history
…sibility properties when Text contains '&' character dotnet#9281
  • Loading branch information
Epica3055 committed Jul 3, 2023
1 parent 70f4445 commit 04c5e1c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,7 @@ 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;
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,39 @@ 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.Name = "dateTimePicker1";
dateTimePicker1.TabIndex = 0;

label1.AutoSize = true;
label1.Name = "label1";
label1.TabIndex = 1;
label1.Text = "&Date";

dateTimePicker2.Name = "dateTimePicker2";
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 04c5e1c

Please sign in to comment.