Windows Forms - printing the textBox's content

Hi Guys.
I'm writting a simple Windows Forms aplication and I have a 'textBox1' with some simple text. The task is to create a menu button which, when clicked, saves all the 'textBox1' content to a new file USING A DIALOG. I suppose it should be done using 'PrintDialog', but how to do it ?
I'm a beginner to Windows Forms :), so thanks for your patience.
Last edited on
You create a method for the button, then in that method you use a CFileDialog
http://msdn.microsoft.com/en-US/library/dk77e5e7(v=vs.80).aspx to open/create a new file to save the content of your text box.
You open the file with either ofstream(std) or CFile, and use the apropriate methots for adding the text from the textBox to the file.
If you use a multiline textbox(Assuming its CEdit) you use a for loop based on CEdit::GetLineCount - returns the number of lines
and CEdit::GetLine(lineNumber);

I assumed you are using MFC
Last edited on
No, I'm not using MFC... I only use Windows Forms Applications in Visual Studio 2010. To be honest, I don't even know what MFC is. I only need to make this printing of the textBox1 and that will be the end of my Windows Forms adventure, I'm planning to start programming using Qt.
But I can't deal with this printing, nothing in the web about this...
GetSaveFileName will handle file saving path choosing for you, showing the default "Save As" window, and it uses plain C (As you requested):

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646928(v=vs.85).aspx

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
OPENFILENAME ofn;
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = (Your Dialog HWND, or 0);
ofn.hInstance = (HINSTANCE)GetModuleHandle(0);
ofn.lpstrFilter = TEXT( "*.mp3\0MP3 File\0*\0All Files\0*.mp3;*.wma;*.wav\0MP3, WMA or WAV\0\0" );
// An example of file filtering:
// It works like this:

/*
First Entry: Extension Filtering
(Null Character)
Second Entry: Description
(Null Character)
(Another Entry here, or another Null Character)

So:

[Extension];[Another Extension]\0[Description]\0[Extension]\0[Description]\0\0
*/
LPTSTR Filename[MAX_PATH];
ofn.lpstrFile = Filename;
ofn.lpstrFile[0] = 0;
ofn.nMaxFile = sizeof(Filename) / sizeof(*Filename);
ofn.lpstrTitl = TEXT( "Test Save File Example" );
ofn.Flags = OFN_ENABLESIZING|OFN_EXPLORER|OFN_PATHMUSTEXIST;

if(GetSaveFileName(&ofn) != 0)
{
    // Filename now contains the chosen filename
}
Last edited on
Ok, I've forgotten to mention it visibly - I'm using C++. I thought, that if this page names cplusplus it's clear, that I'm making code with C++ :P.
So, as I consider, plain C is not appropriate choice here...
I'm also using some Open/Save file methods in my app, but I'm using a OpenFileDialog and SaveFileDialog for this and it works ok (I mean saving textBox1 content do txt file and load txt file content and place it into textBox1).
Last edited on
Oh, so you're using .NET.
Sorry, cannot help much with that, i only have experience with the C winapi.

Anyways you can use C things in C++, too.
Last edited on
.NET, exactly. Yes, I know, but it's an university task and I have to use C++ only.
I've found something like that (it's in Polish, doesn't matter, the code is most important) -> http://patryknet.blogspot.com/2009/11/drukowanie.html but, as I suppose that's for C#, not C++.
In this case you do kinda the same.
You create a method for the button, when it's pressed.
In that method you call ToString for the textbox1, it should return the text from the textbox.
Then you use the file class
http://msdn.microsoft.com/en-us/library/system.io.file_methods(v=vs.71).aspx and you use the methods from overthere to append your text to the file.
An exemple of adding an function call for a button is here
http://www.functionx.com/vcnet/controls/button.htm
Last edited on
I've written such a code -> http://pastebin.com/X7vaKNpv
The idea was to copy the textBox1's content to the String^ and then send it to the printer.
Using the code above, after clicking button6 printer choosing window opens normally, but, after choosing it the MessageBox with "OK" appears and then the application crashes.
Try and put a break point at the beginning of the button6_Click and document_PrintPage functions and us step into until the program crashes. Note at what line it crashes.
Also if it's not to much you could upload the entire project someware and put the link here and i will try and make a debug when i get home(hope i get the time :)) )
I've just found my mistake - I didn't put anything to docToPrint anywhere, so I've added two lines of code:
1
2
3
using namespace System::Drawing::Printing;
 
PrintDocument^ docToPrint = gcnew PrintDocument();


Now it works without crashes, but printer prints only a plain page. I suppose that these two lines:
1
2
3
String^ text = textBox1->Text;
(.....)
e->Graphics->DrawString(text, printFont, System::Drawing::Brushes::Black, 10, 10 );

should make the printer prints content of textBox1, am I right ?
Last edited on
You are right, the 2 lines of code are responsible for printing.In this type of situations is best to try and check if your string really have the values you want. Try and output the value of text either in a label or in a message box.
Also i thing it would be better to use the ToString method, rather then Text(might be wrong though, i think i am not checking the right class :)) ).
Yes, but even if i put there "example" instead of text, the printer prints only plain page, no "example" on it.

EDIT:
I mean that even:
e->Graphics->DrawString("example", printFont, System::Drawing::Brushes::Black, 10, 10 );
doesn't work, the printer works plain page.
Last edited on
I've got a solution. Me and MSDN users managed to solve the problem in this topic ->http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/3f7e78e1-15ee-4858-82d2-6bceeb18c818

Thank you all! :)
Last edited on
Topic archived. No new replies allowed.