Skip to content

Commit 330a7c6

Browse files
authored
fix #13071 Changing properties with RefreshProperties.All does not requery the property list in the PropertyGrid in .net 9 (worked in .net 8) (#13295)
* fix #13071 Changing properties with RefreshProperties.All does not requery the property list in the PropertyGrid in .net 9 (worked in .net 8) : revert PR#12431 , use another approach. * add unit test
1 parent 1364df6 commit 330a7c6

File tree

4 files changed

+106
-30
lines changed

4 files changed

+106
-30
lines changed

src/System.Windows.Forms/System/Windows/Forms/Controls/PropertyGrid/PropertyGridInternal/GridEntry.GridEntryAccessibleObject.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,10 @@ internal override void SetFocus()
410410

411411
base.SetFocus();
412412

413-
RaiseAutomationEvent(UIA_EVENT_ID.UIA_AutomationFocusChangedEventId);
413+
if (!PropertyGridView.InPropertySet && !PropertyGridView.EditMouseDown)
414+
{
415+
RaiseAutomationEvent(UIA_EVENT_ID.UIA_AutomationFocusChangedEventId);
416+
}
414417
}
415418
}
416419
}

src/System.Windows.Forms/System/Windows/Forms/Controls/PropertyGrid/PropertyGridInternal/PropertyGridView.Flags.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ private enum Flags : ushort
2222
/// </summary>
2323
ButtonLaunchedEditor = 0x0100,
2424
NoDefault = 0x0200,
25-
ResizableDropDown = 0x0400
25+
ResizableDropDown = 0x0400,
26+
EditMouseDown = 0x0800
2627
}
2728
}

src/System.Windows.Forms/System/Windows/Forms/Controls/PropertyGrid/PropertyGridInternal/PropertyGridView.cs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,44 +2566,59 @@ private void OnEditLostFocus(object? sender, EventArgs e)
25662566
InvokeLostFocus(this, EventArgs.Empty);
25672567
}
25682568

2569-
private void OnEditMouseDown(object? sender, MouseEventArgs e)
2569+
internal bool EditMouseDown
25702570
{
2571-
if (!FocusInside)
2572-
{
2573-
SelectGridEntry(_selectedGridEntry, pageIn: false);
2574-
}
2575-
2576-
if (e.Clicks % 2 == 0)
2577-
{
2578-
DoubleClickRow(_selectedRow, toggleExpand: false, RowValue);
2579-
EditTextBox.SelectAll();
2580-
}
2571+
get => _flags.HasFlag(Flags.EditMouseDown);
2572+
private set => SetFlag(Flags.EditMouseDown, value);
2573+
}
25812574

2582-
if (_rowSelectTime == 0)
2575+
private void OnEditMouseDown(object? sender, MouseEventArgs e)
2576+
{
2577+
try
25832578
{
2584-
return;
2585-
}
2586-
2587-
// Check if the click happened within the double click time since the row was selected.
2588-
// This allows the edits to be selected with two clicks instead of 3 (select row, double click).
2589-
long timeStamp = DateTime.Now.Ticks;
2590-
int delta = (int)((timeStamp - _rowSelectTime) / 10000); // make it milliseconds
2579+
EditMouseDown = true;
25912580

2592-
if (delta < SystemInformation.DoubleClickTime)
2593-
{
2594-
Point screenPoint = EditTextBox.PointToScreen(e.Location);
2581+
if (!FocusInside)
2582+
{
2583+
SelectGridEntry(_selectedGridEntry, pageIn: false);
2584+
}
25952585

2596-
if (Math.Abs(screenPoint.X - _rowSelectPos.X) < SystemInformation.DoubleClickSize.Width &&
2597-
Math.Abs(screenPoint.Y - _rowSelectPos.Y) < SystemInformation.DoubleClickSize.Height)
2586+
if (e.Clicks % 2 == 0)
25982587
{
25992588
DoubleClickRow(_selectedRow, toggleExpand: false, RowValue);
2600-
PInvokeCore.SendMessage(EditTextBox, PInvokeCore.WM_LBUTTONUP, (WPARAM)0, (LPARAM)e.Location);
26012589
EditTextBox.SelectAll();
26022590
}
26032591

2604-
_rowSelectPos = Point.Empty;
2592+
if (_rowSelectTime == 0)
2593+
{
2594+
return;
2595+
}
26052596

2606-
_rowSelectTime = 0;
2597+
// Check if the click happened within the double click time since the row was selected.
2598+
// This allows the edits to be selected with two clicks instead of 3 (select row, double click).
2599+
long timeStamp = DateTime.Now.Ticks;
2600+
int delta = (int)((timeStamp - _rowSelectTime) / 10000); // make it milliseconds
2601+
2602+
if (delta < SystemInformation.DoubleClickTime)
2603+
{
2604+
Point screenPoint = EditTextBox.PointToScreen(e.Location);
2605+
2606+
if (Math.Abs(screenPoint.X - _rowSelectPos.X) < SystemInformation.DoubleClickSize.Width &&
2607+
Math.Abs(screenPoint.Y - _rowSelectPos.Y) < SystemInformation.DoubleClickSize.Height)
2608+
{
2609+
DoubleClickRow(_selectedRow, toggleExpand: false, RowValue);
2610+
PInvokeCore.SendMessage(EditTextBox, PInvokeCore.WM_LBUTTONUP, (WPARAM)0, (LPARAM)e.Location);
2611+
EditTextBox.SelectAll();
2612+
}
2613+
2614+
_rowSelectPos = Point.Empty;
2615+
2616+
_rowSelectTime = 0;
2617+
}
2618+
}
2619+
finally
2620+
{
2621+
EditMouseDown = false;
26072622
}
26082623
}
26092624

