|
Thank You so much, but the msdn tutorial its throw me exception "Object Reference not set to an instance of an object" and not printing... aand code project tutorial its not c++ Windows form application, i think its c# .net.
please help me if u can with your tutorial for c++ windows form...... thank you..
<pre lang="text">
modified 15-Apr-13 6:16am.
|
|
|
|
|
eakteam wrote: Object Reference not set to an instance of an object Means you are using an object reference that has not been initialised; use your debugger to find out where, what and why. As to the rest, whether it is C#, VB.NET or C++/CLR, the basics are the same and the code samples in MSDN are given in all languages.
Use the best guess
|
|
|
|
|
THIS IS MY CODE AND VISUAL STUDIO DEBUGER SHOW : A first chance exception of type 'System.NullReferenceException' occurred in System.Drawing.dll
#pragma once
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
namespace Printing2 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Drawing::Printing;
using namespace System::IO;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ Button1;
private: System::Windows::Forms::PrintDialog^ PrintDialog1;
private: System::Drawing::Printing::PrintDocument^ document;
protected:
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->Button1 = (gcnew System::Windows::Forms::Button());
this->PrintDialog1 = (gcnew System::Windows::Forms::PrintDialog());
this->document = (gcnew System::Drawing::Printing::PrintDocument());
this->SuspendLayout();
this->Button1->Location = System::Drawing::Point(94, 106);
this->Button1->Name = L"Button1";
this->Button1->Size = System::Drawing::Size(75, 23);
this->Button1->TabIndex = 0;
this->Button1->Text = L"button1";
this->Button1->UseVisualStyleBackColor = true;
this->Button1->Click += gcnew System::EventHandler(this, &Form1::Button1_Click);
this->PrintDialog1->UseEXDialog = true;
this->document->PrintPage += gcnew System::Drawing::Printing::PrintPageEventHandler(this, &Form1::document_PrintPage_1);
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->Button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
System::Drawing::Printing::PrintDocument ^ docToPrint;
private: System::Void Button1_Click(System::Object^ sender, System::EventArgs^ e) {
PrintDialog1->AllowSomePages = true;
PrintDialog1->ShowHelp = true;
PrintDialog1->Document = docToPrint;
if ( docToPrint == nullptr )
System::Windows::Forms::MessageBox::Show( "null" );
;
;
if ( PrintDialog1 == nullptr )
System::Windows::Forms::MessageBox::Show( "pnull" );
;
;
System::Windows::Forms::DialogResult result = PrintDialog1->ShowDialog();
System::Windows::Forms::MessageBox::Show(result.ToString());
;
;
try{
if ( result == System::Windows::Forms::DialogResult::OK )
{
docToPrint->Print();
}
}
catch (Exception ^T){
MessageBox::Show(T->Message);
}
}
private: System::Void document_PrintPage_1(System::Object^ sender, System::Drawing::Printing::PrintPageEventArgs^ e) {
String^ text = "In document_PrintPage method.";
System::Drawing::Font^ printFont = gcnew System::Drawing::Font( "Arial",35,System::Drawing::FontStyle::Regular );
e->Graphics->DrawString( text, printFont, System::Drawing::Brushes::Black, 10, 10 );
}
};
}
|
|
|
|
|
Sorry, but I cannot guess where the problem occurs. You need to step through the code to find out what reference has not been set. Also use the stacktrace to see where was the last actual step in your code.
Use the best guess
|
|
|
|
|
ok ok ... thank you so so much...
i will see with myself where i did wrong...
but anyway if u have time and without disturbing you, ill be very happy if u make a form for me just to print ot the prnter something when button clicked... it need just 5 minutes to do... anyway if u cant its oki.. Thank You for ur time.
|
|
|
|
|
We don't do this for people. Imagine how many requests we get from people wanting us to do this bit of work, or that bit of work for them. It's not just the writing of the code; you would need descriptions of what we added and why so that you can modify it for yourself.
|
|
|
|
|
ok i understand u very well... anyway my question take answer only here... so thank you so much. i will try harder with myself
|
|
|
|
|
We need serious help here! Appreciate any technical advise and sample codes.
We are facing the same problem. My programmers are struggling for weeks to try to understand how to format and print a report in MFC (C++)? It is so simple in JAVA. Why is the MFC environment technically so difficult to format and print a report, which is the most common things (output a formatted report) that an application wants to do. It seems that each and every developer have to construct their own codes to format & print a report in MFC C++.
Is there any relevant sample code (formatting and printing a report) that can be downloaded and modified?
|
|
|
|
|
kklim wrote: My programmers are struggling for weeks It took me less than a minute to find Printing with MFC Made Easy[^].
Veni, vidi, abiit domum
|
|
|
|
|
Richard,
Thanks for your pointer. Very helpful indeed!
Appreciate if we can also get your advise on how to print to a network (IP) printer? We are using HP laser printer, connected over a LAN.
|
|
|
|
|
It's exactly the same as printing to a local one.
Veni, vidi, abiit domum
|
|
|
|
|
Thanks again.
Somehow, we tried many times, but it does not work. Do we need to get the printer handler (or context of the printer) before we can output to it? If so, can what are the functions to do so?
|
|
|
|
|
All printing in Windows goes through the same route and print data is passed to the printer drivers to be rendered onto the physical device. It does not matter whether the printer is parallel, USB, network, WiFi or Bluetooth, they all work in the same way. If you have problems with a specific device then you need to diagnose why it is not working.
Veni, vidi, abiit domum
|
|
|
|
|
Thank you so much, Richard.
You have been most help in resolving our problems.
|
|
|
|
|
hi,
am new to metro style app design,,I want someone to help me on how i will connect to a access database and also the query to insert data to it from c++,,
|
|
|
|
|
|
conversion from tchar* to string^
|
|
|
|
|
You have already asked this in QA. Please do not cross post.
|
|
|
|
|
I already responded to this query at Conversion from TCHAR* to string in c++[^] in Q&A. Please don't repost in multiple forums. If you have more information to add then edit your original question.
Use the best guess
|
|
|
|
|
I have an old Borland C++ project that displayed a 16 x 16 group of labels displaying 0x00 to 0xFF for the purpose of diagnostics.
I am now using MicroSoft Visual Studio 2010 C++
I am having a problem creating an array of labels at runtime that the labels are accessable by other routines such as a timer interval routine
#pragma once
#include <stdio.h>
namespace SerialTester {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
int count;
int tindex;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
count = 0;
tindex = 0;
char buffer[200];
array< Label ^ > ^ labels;
labels = gcnew array<Label^>(256);
for (int index = 0; index < 256; ++index)
{
Label ^ label = gcnew Label;
label->Size = System::Drawing::Size(25, 25);
label->Name = "C" + index;
sprintf(buffer,"%2X",index);
label->Text = Convert::ToString( index, 16 );
String^ clistr = gcnew String(buffer);
Controls->Add(label);
labels[index] = label;
}
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Panel^ panel1;
private: System::Windows::Forms::Timer^ timer1;
private: System::ComponentModel::IContainer^ components;
protected:
private:
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->label1 = (gcnew System::Windows::Forms::Label());
this->panel1 = (gcnew System::Windows::Forms::Panel());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->SuspendLayout();
this->Controls->Add(this->panel1);
this->Controls->Add(this->label1);
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
count++;
label1->Text = Convert::ToString( count, 10 );
}
private: System::Void panel1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
labels[tindex++]->BackColor = System::Drawing::Color::SkyBlue;
tindex%=256;
}
};
}
So when I create the form I created an array of 256 Labels at runtime, labels[x] but in the timer1_Tick I can not access them because the array is not global.
QUESTION: How can I do this, should my creation be somewheres else so that it is global.
QUESTION: in my Borland project I simply used sprintf to format the caption of each label to 00 .. FF, but can not seem to format the text simply in upper case.
Any help from anyone would be greatly appriciated.
Or a link to a tutorial on this.
Thank you in advance
Douglas
|
|
|
|
|
1. Yes you need to move the array declaration out of the constructor. As it is now, the array is a local variable to the constructor and not accessible outside of the constructor. Read up on variable scope.
2. Use the string class ToUpper() method to make the string all uppercase.
|
|
|
|
|
Hello
Thank you for your reply
I had tried declaring the following after the global int count; up at the top of the example
array< Label ^ > ^ labels;
labels = gcnew array<Label^>(256);
But I got an error saying I could declare on an unmanaged heap
Any thoughts or is this the wrong place.
My help on visual c++ does not work as nice as the c# version does
Thank you
Douglas
|
|
|
|
|
Without seeing more of what you did, it's hard to tell but it should look something like:
private array< Label ^ > ^ labels;
Form1(void)
{
}
|
|
|
|
|
Awesome
Thank you for your time that works.
Douglas
|
|
|
|
|