I'm trying to move functions that I have created in my .h files into the .cpp files of my winForm application. I don't really know how to get this to work.

Example code in .h:

void loadCustomerDetails() {
          SQLiteConnection^ ObjConnection = gcnew SQLiteConnection("Data Source=SwiftService.db3;");
          SQLiteCommand^ ObjCommand = gcnew SQLiteCommand("SELECT * FROM Contact WHERE Type = 'Customer'", ObjConnection);
          ObjCommand->CommandType = CommandType::Text;	
          SQLiteDataAdapter^ ObjDataAdapter = gcnew SQLiteDataAdapter(ObjCommand);
          DataSet^ dataSet = gcnew DataSet();
          ObjDataAdapter->Fill(dataSet, "Contact");

          int posY;		//y position for the dynamically created panels
          posY = 10;
          DataTable ^table = dataSet->Tables["Contact"];
			
	for each (DataRow ^row in table->Rows)
	{
		Panel ^pnl = gcnew Panel();		//create a panel
		//set panel properties
		pnl->Location = Point(14, posY);
		pnl->Size = System::Drawing::Size(361, 101);
		pnl->BorderStyle= BorderStyle::FixedSingle;
		pnl->AutoScroll = true;
		pnl->Tag = row->default[5]->ToString();	//set Tag for panel so that the contact is unique


		//create labels and set properties 
		Label ^lblName = gcnew Label();
		lblName->AutoSize = true;
		lblName->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, 
		System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0)));
		lblName->Location = System::Drawing::Point(5, 7);
		lblName->Text = row->default[0]->ToString();		//get the item at the index 

				 		 
		posY += 110; //increment position y 
		//add event handler for labels
		lblName->Click += gcnew System::EventHandler(this, &Contacts::lbl_Click);
		lblTel->Click += gcnew System::EventHandler(this, &Contacts::lbl_Click);
		lblMobile->Click += gcnew System::EventHandler(this, &Contacts::lbl_Click);
		lblAddress->Click += gcnew System::EventHandler(this, &Contacts::lbl_Click);
		pnl->Click += gcnew System::EventHandler(this, &Contacts::pnl_Click);	//add event handler for panel
		//add controls
		pnl->Controls->Add(lblName);
		panel2->Controls->Add(pnl);
		 }
	ObjConnection->Close();	//close connection
		 }

Ok, so that's some code (I've deleted some stuff from it to make it shorter) that I want to move over to the cpp file.

I know that I need to create a prototype function in the .h file. But I get errors such as:
1>.\Contacts.cpp(61) : error C2653: 'Contacts' : is not a class or namespace name
for the line:

lblName->Click += gcnew System::EventHandler(this, &Contacts::lbl_Click);

Also how do I make changes to controls on the form via the cpp file, such as this line of code:

panel2->Controls->Add(pnl);

I'm not asking for anyone to translate the above code. I just need some basics on how to move over to the cpp files.

Thanks.

Recommended Answers

All 5 Replies

>>lblName->Click += gcnew System
You do NOT use += in that line, just =.

>>lblName->Click += gcnew System
You do NOT use += in that line, just =.

I don't think that's the problem. The code above works perfectly fine when it is in the Contacts.h file.

I'm just trying to move that whole function "loadCustomerDetails()" to the cpp file.

Is that function a member of a class? If yes, then in the *.cpp file you have to identify the namespace in which it appears as well as the class name, something like this:

#include "contacts.h"
namespace MyNamespace // or whatever its called in the *.h file
{
   void MyClass::loadCustomerDetails()
   {
     // blabla
   }
}
commented: AD, Its Managed VC +11

ah nvm, it worked, thanks :) I didn't really need to change much at all, just use the structure that u posted. Thanks alot once again.

>>lblName->Click += gcnew System
You do NOT use += in that line, just =.

No disrespect, AD, but it is correct. += is overloaded for events (which are really delegates in disguise) so that you can chain more than one event handler to a single event.

See http://www.functionx.com/vccli/general/events.htm (about 3/4 of the way down the page)

Add this to the ways that .NET has tried to corrupt our peaceful C++ lives.

commented: Ha ha! +11
commented: Thanks for the clarification +34
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.