Hi I have created an application of a textbox,
in which a user enters some text in the textbox and on clicking a 'Print' button
the text is directly stored into the memory and a print out is created.

Dani AI

Generated

A quick, practical summary that builds on 's hint plus 's follow-up about character limits.

A simple printing flow for a WinForms textbox: capture the textbox text into a string, create a PrintDocument, handle its PrintPage event to draw the text onto the page, and show a PrintDialog so the user can pick a printer. For short, single‑page text you can draw the whole string into e.MarginBounds; for longer text implement pagination by measuring how many characters fit and setting e.HasMorePages (see the Microsoft printing guidance below).

// capture text then print (simple, single-page example)
string toPrint = textBox1.Text;

var pd = new PrintDocument();
pd.PrintPage += (s, e) =>
{
    using (var font = new Font("Segoe UI", 10))
    {
        var layout = e.MarginBounds;
        e.Graphics.DrawString(toPrint, font, Brushes.Black, layout);
    }
};

using (var dlg = new PrintDialog { Document = pd })
{
    if (dlg.ShowDialog() == DialogResult.OK)
        pd.Print();
}

To limit characters in a TextBox use the MaxLength property (for multiline boxes it caps total characters). Example: textBox1.MaxLength = 200;. If you must handle pasted text or enforce more complex rules, validate in TextChanged and truncate or restore the cursor position.

If you need formatted output, use a RichTextBox printing helper or implement EM_FORMATRANGE to preserve RTF. For pagination, look up PrintDocument.PrintPage and examples of measuring strings and looping across pages.

References: and TextBoxBase.MaxLength property.

Recommended Answers

All 3 Replies

Are you asking how to print the text? Look in the the PrintDialog and richTextBox controls.

Are you asking how to print the text? Look in the the PrintDialog and richTextBox controls.

Yes!

Hi,how to limit numbers of characters in a textbox?

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.