From 43815866b4dded610b274859dff8c395571a30af Mon Sep 17 00:00:00 2001 From: gpetrou <4172445+gpetrou@users.noreply.github.com> Date: Wed, 3 Nov 2021 08:45:42 +0200 Subject: [PATCH] Use 'is not null' in some System.Windows.Forms files (#6112) ## Proposed changes - Contributes to https://github.com/dotnet/winforms/issues/3459 --- .../Windows/Forms/AutomationMessages.cs | 6 ++--- .../System/Windows/Forms/Control.Annotated.cs | 2 +- .../ComponentEditorForm.PageSelector.cs | 2 +- .../Forms/Design/ComponentEditorForm.cs | 10 ++++----- .../Forms/Design/ComponentEditorPage.cs | 2 +- .../src/System/Windows/Forms/DomainUpDown.cs | 6 ++--- .../Forms/DrawListViewItemEventArgs.cs | 2 +- .../src/System/Windows/Forms/DropTarget.cs | 2 +- .../Forms/KeyboardToolTipStateMachine.cs | 12 +++++----- .../src/System/Windows/Forms/KeysConverter.cs | 2 +- .../Windows/Forms/Layout/CommonProperties.cs | 2 +- .../Forms/Layout/DockAndAnchorLayout.cs | 22 +++++++++---------- .../System/Windows/Forms/Layout/FlowLayout.cs | 2 +- .../Windows/Forms/Layout/LayoutTransaction.cs | 12 +++++----- .../Windows/Forms/Layout/LayoutUtils.cs | 2 +- .../Forms/Layout/TableLayout.ContainerInfo.cs | 6 ++--- .../Windows/Forms/Layout/TableLayout.cs | 16 +++++++------- .../src/System/Windows/Forms/MessageBox.cs | 2 +- .../Windows/Forms/Printing/PageSetupDialog.cs | 14 ++++++------ ...rollerWithStatusDialog.BackgroundThread.cs | 6 ++--- .../PrintControllerWithStatusDialog.cs | 16 +++++++------- .../Windows/Forms/Printing/PrintDialog.cs | 4 ++-- .../Forms/Printing/PrintPreviewControl.cs | 10 ++++----- .../Forms/Printing/PrintPreviewDialog.cs | 4 ++-- .../Forms/VisualStyles/VisualStyleRenderer.cs | 6 ++--- .../src/misc/MultitargetUtil.cs | 4 ++-- .../src/misc/WeakHashtable.cs | 4 ++-- 27 files changed, 89 insertions(+), 89 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/AutomationMessages.cs b/src/System.Windows.Forms/src/System/Windows/Forms/AutomationMessages.cs index 0c85c6714f6..9383ca1d982 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/AutomationMessages.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/AutomationMessages.cs @@ -33,7 +33,7 @@ public static IntPtr WriteAutomationText(string text) IntPtr fileId = IntPtr.Zero; string fullFileName = GenerateLogFileName(ref fileId); - if (fullFileName != null) + if (fullFileName is not null) { try { @@ -96,9 +96,9 @@ private static string GenerateLogFileName(ref IntPtr fileId) string fullFileName = null; string filePath = System.Environment.GetEnvironmentVariable("TEMP"); - Debug.Assert(filePath != null, "Could not get value of the TEMP environment variable"); + Debug.Assert(filePath is not null, "Could not get value of the TEMP environment variable"); - if (filePath != null) + if (filePath is not null) { if (fileId == IntPtr.Zero) // Create id { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Control.Annotated.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Control.Annotated.cs index c1af0344719..e9e3860bf3e 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Control.Annotated.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Control.Annotated.cs @@ -26,6 +26,6 @@ public partial class Control internal bool TryGetService( [NotNullWhen(true)] out T? service) where T : class - => (service = GetService(typeof(T)) as T) != null; + => (service = GetService(typeof(T)) as T) is not null; } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorForm.PageSelector.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorForm.PageSelector.cs index cc9e82cbb04..cf1dd3a3d2e 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorForm.PageSelector.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorForm.PageSelector.cs @@ -204,7 +204,7 @@ private unsafe void OnCustomDraw(ref Message m) case ComCtl32.CDDS.ITEMPREPAINT: { TreeNode itemNode = TreeNode.FromHandle(this, nmtvcd->nmcd.dwItemSpec); - if (itemNode != null) + if (itemNode is not null) { int state = STATE_NORMAL; ComCtl32.CDIS itemState = nmtvcd->nmcd.uItemState; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorForm.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorForm.cs index affd48d5dc1..8ed92b62649 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorForm.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorForm.cs @@ -167,10 +167,10 @@ private void OnButtonClick(object sender, EventArgs e) private void OnConfigureUI() { Font uiFont = Control.DefaultFont; - if (component.Site != null) + if (component.Site is not null) { IUIService uiService = (IUIService)component.Site.GetService(typeof(IUIService)); - if (uiService != null) + if (uiService is not null) { uiFont = (Font)uiService.Styles["DialogFont"]; } @@ -200,7 +200,7 @@ private void OnConfigureUI() int selectorWidth = MIN_SELECTOR_WIDTH; - if (pageSites != null) + if (pageSites is not null) { // Add the nodes corresponding to the pages for (int n = 0; n < pageSites.Length; n++) @@ -225,7 +225,7 @@ private void OnConfigureUI() string caption = string.Empty; ISite site = component.Site; - if (site != null) + if (site is not null) { caption = string.Format(SR.ComponentEditorFormProperties, site.Name); } @@ -242,7 +242,7 @@ private void OnConfigureUI() grayStrip.Bounds = new Rectangle(pageHostBounds.X, BUTTON_PAD, pageHostBounds.Width, STRIP_HEIGHT); - if (pageSites != null) + if (pageSites is not null) { Rectangle pageBounds = new Rectangle(0, 0, pageHostBounds.Width, pageHostBounds.Height); for (int n = 0; n < pageSites.Length; n++) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorPage.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorPage.cs index 175d7ee19fd..31641b77b3d 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorPage.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorPage.cs @@ -207,7 +207,7 @@ protected virtual void ReloadComponent() /// protected virtual void SetDirty() { - if (!IsLoading() && PageSite != null) + if (!IsLoading() && PageSite is not null) { PageSite.SetDirty(); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.cs b/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.cs index 6cc1ea2e05a..e62fb32495b 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/DomainUpDown.cs @@ -411,7 +411,7 @@ private void SelectIndex(int index) { // Sanity check index - Debug.Assert(_domainItems != null, "Domain values array is null"); + Debug.Assert(_domainItems is not null, "Domain values array is null"); Debug.Assert(index < _domainItems.Count && index >= -1, "SelectValue: index out of range"); if (_domainItems is null || index < -1 || index >= _domainItems.Count) { @@ -453,7 +453,7 @@ private void SortDomainItems() // Sanity check Debug.Assert(_sorted, "Sorted == false"); - if (_domainItems != null) + if (_domainItems is not null) { // Sort the domain values ArrayList.Adapter(_domainItems).Sort(new DomainUpDownItemCompare()); @@ -482,7 +482,7 @@ public override string ToString() { string s = base.ToString(); - if (Items != null) + if (Items is not null) { s += ", Items.Count: " + Items.Count.ToString(CultureInfo.CurrentCulture); s += ", SelectedIndex: " + SelectedIndex.ToString(CultureInfo.CurrentCulture); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/DrawListViewItemEventArgs.cs b/src/System.Windows.Forms/src/System/Windows/Forms/DrawListViewItemEventArgs.cs index f28fc505b79..50efc0b3a41 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/DrawListViewItemEventArgs.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/DrawListViewItemEventArgs.cs @@ -95,7 +95,7 @@ public void DrawText(TextFormatFlags flags) private Rectangle UpdateBounds(Rectangle originalBounds, bool drawText) { Rectangle resultBounds = originalBounds; - if (Item.ListView != null && Item.ListView.View == View.Details) + if (Item.ListView is not null && Item.ListView.View == View.Details) { // Note: this logic will compute the bounds so they align w/ the system drawn bounds only // for the default font. diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/DropTarget.cs b/src/System.Windows.Forms/src/System/Windows/Forms/DropTarget.cs index 648e92c8e73..b18771a3a57 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/DropTarget.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/DropTarget.cs @@ -62,7 +62,7 @@ HRESULT Ole32.IDropTarget.DragEnter(object pDataObj, uint grfKeyState, Point pt, { Debug.WriteLineIf(CompModSwitches.DragDrop.TraceInfo, "OleDragEnter received"); Debug.WriteLineIf(CompModSwitches.DragDrop.TraceInfo, "\t" + (pt.X) + "," + (pt.Y)); - Debug.Assert(pDataObj != null, "OleDragEnter didn't give us a valid data object."); + Debug.Assert(pDataObj is not null, "OleDragEnter didn't give us a valid data object."); DragEventArgs? drgevent = CreateDragEventArgs(pDataObj, grfKeyState, pt, pdwEffect); if (drgevent is not null) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/KeyboardToolTipStateMachine.cs b/src/System.Windows.Forms/src/System/Windows/Forms/KeyboardToolTipStateMachine.cs index 082bcfef92b..03c939291c1 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/KeyboardToolTipStateMachine.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/KeyboardToolTipStateMachine.cs @@ -103,7 +103,7 @@ public void NotifyAboutMouseEnter(IKeyboardToolTip sender) private bool IsToolTracked(IKeyboardToolTip sender) { - return _toolToTip[sender] != null; + return _toolToTip[sender] is not null; } public void NotifyAboutLostFocus(IKeyboardToolTip sender) @@ -246,7 +246,7 @@ private void Transit(SmEvent @event, IKeyboardToolTip source) try { ToolTip toolTip = _toolToTip[source]; - if ((_currentTool is null || _currentTool.CanShowToolTipsNow()) && toolTip != null) + if ((_currentTool is null || _currentTool.CanShowToolTipsNow()) && toolTip is not null) { _currentState = Transition(source, toolTip, @event); } @@ -290,10 +290,10 @@ private void HidePersistent() private SmState FullFsmReset() { - if (_currentState == SmState.Shown && _currentTool != null) + if (_currentState == SmState.Shown && _currentTool is not null) { ToolTip currentToolTip = _toolToTip[_currentTool]; - if (currentToolTip != null) + if (currentToolTip is not null) { currentToolTip.HideToolTip(_currentTool); } @@ -312,7 +312,7 @@ private void ResetTimer() private void Reset(ToolTip toolTipToReset) { - if (toolTipToReset is null || (_currentTool != null && _toolToTip[_currentTool] == toolTipToReset)) + if (toolTipToReset is null || (_currentTool is not null && _toolToTip[_currentTool] == toolTipToReset)) { FullFsmReset(); } @@ -332,7 +332,7 @@ private void StopTracking(IKeyboardToolTip tool, ToolTip toolTip) private void OnFormDeactivation(ToolTip sender) { - if (_currentTool != null && _toolToTip[_currentTool] == sender) + if (_currentTool is not null && _toolToTip[_currentTool] == sender) { FullFsmReset(); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/KeysConverter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/KeysConverter.cs index 41c3c1760f3..928fd1c371c 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/KeysConverter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/KeysConverter.cs @@ -180,7 +180,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c obj = Enum.Parse(typeof(Keys), tokens[i]); } - if (obj != null) + if (obj is not null) { Keys currentKey = (Keys)obj; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/CommonProperties.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/CommonProperties.cs index 447999aac4a..3f8a2075a46 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/CommonProperties.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/CommonProperties.cs @@ -169,7 +169,7 @@ internal static Rectangle GetSpecifiedBounds(IArrangedElement element) internal static void ResetPadding(IArrangedElement element) { object value = element.Properties.GetObject(_paddingProperty); - if (value != null) + if (value is not null) { element.Properties.RemoveObject(_paddingProperty); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/DockAndAnchorLayout.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/DockAndAnchorLayout.cs index 6e2095e6e75..6e9db482778 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/DockAndAnchorLayout.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/DockAndAnchorLayout.cs @@ -299,7 +299,7 @@ private static void LayoutAnchoredControls(IArrangedElement container) IArrangedElement element = children[i]; if (CommonProperties.GetNeedsAnchorLayout(element)) { - Debug.Assert(GetAnchorInfo(element) != null, "AnchorInfo should be initialized before LayoutAnchorControls()."); + Debug.Assert(GetAnchorInfo(element) is not null, "AnchorInfo should be initialized before LayoutAnchorControls()."); SetCachedBounds(element, GetAnchorDestination(element, displayRectangle, /*measureOnly=*/false)); } } @@ -403,7 +403,7 @@ private static Size LayoutDockedControls(IArrangedElement container, bool measur } // Treat the MDI client specially, since it's supposed to blend in with the parent form - if (mdiClient != null) + if (mdiClient is not null) { SetCachedBounds(mdiClient, remainingBounds); } @@ -641,7 +641,7 @@ private static void UpdateAnchorInfo(IArrangedElement element) Debug.Indent(); Debug.WriteLineIf(CompModSwitches.RichLayout.TraceInfo, element.Container is null ? "No parent" : "Parent"); - if (CommonProperties.GetNeedsAnchorLayout(element) && element.Container != null) + if (CommonProperties.GetNeedsAnchorLayout(element) && element.Container is not null) { Rectangle bounds = GetCachedBounds(element); AnchorInfo oldAnchorInfo = new AnchorInfo @@ -750,11 +750,11 @@ public static void SetAnchor(IArrangedElement container, IArrangedElement elemen SetAnchorInfo(element, null); } - if (element.Container != null) + if (element.Container is not null) { bool rightReleased = IsAnchored(oldValue, AnchorStyles.Right) && !IsAnchored(value, AnchorStyles.Right); bool bottomReleased = IsAnchored(oldValue, AnchorStyles.Bottom) && !IsAnchored(value, AnchorStyles.Bottom); - if (element.Container.Container != null && (rightReleased || bottomReleased)) + if (element.Container.Container is not null && (rightReleased || bottomReleased)) { // If the right or bottom anchor is being released, we have a special case where the element's // margin may affect preferredSize where it didn't previously. Rather than do an expensive @@ -810,7 +810,7 @@ public static void ScaleAnchorInfo(IArrangedElement element, SizeF factor) AnchorInfo anchorInfo = GetAnchorInfo(element); // some controls don't have AnchorInfo, i.e. Panels - if (anchorInfo != null) + if (anchorInfo is not null) { anchorInfo.Left = (int)((float)anchorInfo.Left * factor.Width); anchorInfo.Top = (int)((float)anchorInfo.Top * factor.Height); @@ -823,13 +823,13 @@ public static void ScaleAnchorInfo(IArrangedElement element, SizeF factor) private static Rectangle GetCachedBounds(IArrangedElement element) { - if (element.Container != null) + if (element.Container is not null) { IDictionary dictionary = (IDictionary)element.Container.Properties.GetObject(s_cachedBoundsProperty); - if (dictionary != null) + if (dictionary is not null) { object bounds = dictionary[element]; - if (bounds != null) + if (bounds is not null) { return (Rectangle)bounds; } @@ -841,7 +841,7 @@ private static Rectangle GetCachedBounds(IArrangedElement element) private static bool HasCachedBounds(IArrangedElement container) { - return container != null && container.Properties.GetObject(s_cachedBoundsProperty) != null; + return container is not null && container.Properties.GetObject(s_cachedBoundsProperty) is not null; } private static void ApplyCachedBounds(IArrangedElement container) @@ -858,7 +858,7 @@ private static void ApplyCachedBounds(IArrangedElement container) } IDictionary dictionary = (IDictionary)container.Properties.GetObject(s_cachedBoundsProperty); - if (dictionary != null) + if (dictionary is not null) { #if DEBUG // In debug builds, we need to modify the collection, so we add a break and an diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/FlowLayout.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/FlowLayout.cs index 141bc414606..58067a91dce 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/FlowLayout.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/FlowLayout.cs @@ -329,7 +329,7 @@ private void Debug_VerifyAlignment(IArrangedElement container, FlowDirection flo // We cannot apply any of these checks @ design-time since dragging new children into a FlowLayoutPanel // will attempt to set the children at the mouse position when the child was dropped - we rely on the control // to reposition the children once added. - if (container is Control flp && flp.Site != null && flp.Site.DesignMode) + if (container is Control flp && flp.Site is not null && flp.Site.DesignMode) { return; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/LayoutTransaction.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/LayoutTransaction.cs index ea2f4b8abed..a1792213343 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/LayoutTransaction.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/LayoutTransaction.cs @@ -43,7 +43,7 @@ public LayoutTransaction(Control? controlToLayout, IArrangedElement? controlCaus _controlToLayout = controlToLayout; _resumeLayout = resumeLayout; - if (_controlToLayout != null) + if (_controlToLayout is not null) { #if DEBUG _layoutSuspendCount = _controlToLayout.LayoutSuspendCount; @@ -62,7 +62,7 @@ public LayoutTransaction(Control? controlToLayout, IArrangedElement? controlCaus public void Dispose() { - if (_controlToLayout != null) + if (_controlToLayout is not null) { _controlToLayout.ResumeLayout(_resumeLayout); @@ -90,17 +90,17 @@ public static IDisposable CreateTransactionIf(bool condition, Control? controlTo public static void DoLayout(IArrangedElement? elementToLayout, IArrangedElement? elementCausingLayout, string? property) { - if (elementCausingLayout != null) + if (elementCausingLayout is not null) { CommonProperties.xClearPreferredSizeCache(elementCausingLayout); - if (elementToLayout != null) + if (elementToLayout is not null) { CommonProperties.xClearPreferredSizeCache(elementToLayout); elementToLayout.PerformLayout(elementCausingLayout, property); } } - Debug.Assert(elementCausingLayout != null, "LayoutTransaction.DoLayout - elementCausingLayout is null, no layout performed - did you mix up your parameters?"); + Debug.Assert(elementCausingLayout is not null, "LayoutTransaction.DoLayout - elementCausingLayout is null, no layout performed - did you mix up your parameters?"); } // This overload should be used when a property has changed that affects preferred size, @@ -110,7 +110,7 @@ public static void DoLayoutIf(bool condition, IArrangedElement? elementToLayout, { if (!condition) { - if (elementCausingLayout != null) + if (elementCausingLayout is not null) { CommonProperties.xClearPreferredSizeCache(elementCausingLayout); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/LayoutUtils.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/LayoutUtils.cs index fa822709821..6e04b707b6e 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/LayoutUtils.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/LayoutUtils.cs @@ -45,7 +45,7 @@ internal partial class LayoutUtils public static Size OldGetLargestStringSizeInCollection(Font? font, ICollection? objects) { Size largestSize = Size.Empty; - if (objects != null) + if (objects is not null) { foreach (object obj in objects) { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/TableLayout.ContainerInfo.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/TableLayout.ContainerInfo.cs index db3c5d2743c..5c3c26884a8 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/TableLayout.ContainerInfo.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/TableLayout.ContainerInfo.cs @@ -205,7 +205,7 @@ public TableLayoutRowStyleCollection RowStyles set { _rowStyles = value; - if (_rowStyles != null) + if (_rowStyles is not null) { _rowStyles.EnsureOwnership(_container); } @@ -226,7 +226,7 @@ public TableLayoutColumnStyleCollection ColumnStyles set { _colStyles = value; - if (_colStyles != null) + if (_colStyles is not null) { _colStyles.EnsureOwnership(_container); } @@ -348,7 +348,7 @@ public bool HasMultiplePercentColumns { get { - if (_colStyles != null) + if (_colStyles is not null) { bool foundAny = false; foreach (ColumnStyle style in _colStyles) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/TableLayout.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/TableLayout.cs index 0c5b94b9ab5..148e5bed247 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Layout/TableLayout.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Layout/TableLayout.cs @@ -182,12 +182,12 @@ internal override Size GetPreferredSize(IArrangedElement container, Size propose proposedConstraints -= cellBorderSize; proposedConstraints.Width = Math.Max(proposedConstraints.Width, 1); proposedConstraints.Height = Math.Max(proposedConstraints.Height, 1); - if (tempInfo.Columns != null && containerInfo.Columns != null && (tempInfo.Columns.Length != containerInfo.Columns.Length)) + if (tempInfo.Columns is not null && containerInfo.Columns is not null && (tempInfo.Columns.Length != containerInfo.Columns.Length)) { ClearCachedAssignments(containerInfo); } - if (tempInfo.Rows != null && containerInfo.Rows != null && (tempInfo.Rows.Length != containerInfo.Rows.Length)) + if (tempInfo.Rows is not null && containerInfo.Rows is not null && (tempInfo.Rows.Length != containerInfo.Rows.Length)) { ClearCachedAssignments(containerInfo); } @@ -344,11 +344,11 @@ private bool xAssignRowsAndColumns(ContainerInfo containerInfo, LayoutInfo[] chi //the element at the head of the non-absolutely positioned element queue LayoutInfo flowElement = GetNextLayoutInfo(childrenInfo, ref flowElementIndex, /*absolutelyPositioned*/false); - while (fixedElement != null || flowElement != null) + while (fixedElement is not null || flowElement is not null) { int colStop = currentCol; int rowStop; - if (flowElement != null) + if (flowElement is not null) { flowElement.RowStart = currentRow; flowElement.ColumnStart = currentCol; @@ -362,7 +362,7 @@ private bool xAssignRowsAndColumns(ContainerInfo containerInfo, LayoutInfo[] chi } //test to see if either the absolutely positioned element is null or it fits. - if (flowElement != null && (fixedElement is null || (!IsCursorPastInsertionPoint(fixedElement, flowElement.RowStart, colStop) && !IsOverlappingWithReservationGrid(fixedElement, reservationGrid, currentRow)))) + if (flowElement is not null && (fixedElement is null || (!IsCursorPastInsertionPoint(fixedElement, flowElement.RowStart, colStop) && !IsOverlappingWithReservationGrid(fixedElement, reservationGrid, currentRow)))) { //Place the flow element. @@ -659,7 +659,7 @@ private void InitializeStrips(Strip[] strips, IList styles) { TableLayoutStyle style = i < styles.Count ? (TableLayoutStyle)styles[i] : null; strip = strips[i]; - if (style != null && style.SizeType == SizeType.Absolute) + if (style is not null && style.SizeType == SizeType.Absolute) { strip.MinSize = (int)Math.Round((double)((TableLayoutStyle)styles[i]).Size); strip.MaxSize = strip.MinSize; @@ -695,7 +695,7 @@ private int InflateColumns(ContainerInfo containerInfo, Size proposedConstraints if (dontHonorConstraint && (proposedConstraints.Width < short.MaxValue)) { - if (containerInfo.Container is TableLayoutPanel tlp && tlp.ParentInternal != null && tlp.ParentInternal.LayoutEngine == DefaultLayout.Instance) + if (containerInfo.Container is TableLayoutPanel tlp && tlp.ParentInternal is not null && tlp.ParentInternal.LayoutEngine == DefaultLayout.Instance) { if (tlp.Dock == DockStyle.Top || tlp.Dock == DockStyle.Bottom || tlp.Dock == DockStyle.Fill) { @@ -821,7 +821,7 @@ private int InflateRows(ContainerInfo containerInfo, Size proposedConstraints, i if (dontHonorConstraint && (proposedConstraints.Height < short.MaxValue)) { - if (containerInfo.Container is TableLayoutPanel tlp && tlp.ParentInternal != null && tlp.ParentInternal.LayoutEngine == DefaultLayout.Instance) + if (containerInfo.Container is TableLayoutPanel tlp && tlp.ParentInternal is not null && tlp.ParentInternal.LayoutEngine == DefaultLayout.Instance) { if (tlp.Dock == DockStyle.Left || tlp.Dock == DockStyle.Right || tlp.Dock == DockStyle.Fill) { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/MessageBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/MessageBox.cs index 7898f5c6259..b7ceab7f09b 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/MessageBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/MessageBox.cs @@ -62,7 +62,7 @@ private static MB GetMessageBoxStyle(IWin32Window owner, MessageBoxButtons butto throw new InvalidOperationException(SR.CantShowModalOnNonInteractive); } - if (owner != null && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0) + if (owner is not null && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0) { throw new ArgumentException(SR.CantShowMBServiceWithOwner, nameof(options)); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PageSetupDialog.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PageSetupDialog.cs index fe0a2075778..07dc70e6602 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PageSetupDialog.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PageSetupDialog.cs @@ -20,7 +20,7 @@ namespace System.Windows.Forms [SRDescription(nameof(SR.DescriptionPageSetupDialog))] public sealed class PageSetupDialog : CommonDialog { - // If PrintDocument != null, pageSettings == printDocument.PageSettings + // If PrintDocument is not null, pageSettings == printDocument.PageSettings private PrintDocument _printDocument; private PageSettings _pageSettings; private PrinterSettings _printerSettings; @@ -78,7 +78,7 @@ public PrintDocument Document set { _printDocument = value; - if (_printDocument != null) + if (_printDocument is not null) { _pageSettings = _printDocument.DefaultPageSettings; _printerSettings = _printDocument.PrinterSettings; @@ -196,12 +196,12 @@ private Comdlg32.PSD GetFlags() flags |= Comdlg32.PSD.NONETWORKBUTTON; } - if (_minMargins != null) + if (_minMargins is not null) { flags |= Comdlg32.PSD.MINMARGINS; } - if (_pageSettings.Margins != null) + if (_pageSettings.Margins is not null) { flags |= Comdlg32.PSD.MARGINS; } @@ -248,7 +248,7 @@ private static void UpdateSettings(Comdlg32.PAGESETUPDLGW data, PageSettings pag PrinterSettings printerSettings) { pageSettings.SetHdevmode(data.hDevMode); - if (printerSettings != null) + if (printerSettings is not null) { printerSettings.SetHdevmode(data.hDevMode); printerSettings.SetHdevnames(data.hDevNames); @@ -302,7 +302,7 @@ protected unsafe override bool RunDialog(IntPtr hwndOwner) } } - if (MinMargins != null) + if (MinMargins is not null) { Margins margins = PrinterUnitConvert.Convert(MinMargins, PrinterUnit.Display, toUnit); data.rtMinMargin.left = margins.Left; @@ -311,7 +311,7 @@ protected unsafe override bool RunDialog(IntPtr hwndOwner) data.rtMinMargin.bottom = margins.Bottom; } - if (_pageSettings.Margins != null) + if (_pageSettings.Margins is not null) { Margins margins = PrinterUnitConvert.Convert(_pageSettings.Margins, PrinterUnit.Display, toUnit); data.rtMargin.left = margins.Left; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintControllerWithStatusDialog.BackgroundThread.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintControllerWithStatusDialog.BackgroundThread.cs index 27982dfea38..cc23e1b8a87 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintControllerWithStatusDialog.BackgroundThread.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintControllerWithStatusDialog.BackgroundThread.cs @@ -33,7 +33,7 @@ internal void Stop() { lock (this) { - if (_dialog != null && _dialog.IsHandleCreated) + if (_dialog is not null && _dialog.IsHandleCreated) { _dialog.BeginInvoke(new MethodInvoker(_dialog.Close)); return; @@ -46,7 +46,7 @@ internal void Stop() // Called from any thread internal void UpdateLabel() { - if (_dialog != null && _dialog.IsHandleCreated) + if (_dialog is not null && _dialog.IsHandleCreated) { _dialog.BeginInvoke(new MethodInvoker(ThreadUnsafeUpdateLabel)); // Don't wait for a response @@ -81,7 +81,7 @@ private void Run() { lock (this) { - if (_dialog != null) + if (_dialog is not null) { _dialog.Dispose(); _dialog = null; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintControllerWithStatusDialog.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintControllerWithStatusDialog.cs index 39f24a9a8ea..1416d9fe2a3 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintControllerWithStatusDialog.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintControllerWithStatusDialog.cs @@ -36,7 +36,7 @@ public override bool IsPreview { get { - if (_underlyingController != null) + if (_underlyingController is not null) { return _underlyingController.IsPreview; } @@ -70,7 +70,7 @@ public override void OnStartPrint(PrintDocument document, PrintEventArgs e) } catch { - if (_backgroundThread != null) + if (_backgroundThread is not null) { _backgroundThread.Stop(); } @@ -79,7 +79,7 @@ public override void OnStartPrint(PrintDocument document, PrintEventArgs e) } finally { - if (_backgroundThread != null && _backgroundThread._canceled) + if (_backgroundThread is not null && _backgroundThread._canceled) { e.Cancel = true; } @@ -93,13 +93,13 @@ public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs { base.OnStartPage(document, e); - if (_backgroundThread != null) + if (_backgroundThread is not null) { _backgroundThread.UpdateLabel(); } Graphics result = _underlyingController.OnStartPage(document, e); - if (_backgroundThread != null && _backgroundThread._canceled) + if (_backgroundThread is not null && _backgroundThread._canceled) { e.Cancel = true; } @@ -113,7 +113,7 @@ public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs public override void OnEndPage(PrintDocument document, PrintPageEventArgs e) { _underlyingController.OnEndPage(document, e); - if (_backgroundThread != null && _backgroundThread._canceled) + if (_backgroundThread is not null && _backgroundThread._canceled) { e.Cancel = true; } @@ -129,12 +129,12 @@ public override void OnEndPage(PrintDocument document, PrintPageEventArgs e) public override void OnEndPrint(PrintDocument document, PrintEventArgs e) { _underlyingController.OnEndPrint(document, e); - if (_backgroundThread != null && _backgroundThread._canceled) + if (_backgroundThread is not null && _backgroundThread._canceled) { e.Cancel = true; } - if (_backgroundThread != null) + if (_backgroundThread is not null) { _backgroundThread.Stop(); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintDialog.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintDialog.cs index c252d09eff9..af691718cfa 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintDialog.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintDialog.cs @@ -24,7 +24,7 @@ public sealed class PrintDialog : CommonDialog { private const PD printRangeMask = PD.ALLPAGES | PD.PAGENUMS | PD.SELECTION | PD.CURRENTPAGE; - // If PrintDocument != null, settings == printDocument.PrinterSettings + // If PrintDocument is not null, settings == printDocument.PrinterSettings private PrinterSettings settings; private PrintDocument printDocument; @@ -562,7 +562,7 @@ private static void UpdatePrinterSettings(IntPtr hDevMode, IntPtr hDevNames, sho settings.SetHdevmode(hDevMode); settings.SetHdevnames(hDevNames); - if (pageSettings != null) + if (pageSettings is not null) { pageSettings.SetHdevmode(hDevMode); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewControl.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewControl.cs index 7b9e2e0a9fb..208b6f6f813 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewControl.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewControl.cs @@ -233,7 +233,7 @@ public int StartPage get { int value = startPage; - if (pageInfo != null) + if (pageInfo is not null) { value = Math.Min(value, pageInfo.Length - (rows * columns)); } @@ -384,7 +384,7 @@ private unsafe int AdjustScroll(Message m, int pos, int maxPos, bool horizontal) // This function computes everything in terms of physical size (millimeters), not pixels private void ComputeLayout() { - Debug.Assert(pageInfo != null, "Must call ComputePreview first"); + Debug.Assert(pageInfo is not null, "Must call ComputePreview first"); layoutOk = true; if (pageInfo.Length == 0) { @@ -433,7 +433,7 @@ private void ComputePreview() document.Print(); pageInfo = previewController.GetPreviewPageInfo(); - Debug.Assert(pageInfo != null, "ReviewPrintController did not give us preview info"); + Debug.Assert(pageInfo is not null, "ReviewPrintController did not give us preview info"); document.PrintController = oldController; } @@ -514,7 +514,7 @@ protected override void OnPaint(PaintEventArgs pevent) { pevent.Graphics.FillRectangle(backBrush, ClientRectangle); - if (pageInfo != null || exceptionPrinting) + if (pageInfo is not null || exceptionPrinting) { // Calculate formats using StringFormat format = new StringFormat @@ -619,7 +619,7 @@ protected override void OnPaint(PaintEventArgs pevent) } box.Inflate(-1, -1); - if (pageInfo[i + StartPage].Image != null) + if (pageInfo[i + StartPage].Image is not null) { pevent.Graphics.DrawImage(pageInfo[i + StartPage].Image, box); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewDialog.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewDialog.cs index b24e42615c7..87666f08f59 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewDialog.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewDialog.cs @@ -1271,7 +1271,7 @@ protected override void CreateHandle() // We want to check printer settings before we push the modal message loop, // so the user has a chance to catch the exception instead of letting go to // the windows forms exception dialog. - if (Document != null && !Document.PrinterSettings.IsValid) + if (Document is not null && !Document.PrinterSettings.IsValid) { throw new InvalidPrinterException(Document.PrinterSettings); } @@ -1439,7 +1439,7 @@ void OncloseToolStripButtonPaint(object sender, PaintEventArgs e) void OnprintToolStripButtonClick(object sender, EventArgs e) { - if (previewControl.Document != null) + if (previewControl.Document is not null) { previewControl.Document.Print(); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/VisualStyles/VisualStyleRenderer.cs b/src/System.Windows.Forms/src/System/Windows/Forms/VisualStyles/VisualStyleRenderer.cs index 4ec6c8fd29a..f9290a79d8e 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/VisualStyles/VisualStyleRenderer.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/VisualStyles/VisualStyleRenderer.cs @@ -801,7 +801,7 @@ private static void RefreshCache() { ThemeHandle? tHandle = null; - if (t_themeHandles != null) + if (t_themeHandles is not null) { string[] classNames = new string[t_themeHandles.Keys.Count]; t_themeHandles.Keys.CopyTo(classNames, 0); @@ -809,7 +809,7 @@ private static void RefreshCache() foreach (string className in classNames) { tHandle = (ThemeHandle?)t_themeHandles[className]; - if (tHandle != null) + if (tHandle is not null) { tHandle.Dispose(); } @@ -819,7 +819,7 @@ private static void RefreshCache() if (AreClientAreaVisualStylesSupported) { tHandle = ThemeHandle.Create(className, false); - if (tHandle != null) + if (tHandle is not null) { t_themeHandles[className] = tHandle; } diff --git a/src/System.Windows.Forms/src/misc/MultitargetUtil.cs b/src/System.Windows.Forms/src/misc/MultitargetUtil.cs index fa407dc5ff9..22e58a59b3d 100644 --- a/src/System.Windows.Forms/src/misc/MultitargetUtil.cs +++ b/src/System.Windows.Forms/src/misc/MultitargetUtil.cs @@ -18,9 +18,9 @@ internal static class MultitargetUtil { string? assemblyQualifiedName = null; - if (type != null) + if (type is not null) { - if (typeNameConverter != null) + if (typeNameConverter is not null) { try { diff --git a/src/System.Windows.Forms/src/misc/WeakHashtable.cs b/src/System.Windows.Forms/src/misc/WeakHashtable.cs index f5de15f8acf..fed56af650f 100644 --- a/src/System.Windows.Forms/src/misc/WeakHashtable.cs +++ b/src/System.Windows.Forms/src/misc/WeakHashtable.cs @@ -106,7 +106,7 @@ private void ScavengeKeys() } } - if (cleanupList != null) + if (cleanupList is not null) { foreach (object o in cleanupList) { @@ -128,7 +128,7 @@ bool IEqualityComparer.Equals(object? x, object? y) return y is null; } - if (y != null && x.GetHashCode() == y.GetHashCode()) + if (y is not null && x.GetHashCode() == y.GetHashCode()) { if (x is WeakReference wX) {