diff --git a/src/System.Windows.Forms/src/PublicAPI.Shipped.txt b/src/System.Windows.Forms/src/PublicAPI.Shipped.txt index 9813e6eec52..e0ec8d6c101 100644 --- a/src/System.Windows.Forms/src/PublicAPI.Shipped.txt +++ b/src/System.Windows.Forms/src/PublicAPI.Shipped.txt @@ -2247,10 +2247,10 @@ System.Windows.Forms.ListControl.ValueMember.set -> void ~System.Windows.Forms.PictureBox.LoadAsync(string url) -> void ~System.Windows.Forms.PrintControllerWithStatusDialog.PrintControllerWithStatusDialog(System.Drawing.Printing.PrintController underlyingController) -> void ~System.Windows.Forms.PrintControllerWithStatusDialog.PrintControllerWithStatusDialog(System.Drawing.Printing.PrintController underlyingController, string dialogTitle) -> void -~System.Windows.Forms.PrintDialog.Document.get -> System.Drawing.Printing.PrintDocument -~System.Windows.Forms.PrintDialog.Document.set -> void -~System.Windows.Forms.PrintDialog.PrinterSettings.get -> System.Drawing.Printing.PrinterSettings -~System.Windows.Forms.PrintDialog.PrinterSettings.set -> void +System.Windows.Forms.PrintDialog.Document.get -> System.Drawing.Printing.PrintDocument? +System.Windows.Forms.PrintDialog.Document.set -> void +System.Windows.Forms.PrintDialog.PrinterSettings.get -> System.Drawing.Printing.PrinterSettings! +System.Windows.Forms.PrintDialog.PrinterSettings.set -> void ~System.Windows.Forms.PrintPreviewControl.Document.get -> System.Drawing.Printing.PrintDocument ~System.Windows.Forms.PrintPreviewControl.Document.set -> void ~System.Windows.Forms.PrintPreviewDialog.AcceptButton.get -> System.Windows.Forms.IButtonControl 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 ae9f9aebf5f..e59e006819e 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 @@ -2,9 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Drawing.Printing; using System.Runtime.InteropServices; using static Interop; @@ -25,18 +24,18 @@ public sealed class PrintDialog : CommonDialog private const PD printRangeMask = PD.ALLPAGES | PD.PAGENUMS | PD.SELECTION | PD.CURRENTPAGE; // If PrintDocument is not null, settings == printDocument.PrinterSettings - private PrinterSettings settings; - private PrintDocument printDocument; + private PrinterSettings? _printerSettings; + private PrintDocument? _printDocument; // Implementing "current page" would require switching to PrintDlgEx, which is windows 2000 and later only - private bool allowCurrentPage; + private bool _allowCurrentPage; - private bool allowPages; - private bool allowPrintToFile; - private bool allowSelection; - private bool printToFile; - private bool showHelp; - private bool showNetwork; + private bool _allowPages; + private bool _allowPrintToFile; + private bool _allowSelection; + private bool _printToFile; + private bool _showHelp; + private bool _showNetwork; /// /// Initializes a new instance of the class. @@ -53,8 +52,8 @@ public PrintDialog() [SRDescription(nameof(SR.PDallowCurrentPageDescr))] public bool AllowCurrentPage { - get { return allowCurrentPage; } - set { allowCurrentPage = value; } + get { return _allowCurrentPage; } + set { _allowCurrentPage = value; } } /// @@ -65,8 +64,8 @@ public bool AllowCurrentPage [SRDescription(nameof(SR.PDallowPagesDescr))] public bool AllowSomePages { - get { return allowPages; } - set { allowPages = value; } + get { return _allowPages; } + set { _allowPages = value; } } /// @@ -77,8 +76,8 @@ public bool AllowSomePages [SRDescription(nameof(SR.PDallowPrintToFileDescr))] public bool AllowPrintToFile { - get { return allowPrintToFile; } - set { allowPrintToFile = value; } + get { return _allowPrintToFile; } + set { _allowPrintToFile = value; } } /// @@ -89,8 +88,8 @@ public bool AllowPrintToFile [SRDescription(nameof(SR.PDallowSelectionDescr))] public bool AllowSelection { - get { return allowSelection; } - set { allowSelection = value; } + get { return _allowSelection; } + set { _allowSelection = value; } } /// @@ -99,19 +98,19 @@ public bool AllowSelection [SRCategory(nameof(SR.CatData))] [DefaultValue(null)] [SRDescription(nameof(SR.PDdocumentDescr))] - public PrintDocument Document + public PrintDocument? Document { - get { return printDocument; } + get { return _printDocument; } set { - printDocument = value; - if (printDocument is null) + _printDocument = value; + if (_printDocument is null) { - settings = new PrinterSettings(); + _printerSettings = new PrinterSettings(); } else { - settings = printDocument.PrinterSettings; + _printerSettings = _printDocument.PrinterSettings; } } } @@ -140,23 +139,24 @@ private PageSettings PageSettings [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [SRDescription(nameof(SR.PDprinterSettingsDescr))] + [AllowNull] public PrinterSettings PrinterSettings { get { - if (settings is null) + if (_printerSettings is null) { - settings = new PrinterSettings(); + _printerSettings = new PrinterSettings(); } - return settings; + return _printerSettings; } set { if (value != PrinterSettings) { - settings = value; - printDocument = null; + _printerSettings = value; + _printDocument = null; } } } @@ -169,8 +169,8 @@ public PrinterSettings PrinterSettings [SRDescription(nameof(SR.PDprintToFileDescr))] public bool PrintToFile { - get { return printToFile; } - set { printToFile = value; } + get { return _printToFile; } + set { _printToFile = value; } } /// @@ -181,8 +181,8 @@ public bool PrintToFile [SRDescription(nameof(SR.PDshowHelpDescr))] public bool ShowHelp { - get { return showHelp; } - set { showHelp = value; } + get { return _showHelp; } + set { _showHelp = value; } } /// @@ -193,8 +193,8 @@ public bool ShowHelp [SRDescription(nameof(SR.PDshowNetworkDescr))] public bool ShowNetwork { - get { return showNetwork; } - set { showNetwork = value; } + get { return _showNetwork; } + set { _showNetwork = value; } } /// @@ -218,39 +218,39 @@ private PD GetFlags() flags |= PD.ENABLEPRINTHOOK; } - if (!allowCurrentPage) + if (!_allowCurrentPage) { flags |= PD.NOCURRENTPAGE; } - if (!allowPages) + if (!_allowPages) { flags |= PD.NOPAGENUMS; } - if (!allowPrintToFile) + if (!_allowPrintToFile) { flags |= PD.DISABLEPRINTTOFILE; } - if (!allowSelection) + if (!_allowSelection) { flags |= PD.NOSELECTION; } flags |= (PD)PrinterSettings.PrintRange; - if (printToFile) + if (_printToFile) { flags |= PD.PRINTTOFILE; } - if (showHelp) + if (_showHelp) { flags |= PD.SHOWHELP; } - if (!showNetwork) + if (!_showNetwork) { flags |= PD.NONETWORKBUTTON; } @@ -269,15 +269,15 @@ private PD GetFlags() /// public override void Reset() { - allowCurrentPage = false; - allowPages = false; - allowPrintToFile = true; - allowSelection = false; - printDocument = null; - printToFile = false; - settings = null; - showHelp = false; - showNetwork = true; + _allowCurrentPage = false; + _allowPages = false; + _allowPrintToFile = true; + _allowSelection = false; + _printDocument = null; + _printToFile = false; + _printerSettings = null; + _showHelp = false; + _showNetwork = true; } internal unsafe static NativeMethods.PRINTDLGEX CreatePRINTDLGEX() @@ -400,7 +400,7 @@ private unsafe bool ShowPrintDialog(IntPtr hwndOwner) return false; } - UpdatePrinterSettings(data.hDevMode, data.hDevNames, (short)data.nCopies, data.Flags, settings, PageSettings); + UpdatePrinterSettings(data.hDevMode, data.hDevNames, (short)data.nCopies, data.Flags, _printerSettings!, PageSettings); PrintToFile = (data.Flags & PD.PRINTTOFILE) != 0; PrinterSettings.PrintToFile = PrintToFile; @@ -495,7 +495,6 @@ private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.PRINTDLGEX data) data.nMaxPage = PrinterSettings.MaximumPage; } - // // The flags NativeMethods.PD_SHOWHELP and NativeMethods.PD_NONETWORKBUTTON don't work with // PrintDlgEx. So we have to strip them out. data.Flags &= ~(PD.SHOWHELP | PD.NONETWORKBUTTON); @@ -556,7 +555,7 @@ private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.PRINTDLGEX data) // Due to the nature of PRINTDLGEX vs PRINTDLG, separate but similar methods // are required for updating the settings from the structure utilized by the dialog. // Take information from print dialog and put in PrinterSettings - private static void UpdatePrinterSettings(IntPtr hDevMode, IntPtr hDevNames, short copies, PD flags, PrinterSettings settings, PageSettings pageSettings) + private static void UpdatePrinterSettings(IntPtr hDevMode, IntPtr hDevNames, short copies, PD flags, PrinterSettings settings, PageSettings? pageSettings) { // Mode settings.SetHdevmode(hDevMode);