vHost.exe stopped working WinForms c #

0

I have been working for a few days on a program that connects to a server to retrieve certain information and then prints it. The problem I have is that when I print on a standard printer or a PDF the program works well but this information must be printed with a POSLAB printer, which loads 80x420mm paper. When I choose this printer to print the information the program peta and the error message comes out (vHost.exe stopped working). I'm using Visual Studio 2013.

I enclose the code that does the printing management:

private void button1_Click(object sender, EventArgs e)
{
    if (this.uni != null)
    {
        clsVoucher objVoucher = new clsVoucher();
        objVoucher.Note = this.nota;
        objVoucher.ExpireMinutes = this.minuts;
        uni.CreateVoucher(ref objVoucher);
        enviaAImprimir(dispositiu, objVoucher.VoucherCode);
        //MessageBox.Show("Nova connexió, contrasenya: " + objVoucher.VoucherCode);
        this.Close();
    }
    else
        label3.BringToFront();
}
private void enviaAImprimir(Dispositiu dispositiu, string p)
{
    this.contrasenyaObtinguda = p;
    PrintDialog printDialog = new PrintDialog();
    PrintDocument printDocument = new PrintDocument();
    printDocument.PrinterSettings.PrinterName = "POSLAB TP805";
    printDocument.DefaultPageSettings.Landscape = true;
    printDocument.DefaultPageSettings.Margins.Top = 25;
    printDocument.DefaultPageSettings.Margins.Bottom = 25;
    printDocument.DefaultPageSettings.Margins.Right = 25;
    printDocument.DefaultPageSettings.Margins.Left = 25;
    //printDocument.DocumentName = "ticketPrint.txt";
    PaperSize midaRoll = new PaperSize("midaTiquets", 314, 1653);
    printDocument.DefaultPageSettings.PaperSize = midaRoll;
    if (!printDocument.PrinterSettings.IsValid)
    {
        string msg = String.Format("Can't find printer \"{0}\".", printDocument.PrinterSettings.PrinterName);
        MessageBox.Show(msg, "Print Error");
        return;
    }
    else
    {
        //printDialog.Document = printDocument; //add the document to the dialog box...        
        printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintingVoucherEvent);//add an event handler that will do the printing 
        //printDocument.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA3", 1180, 1660);
        printDocument.Print();
    }
}
public void PrintingVoucherEvent(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    StringFormat format = new StringFormat();
    format.LineAlignment = StringAlignment.Center;
    format.Alignment = StringAlignment.Center;
    Graphics graphic = e.Graphics;
    Font font = new Font("Courier New", 12); //must use a mono spaced font as the spaces need to line up
    float fontHeight = font.GetHeight();
    int startX = 5;
    int startY = 10;
    int offset = 40;
    //MOSTRA LES DUES CAPÇALERES I EL TITOL//
    graphic.DrawString(" Proves de funcionament", font, new SolidBrush(Color.Black), startX, startY);
    graphic.DrawString(dispositiu.header1, font, new SolidBrush(Color.Black), startX, startY + offset);
    offset = offset + (int)fontHeight; //make the spacing consistent
    graphic.DrawString(dispositiu.header2, font, new SolidBrush(Color.Black), startX, startY + offset);
    offset = offset + (int)fontHeight; //make the spacing consistent
    graphic.DrawString("----------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
    offset = offset + (int)fontHeight + 5; //make the spacing consistent
    //MOSTRA LA NOVA CONTRASENYA I ALTRA INFORMACIO//
    graphic.DrawString("Connexio al dispositiu: " + dispositiu.name, font, new SolidBrush(Color.Black), startX, startY + offset);
    offset = offset + (int)fontHeight; //make the spacing consistent
    graphic.DrawString("Contrasenya generada: " + this.contrasenyaObtinguda, font, new SolidBrush(Color.Black), startX, startY + offset);
    offset = offset + (int)fontHeight; //make the spacing consistent
    graphic.DrawString(dispositiu.footer1, font, new SolidBrush(Color.Black), startX, startY + offset);
    offset = offset + (int)fontHeight; //make the spacing consistent
    graphic.DrawString(dispositiu.footer2, font, new SolidBrush(Color.Black), startX, startY + offset);
    offset = offset + (int)fontHeight + 5; //make the spacing consistent  

    graphic.Dispose();
}

It is worth mentioning that I am not allowed to use frameworks like CrystalReports or others to print this information ...

    
asked by Josep Viñolas Solés 23.05.2017 в 12:34
source

0 answers