i want to print textbox contents at a coordinate in C# when user click a button on form.But i don't know code,please help me!
Thank you so much!

Dani AI

Generated

Short answer for : printing a TextBox to paper at chosen X/Y coordinates is done in a PrintDocument.PrintPage handler — draw the string with the PrintPageEventArgs.Graphics object (e.Graphics.DrawString). ’s label trick was for on‑screen placement only; was right to point toward PrintDocument as the right API to use. (learn.microsoft.com)

Important units and margins to understand before positioning: the printer Graphics uses GraphicsUnit.Display by default, which for printers maps to 1/100 inch (i.e. 100 units = 1.00"). So passing PointF(200,300) means roughly 2.00" from the left and 3.00" from the top. Also use e.MarginBounds / e.PageBounds and be aware of the printer’s physical non‑printable area (HardMarginX/HardMarginY); you can set PrintDocument.OriginAtMargins = true if you want 0,0 to mean the top‑left margin rather than the printable area origin. Account for these offsets when printing to preprinted forms. (stackoverflow.com)

Example pattern (minimal):

var pd = new PrintDocument();
pd.OriginAtMargins = true; // optional
pd.PrintPage += (s,e) =>
{
    string text = textBox1.Text;
    using (var font = new Font("Arial", 10))
    {
        // coordinates are in hundredths of an inch: inches * 100
        float x = 2.0f * 100f; // 2.00 inches
        float y = 3.0f * 100f; // 3.00 inches
        e.Graphics.DrawString(text, font, Brushes.Black, new PointF(x, y));
    }
};
pd.Print();

Troubleshooting tips: print to a PDF printer or use PrintPreview first, print a simple grid (or a ruler page) to discover the printer offsets, and tweak coordinates until fields line up; every printer can have different hard margins so always test on the target device (or use the printer’s HardMargin values). For preprinted forms a small calibration routine (print numbered grid, measure, convert mm/inches to hundredths) works well. (stackoverflow.com)

Recommended Answers

All 6 Replies

Hi thuyson, welcome at daniweb!
Do you mean you want to show the contents of a textbox in a different place on the same form, by clicking a button?

Create a label and in the properties make the Visible property false. In the event handler of the Button type

Label1.Visible = true;
Label1.Location = new Point(any number,any number);
Label1.Text = "Anything you want";

No,i want to print textbox's contents on paper of printer at different coordinates,not on form. Can u give me project's code.
Please help me!
Thanks so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.