Si la boîte de Dialogue impression Annulé, Alors Ne pas Imprimer?
-
14-11-2019 - |
Question
Si j'exécute ce code, et appuyez sur annuler sur le PrintDialog, elle affiche toujours.Comment puis-je savoir si l'utilisation enfoncé annuler?
PrintDocument document = new PrintDocument();
PrintDialog dialog = new PrintDialog();
dialog.ShowDialog();
document.PrinterSettings = p.PrinterSettings;
document.Print();
Addendum
WebBrowser w = new WebBrowser();
w.ShowPrintDialog(); //.ShowPrintDialog returns a void, how can I deal with this?
La solution
Vous pouvez vérifier le résultat de la méthode ShowDialog:
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//Print
}
Autres conseils
Showdialog renvoie une énumération de résultat de la boîte de dialogue.Il sera correct ou annulera.
PrintDocument document = new PrintDocument();
PrintDialog dialog = new PrintDialog();
if(dialog.ShowDialog() == DialogResult.Ok)
{
document.PrinterSettings = p.PrinterSettings;
document.Print();
}
Les réponses ci-dessus sont correctes pour System.Windows.Forms.PrintDialog
.Toutefois, si vous n'êtes pas la construction d'un Forms
l'application, l' PrintDialog
vous allez utiliser est System.Windows.Controls.PrintDialog
.Ici, ShowDialog
retourne un bool?
:
var dialog = new System.Windows.Controls.PrintDialog();
if (dialog.ShowDialog() == true)
{
// Print...
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow