Skip to content

Commit df745d6

Browse files
authored
Update OnSelectedItemName_Paint: When selectedItem.Site is null, only show selectedItem type in Items Collection Editor (#13078)
<!-- Please read CONTRIBUTING.md before submitting a pull request --> Fixes #13040 ## Proposed changes - Update method `OnSelectedItemName_Paint`: When `selectedItem.Site` is null, only show selectedItem type in Items Collection Editor <!-- We are in TELL-MODE the following section must be completed --> ## Customer Impact - The right type name can be show in the Items Collection Editor ## Regression? - No ## Risk - Minimal <!-- end TELL-MODE --> ## Screenshots <!-- Remove this section if PR does not change UI --> ### Before The wrong type name show in the Items Collection Editor ![image](https://github.com/user-attachments/assets/873bf817-1165-4a5d-972c-3b9054959b82) ### After The type name `ToolStripDropDownMenu` can be show correctly, and the item name is empty ![image](https://github.com/user-attachments/assets/222bb269-7229-40cf-96a8-3c176f8aae84) ## Test methodology <!-- How did you ensure quality? --> - Manually ## Test environment(s) <!-- Remove any that don't apply --> - .net 10.0.0-preview.3.25152.4 <!-- Mention language, UI scaling, or anything else that might be relevant --> ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/winforms/pull/13078)
2 parents a339d44 + 6c7abd1 commit df745d6

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ToolStripCollectionEditor.ToolStripItemEditorForm.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,8 @@ private void OnNewItemTypes_SelectionChangeCommitted(object sender, EventArgs e)
590590
{
591591
if (ex.IsCriticalException())
592592
{
593-
throw;
594-
}
593+
throw;
594+
}
595595
}
596596

597597
PropertyDescriptor imageProperty = TypeDescriptor.GetProperties(newItem)[nameof(Image)];
@@ -885,33 +885,30 @@ private void OnSelectedItemName_Paint(object sender, PaintEventArgs e)
885885
selectedItem = _listBoxItems.SelectedItem is ToolStrip strip ? strip : (ToolStripItem)_listBoxItems.SelectedItem;
886886

887887
string className = "&" + selectedItem.GetType().Name;
888-
if (selectedItem.Site is not null)
889-
{
890-
// Erase background
891-
e.Graphics.FillRectangle(SystemBrushes.Control, bounds);
892-
string itemName = selectedItem.Site.Name;
893-
894-
if (label is not null)
895-
{
896-
label.Text = className + itemName;
897-
}
888+
// Erase background
889+
e.Graphics.FillRectangle(SystemBrushes.Control, bounds);
890+
string itemName = selectedItem.Site?.Name ?? string.Empty;
898891

899-
int classWidth = 0;
900-
classWidth = (int)e.Graphics.MeasureString(className, boldFont).Width;
901-
e.Graphics.DrawString(className, boldFont, SystemBrushes.WindowText, bounds, stringFormat);
902-
int itemTextWidth = (int)e.Graphics.MeasureString(itemName, _selectedItemName.Font).Width;
903-
Rectangle textRect = new(classWidth + GdiPlusExtraSpace, 0, bounds.Width - (classWidth + GdiPlusExtraSpace), bounds.Height);
904-
label.AutoEllipsis = itemTextWidth > textRect.Width;
892+
if (label is not null)
893+
{
894+
label.Text = className + itemName;
895+
}
905896

906-
TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.PreserveGraphicsTranslateTransform | TextFormatFlags.PreserveGraphicsClipping;
907-
if (rightToLeft)
908-
{
909-
flags |= TextFormatFlags.RightToLeft;
910-
}
897+
int classWidth = 0;
898+
classWidth = (int)e.Graphics.MeasureString(className, boldFont).Width;
899+
e.Graphics.DrawString(className, boldFont, SystemBrushes.WindowText, bounds, stringFormat);
900+
int itemTextWidth = (int)e.Graphics.MeasureString(itemName, _selectedItemName.Font).Width;
901+
Rectangle textRect = new(classWidth + GdiPlusExtraSpace, 0, bounds.Width - (classWidth + GdiPlusExtraSpace), bounds.Height);
902+
label.AutoEllipsis = itemTextWidth > textRect.Width;
911903

912-
TextRenderer.DrawText(e.Graphics, itemName, _selectedItemName.Font, textRect, SystemColors.WindowText, flags);
904+
TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.PreserveGraphicsTranslateTransform | TextFormatFlags.PreserveGraphicsClipping;
905+
if (rightToLeft)
906+
{
907+
flags |= TextFormatFlags.RightToLeft;
913908
}
914909

910+
TextRenderer.DrawText(e.Graphics, itemName, _selectedItemName.Font, textRect, SystemColors.WindowText, flags);
911+
915912
break;
916913
case 0:
917914
// Erase background.

0 commit comments

Comments
 (0)