Print Button

ad.txt

Member
Joined
Oct 4, 2022
Messages
17
Programming Experience
Beginner
Hi !
I'm completely new to C# so my question may be quite absurd. I have to add a print button on a Form.
My goal is that the user can just print a document by clicking on this button without having to browse in the computer.

I suppose I have to create a first code selecting the document from the computer and then a second one to print this document.
Is that right ?

I've tried many things and codes that I found on YouTube or on the internet but none of them have worked.

Do you have any idea of code that I can use to achieve my goal ?

I'm completely lost so any help would be amazing !

Thank you !! :)
 
What kind of document are you trying to print?

We don't just give you code in this forum. Show us what you have tried.
 
codetried.PNG

Thank you for your response !

I'd like to open an excel file.
This is what I tried but it doesn't work ...
 
Please post your code as text in code tags. Code in screenshots is very hard to deal with, specially on small devices like phones.
 
Is Excel installed on the machine that you want to print from?
 
C#:
namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
        
            OpenFileDialog dialog = new OpenFileDialog();
            if (DialogResult.OK == dialog.ShowDialog())
            {
                string path = dialog.FileName;
            }
 
        }

        private void button1_Click(object sender, EventArgs e)
        {
           printPrewiewDialog!.Document = PrintDocument1;
        }
    }
}
This is my code, I hope you'll see it better like this, sorry for the format.
And yes Excel is installed on the machine that I want to print from.
 
Last edited by a moderator:
You don't need a print document, print preview dialog, nor an open file dialog. You just need to use Process.Start() passing in a StartInfo with the "print" verb, assuming that Excel is installed on the machine that you are trying to print from.

 
For future reference, QUOTE tags are for quotes while CODE tags are for code. I have fixed the formatting of the code snippet you posted.
 
Thanks a lot ! I'm much closer to where I want to go.
Following your info I've written this code :

C#:
private void button1_Click(object sender, EventArgs e)
{
    string FileToPrint = "copy_of_etiquette.xlsx";
        ProcessStartInfo info = new ProcessStartInfo(FileToPrint);
    info.Verb = "Print";
    info.CreatNoWindow = true;
    info.WindowStyle = ProcessWindowStyle.Hidden;
    Process.Start(info);
}

The only problem is that when I run the code I have an error appearing saying that "An error as occured trying to start process "copy_of_etiquette.xlsx with working directory".

I'm not sure I've put the name of the file in the right place, could it be why I have this error ?

Thank you for your help
 
The process doesn't know where to find that file. Either specify full path to file, or set info.WorkingDirectory to the directory where the file is.
 
Thanks for your response !

My problem is that the file will not always be in the same directory on my computer. Thus I can't specify the full path to the file and I'm not sure that info.WorkingDirectory would work either.

Is there any solutions to that ?
 
What directory will it be in? Documents? Application folder? Application.StartupPath or Environment.GetFolderPath may help. In addition use Path.Combine to combine the folder path and file name for those.
 
It will be in a folder on my Local Disk.

I've used what you suggested but I have an error appearing for both solution saying that "Only assignment, call, increment, ..., can be used as a statement."

C#:
private void button1_Click(object sender, EventArgs e)
{
    string FileToPrint = "copy_of_etiquette.xlsx";
        ProcessStartInfo info = new ProcessStartInfo(FileToPrint);
    info.Verb = "Print";
    info.CreatNoWindow = true;
    info.WindowStyle = ProcessWindowStyle.Hidden;
    Application.StartupPath;
    Path.Combine;
    Process.Start(info);
}
 
It will be in a folder on my Local Disk.
What folder? You can't just specify the name of a file and expect it to be found in any folder on your hard drive. Where will the file be? If it could be any folder then you would have to have the suer specify that folder at run time. Otherwise, you're expecting magic.
 
As for Path.Combine, what are you expecting that code to do? Did you make any attempt to find out how the method works, like reading the documentation or even just paying attention to Intellisense when you typed it? The point is that you give it two or more partial paths and it combines them into one and returns that. Just writing the method name with no parentheses, no arguments and ignoring the return value can't do anything useful. Raed the documentation, which you can navigate directly to by clicking the name in code and pressing F1.
 

Latest posts

Back
Top Bottom