简体   繁体   中英

Printing Label in windows form application

I am developing a windows form application. In this application I am using one panel which contains many labels. They are having property of drag and drop. The user will drag and drop the labels where he want. Finally when he click on print button then it has to print.

But I am using list of labels.I am getting blank sheet.

 private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
        {
            List<Label> lbls = this.Controls.OfType<Label>().ToList();
            foreach (var lbl in lbls)
            {
                e.Graphics.DrawString(lbl.Text,
             new Font(comboBox1.SelectedItem.ToString(), Int32.Parse(comboBox2.SelectedItem.ToString()), FontStyle.Bold), Brushes.Black, lbl.Left, lbl.Top);
            }

I can see two problems you might have.

First, set a breakpoint and check that this.Controls.OfType<Label>().ToList() actually has any items in it. If it doesn't then you may have to change it to look for child controls of the panel, not of the form.

Secondly, the page you're printing has a different size to your form, and it has margins. PrintPageEventArgs.MarginBounds will tell you where and how big the margins are. PrintPageEventArgs.PageBounds will tell you how big the page is. Make sure you draw inside the margins. If something is drawn outside the margins it probably won't get printed. If it's outside of the page then it's 100% definitely not getting printed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM