Israel

Israel

  • 633
  • 1.3k
  • 221k

Its cant print singly...

Sep 7 2016 7:27 AM
Hi!
 
With theses codes I can print all the textboxes content without problems.
Then what can I do if I would like to print for example a specific texbox or some textboxes (not all for instance).
 
See codes please:
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString(textBox1.Text, textBox1.Font, Brushes.Black, 50, 20);
e.Graphics.DrawString(textBox2.Text, textBox2.Font, Brushes.Black, 50, 50);
e.Graphics.PageUnit = GraphicsUnit.Inch;
}
private void button1_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
private void Printpreview_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
 
Thanx in advance,
Israel. 

Answers (4)

1
Tapan Patel

Tapan Patel

  • 282
  • 6.3k
  • 416.2k
Sep 7 2016 12:32 PM
i have updated my answer and please don't copy everything on the answer, please try to take a code out, understand then try it on your own. That way, you will learn more. Only thing, I have changed/added is inside the methods you already posted. So simply take those lines and try out.
1
Tapan Patel

Tapan Patel

  • 282
  • 6.3k
  • 416.2k
Sep 7 2016 11:22 AM
  1. List<TextBox>lstPrintableTextBoxes=null;//ClassVariable
  2. privatevoidForm1_Load(objectsender,EventArgse)
  3. {
  4. lstPrintableTextBoxes=newList<TextBox>();
  5. }
  6. private void printDocument1_PrintPage(objectsender,System.Drawing.Printing.PrintPageEventArgse)
  7. {
  8. int ycordinate=10;
  9. foreach(TextBoxtxtboxinlstPrintableTextBoxes)
  10. {
  11. e.Graphics.DrawString(txtbox.Text,txtbox.Font,Brushes.Black,50,ycordinate+20);
  12. }
  13. e.Graphics.PageUnit=GraphicsUnit.Inch;
  14. }
  15. privatevoidtextBox2_TextChanged(objectsender,EventArgse)
  16. {
  17. if(!lstPrintableTextBoxes.Contains(senderasTextBox))
  18. lstPrintableTextBoxes.Add(senderasTextBox);
  19. }
  20. privatevoidtextBox3_TextChanged(objectsender,EventArgse)
  21. {
  22. if(!lstPrintableTextBoxes.Contains(senderasTextBox))
  23. lstPrintableTextBoxes.Add(senderasTextBox);
  24. }
1
Tapan Patel

Tapan Patel

  • 282
  • 6.3k
  • 416.2k
Sep 7 2016 9:09 AM
You probably need to add the checkboxes before text boxes to keep track of the textbox that user wants to print. Once user clicks for print, your logic should only consider the textboxes where corresponding checkboxes are checked.. That is option 1.
 
Another option is to keep track of textboxes those have values (maintain a list of TextBox class and add textbox upon Text_Changed Event) and once user triggers an action (Via some button - say Print), just print the textboxes those are in the list.
 
 
Please mark it as an answer if it address the issue.
0
Israel

Israel

  • 633
  • 1.3k
  • 221k
Sep 7 2016 10:56 AM
Your theor is good but I need to see it in action with codes. That why I put my codes here to be helped.