TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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.
Reply
Answers (
4
)
1
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
282
6.3k
416.2k
Sep 7 2016 11:22 AM
List<TextBox>lstPrintableTextBoxes=
null
;
//ClassVariable
private
void
Form1_Load(
object
sender,EventArgse)
{
lstPrintableTextBoxes=
new
List<TextBox>();
}
private
void
printDocument1_PrintPage(
object
sender,System.Drawing.Printing.PrintPageEventArgse)
{
int
ycordinate
=10;
foreach
(TextBoxtxtbox
in
lstPrintableTextBoxes)
{
e.Graphics.DrawString(txtbox.Text,txtbox.Font,Brushes.Black,50,
ycordinate
+20);
}
e.Graphics.PageUnit=GraphicsUnit.Inch;
}
private
void
textBox2_TextChanged(
object
sender,EventArgse)
{
if
(!lstPrintableTextBoxes.Contains(sender
as
TextBox))
lstPrintableTextBoxes.Add(sender
as
TextBox);
}
private
void
textBox3_TextChanged(
object
sender,EventArgse)
{
if
(!lstPrintableTextBoxes.Contains(sender
as
TextBox))
lstPrintableTextBoxes.Add(sender
as
TextBox);
}
1
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
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.
display gridview on button click
How to convert html to pdf