@@ -3992,7 +4007,7 @@ private void Refresh(bool fullRefresh, int startRow, int endRow)
39924007
startRow = 0;
39934008
}
39944009

3995-
if (OwnerGrid.HavePropertyEntriesChanged())
4010+
if (fullRefresh || OwnerGrid.HavePropertyEntriesChanged())
39964011
{
39974012
if (HasEntries && !InPropertySet && !CommitEditTextBox())
39984013
{

src/test/unit/System.Windows.Forms/System/Windows/Forms/PropertyGridTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4158,6 +4158,63 @@ public void PropertyGrid_SelectedGridItemChanged_TriggeredCorrectly()
41584158
eventArgs.NewSelection.Should().Be(gridItem);
41594159
}
41604160

4161+
// Regression test for https://github.com/dotnet/winforms/issues/13071
4162+
[WinFormsFact]
4163+
public void PropertyGrid_FullRefreshShouldTriggerTypeConverterGetProperties()
4164+
{
4165+
using PropertyGrid propertyGrid = new()
4166+
{
4167+
SelectedObject = new SelectedObject()
4168+
};
4169+
PropertyGridView propertyGridView = propertyGrid.TestAccessor().Dynamic._gridView;
4170+
4171+
MyTypeConverter.GetPropertiesInvokeCount = 0;
4172+
propertyGridView.Refresh(true);
4173+
4174+
int getPropertiesInvokeCount2 = MyTypeConverter.GetPropertiesInvokeCount;
4175+
getPropertiesInvokeCount2.Should().Be(1);
4176+
}
4177+
4178+
#region classes used for PropertyGrid_FullRefreshShouldTriggerTypeConverterGetProperties
4179+
[TypeConverter(typeof(MyTypeConverter))]
4180+
private class SelectedObject
4181+
{
4182+
private string _a;
4183+
private string _b;
4184+
4185+
[RefreshProperties(RefreshProperties.All)]
4186+
public string A
4187+
{
4188+
get { return _a; }
4189+
set { _a = value; }
4190+
}
4191+
4192+
public string B
4193+
{
4194+
get { return _b; }
4195+
set { _b = value; }
4196+
}
4197+
}
4198+
4199+
private class MyTypeConverter : TypeConverter
4200+
{
4201+
public static int GetPropertiesInvokeCount { get; set; }
4202+
public MyTypeConverter()
4203+
: base() { }
4204+
4205+
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
4206+
{
4207+
return true;
4208+
}
4209+
4210+
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
4211+
{
4212+
GetPropertiesInvokeCount++;
4213+
return base.GetProperties(context, value, attributes) ?? TypeDescriptor.GetProperties(value, attributes);
4214+
}
4215+
}
4216+
#endregion
4217+
41614218
private class SubToolStripRenderer : ToolStripRenderer
41624219
{
41634220
}

0 commit comments

Comments
 (0)