7 Replies - 14328 Views - Last Post: 08 January 2012 - 03:08 PM

#1 Alyssa Saila  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 07-January 12

Calling A Simple Function From "button1_Click" With Visual C++

Posted 07 January 2012 - 01:46 PM


My question is about calling simple functions with the line beneath in Visual Studio (using Visual C++)
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {



I'm starting C++ playing around with simple console functions.

I realize the importance of learning the basics with basic console functions,
but I want to play around a bit in Visual Studio, and not with just plain console screens.

Using Visual C++, I can compile a simple working windows form application with a single button (for example a message box).

I simply draw a button on the form, double click in it, and add the following line to the button:

MessageBox::Show("Hello New World");



After I paste it in it looks like this of course (and it works tip top):

#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 MessageBox::Show("Hello New World");
			 }
	};
}



However, if I were to drop this code beneath in the same place (as I did with the message box code), I receive an error..

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ofstream outputFile;
    outputFile.open("C:\\Users\\Anonymous\\Desktop\\Write To Me.txt");
    
	// Write 3 great names to the file
	outputFile << "Bach\n";
	outputFile << "Beethoven\n";
	outputFile << "Mozart\n";
	// Close the file
	outputFile.close();
	return 0;
}



Note: this code works tip top by itself as a windows console simple app (I simply pasted it in using Dev-C++ and compiled it)

Forgive my ignorance, but every book I read keeps teaching me C++ console stuff.

My guess is the header files are supposed to be placed somewhere else?
Or perhaps this function all together should be placed elsewhere as the

Which is good! : ), but how do I apply these functions to buttons in a C++ windows form application?

Any help is super appreciated ;)


Is This A Good Question/Topic? 0
  • +

Replies To: Calling A Simple Function From "button1_Click" With Visual C++

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Calling A Simple Function From "button1_Click" With Visual C++

Posted 07 January 2012 - 01:50 PM

Moved to CLI C++
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,422
  • Joined: 25-December 09

Re: Calling A Simple Function From "button1_Click" With Visual C++

Posted 08 January 2012 - 07:43 AM

You should not try to mix managed C++ code with unmanaged C++ code. Windows forms uses managed code, the other code is unmanaged C++ (standard C++).

Jim
Was This Post Helpful? 0
  • +
  • -

#4 Alyssa Saila  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 07-January 12

Re: Calling A Simple Function From "button1_Click" With Visual C++

Posted 08 January 2012 - 09:54 AM

Thanks Jim.

After no replies yesterday I did some more reading (a lot more reading) and I found that I'm so far off the mark -it's no wonder I didn't receive a response. :)

I know that this a newbie frequently asked question, and I know it's mentioned in this forum a thousand times.
In fact, I thought this post beneath was pretty much money.. but due to my inexperience I still couldn't follow it well.
Call Function From CPP File


My problem is not at least knowing enough about what I'm asking to successfully search for it.

I started out with VB, so pasting function code into buttons was a lot easier.

However, with C++, I realize now that it's a matter of creating a separate .cpp file, and linking the functions in that file with header files?

It's some how a matter of linking or "pointing" everything together for the compiler?

In any case, I'll eventually have my ahha moment.

I can write and call functions at this point tip top (as console apps using Dev-C++), so now it's just a matter of figuring out how to execute them in Visual Studio.

My sole objective is to execute them (the functions) from a windows form in VS.

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,422
  • Joined: 25-December 09

Re: Calling A Simple Function From "button1_Click" With Visual C++

Posted 08 January 2012 - 02:00 PM

If you want to use Windows forms I suggest you stick with one of the managed versions, either CLI C++ or C#. Trying to access Windows forms from standard C++ is very difficult. See this link for a Wiki article on C++/CLI or this link from Microsoft. This topic may also answer some of your questions.



Jim
Was This Post Helpful? 0
  • +
  • -

#6 Alyssa Saila  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 07-January 12

Re: Calling A Simple Function From "button1_Click" With Visual C++

Posted 08 January 2012 - 02:34 PM

I realize how oblivious I sound asking this,
but I'm hoping you could pin point my confusion if I walk you through my thought process.

If CLI is the easiest way to achieve my goal
(which is adding C++ functions to button1_click)

I start VS and go to

New > Project > Visual C++

..and listed beneath that are options for:

ATL
CLR
General
MFC
Test
Win32


I read up on CLI, but why is it not mentioned in the list of options?

How do I start a fresh CLI project to create one?

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,422
  • Joined: 25-December 09

Re: Calling A Simple Function From "button1_Click" With Visual C++

Posted 08 January 2012 - 03:00 PM

I think you would use CLR, but I may be wrong. See this link MSDN C++/CLI.


Jim
Was This Post Helpful? 0
  • +
  • -

#8 Alyssa Saila  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 07-January 12

Re: Calling A Simple Function From "button1_Click" With Visual C++

Posted 08 January 2012 - 03:08 PM

Thanks again Jim.

But from New Project > Visual C++ > CLR

I get four options:

Windows Forms Control Library
Windows Forms Application
CLR Empty Project
CLR Console Application
Class Library

The most general reasonable option I'm assuming would be "Windows Forms Application"

..which is what I've been using.

Which as me even more confused.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1