Function definition to use across project Visual C++ - visual-studio-2010

Trying to start up again with Visual C++, using 2010 Express edition.
Trying to figure out something.
If define a function in the Project.cpp file, why can't I use it in the Form1.h file, specifically the private: System::Void Form1_Load?
I get this error:
1>c:\users\boss\documents\visual studio 2010\projects\second\second\Form1.h(94): error C3861: 'Function': identifier not found
Is there any way to define a function so it can be used anywhere?
in Form1.h:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
this->txtMain->Text += FunctionX("Data");
this->txtMain->SelectionStart = this->txtMain->Text->Length;
}
in Project.cpp:
std::string FunctionX(std::string message) {
// other code here
return message;
}

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
extern std::string FunctionX(std::string message);
this->txtMain->Text += msclr::interop::marshal_as<System::String^>(FunctionX("Data"));
this->txtMain->SelectionStart = this->txtMain->Text->Length;
}
This works! Thanks for the tips.

Related

How to see textbox entry from one form in another form

I am trying to pass the textBox1 value from Form5 to Form1. However it does not seem to remember or see the value when it gets to Form1. I want paf_path to be what paf_path2 is.
System::Void Form5::button1_Click(System::Object^ sender, System::EventArgs^ e) {
System::String^ paf_path2 = textBox1->Text;
FolderBrowserDialog^ folderBrowserDialog1;
folderBrowserDialog1 = gcnew System::Windows::Forms::FolderBrowserDialog;
folderBrowserDialog1->Description = L"Select the directory of your MAF files ";
folderBrowserDialog1->ShowNewFolderButton = false;
// Show the FolderBrowserDialog.
System::Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog();
if (result == ::DialogResult::OK)
{
paf_path2 = folderBrowserDialog1->SelectedPath;
}
textBox1->Text = paf_path2;
}
System::Void Form5::button2_Click(System::Object^ sender, System::EventArgs^ e){
Form1 ^dos3 = gcnew Form1(this);
dos3->Show();
this->Hide();
}
System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e) {
System::String^ test = textBox1->Text;
if (test == "")
{
MessageBox::Show("Please enter partial address!");
}
else
{
Form5^doss = gcnew Form5(this);
System::String^ paf_path2 = doss->textBox1->Text;
char* paf_path = (char*) (void*) Marshal::StringToHGlobalAnsi(paf_path2);

VC++ 2010 - if button1 is clicked

I have a little problem with my script. I would like to check if the button1 was clicked in another event (pictureBox_Click). How could I do it?
It should works like this:
private: System::Void pictureBox_Click(System::Object^ sender, System::EventArgs^ e) {
if (button1 is clicked=true)
{
code;
code;
code;
}
if (button2 is clicked=true)
{
code;
code;
code;
}
}
I will be grateful for help.
You'd need to store when the button was clicked in a variable. Add an event handler for the button click events, and store the values.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
buttonClicked = true;
}
private: System::Void pictureBox_Click(System::Object^ sender, System::EventArgs^ e)
{
if (buttonClicked)
{
// ...

What causes ambiguous symbol errors? C++

I am trying to learn C++ by doing a small windows phone app. Currently I am just following a tutorial to get to grips with developing for the windows phone. However, I have encountered a ambiguous signal error when trying to build the code. I am used to the niceties associated with Java and am a bit lost as to what could be causing this error. The error dump I get is:
1>c:\program files (x86)\windows phone kits\8.0\include\wrl\event.h(740): error C2872: 'EventRegistrationToken' : ambiguous symbol
1> could be 'c:\program files (x86)\windows phone kits\8.0\include\eventtoken.h(51) : EventRegistrationToken'
1> or 'c:\program files (x86)\windows phone kits\8.0\windows metadata\windows.winmd : Windows::Foundation::EventRegistrationToken'
1> c:\program files (x86)\windows phone kits\8.0\include\wrl\event.h(1035) : see reference to class template instantiation 'Microsoft::WRL::EventSource<TDelegateInterface>' being compiled
1>c:\program files (x86)\windows phone kits\8.0\include\wrl\event.h(814): error C2872: 'EventRegistrationToken' : ambiguous symbol
1> could be 'c:\program files (x86)\windows phone kits\8.0\include\eventtoken.h(51) : EventRegistrationToken'
1> or 'c:\program files (x86)\windows phone kits\8.0\windows metadata\windows.winmd : Windows::Foundation::EventRegistrationToken'
The code is attached below - sorry for giving the whole file, but I literally dont know where to start. Any help would be greatly appreciated.
Thanks
#include "pch.h"
#include "WindowsPhoneGame.h"
#include "BasicTimer.h"
//#include <string.h>
#include <sstream>
//using namespace std;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::UI::Core;
using namespace Windows::System;
using namespace Windows::Foundation;
using namespace Windows::Graphics::Display;
using namespace concurrency;
WindowsPhoneGame::WindowsPhoneGame() :
m_windowClosed(false),
m_windowVisible(true)
{
}
void WindowsPhoneGame::Initialize(CoreApplicationView^ applicationView)
{
applicationView->Activated +=
ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &WindowsPhoneGame::OnActivated);
CoreApplication::Suspending +=
ref new EventHandler<SuspendingEventArgs^>(this, &WindowsPhoneGame::OnSuspending);
CoreApplication::Resuming +=
ref new EventHandler<Platform::Object^>(this, &WindowsPhoneGame::OnResuming);
m_renderer = ref new Renderer();
}
void WindowsPhoneGame::SetWindow(CoreWindow^ window)
{
window->VisibilityChanged +=
ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &WindowsPhoneGame::OnVisibilityChanged);
window->Closed +=
ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &WindowsPhoneGame::OnWindowClosed);
window->PointerPressed +=
ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &WindowsPhoneGame::OnPointerPressed);
window->PointerMoved +=
ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &WindowsPhoneGame::OnPointerMoved);
window->PointerReleased +=
ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &WindowsPhoneGame::OnPointerReleased);
m_renderer->Initialize(CoreWindow::GetForCurrentThread());
}
void WindowsPhoneGame::Load(Platform::String^ entryPoint)
{
}
void WindowsPhoneGame::Run()
{
BasicTimer^ timer = ref new BasicTimer();
while (!m_windowClosed)
{
if (m_windowVisible)
{
timer->Update();
CoreWindow::GetForCurrentThread()->Dispatcher- >ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
m_renderer->Update(timer->Total, timer->Delta);
m_renderer->Render();
m_renderer->Present(); // This call is synchronized to the display frame rate.
}
else
{
CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
}
}
}
void WindowsPhoneGame::Uninitialize()
{
}
void WindowsPhoneGame::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
{
m_windowVisible = args->Visible;
}
void WindowsPhoneGame::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
{
m_windowClosed = true;
}
void WindowsPhoneGame::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
{
ostringstream sstream;
sstream << "Pressed at: " << "X: " << args->CurrentPoint->Position.X << " Y: " << args->CurrentPoint->Position.Y << "\n";
string s = sstream.str();
OutputDebugStringA(s.c_str());
}
void WindowsPhoneGame::OnPointerMoved(CoreWindow^ sender, PointerEventArgs^ args)
{
ostringstream sstream;
sstream << "Moved at: " << "X: " << args->CurrentPoint->Position.X << " Y: " << args->CurrentPoint->Position.Y << "\n";
string s = sstream.str();
OutputDebugStringA(s.c_str());
}
void WindowsPhoneGame::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
{
ostringstream sstream;
sstream << "Released at: " << "X: " << args->CurrentPoint->Position.X << " Y: " << args->CurrentPoint->Position.Y << "\n";
string s = sstream.str();
OutputDebugStringA(s.c_str());
}
void WindowsPhoneGame::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
{
CoreWindow::GetForCurrentThread()->Activate();
}
void WindowsPhoneGame::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
{
// Save app state asynchronously after requesting a deferral. Holding a deferral
// indicates that the application is busy performing suspending operations. Be
// aware that a deferral may not be held indefinitely. After about five seconds,
// the app will be forced to exit.
SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
m_renderer->ReleaseResourcesForSuspending();
create_task([this, deferral]()
{
// Insert your code here.
deferral->Complete();
});
}
void WindowsPhoneGame::OnResuming(Platform::Object^ sender, Platform::Object^ args)
{
// Restore any data or state that was unloaded on suspend. By default, data
// and state are persisted when resuming from suspend. Note that this event
// does not occur if the app was previously terminated.
m_renderer->CreateWindowSizeDependentResources();
}
IFrameworkView^ Direct3DApplicationSource::CreateView()
{
return ref new WindowsPhoneGame();
}
[Platform::MTAThread]
int main(Platform::Array<Platform::String^>^)
{
auto direct3DApplicationSource = ref new Direct3DApplicationSource();
CoreApplication::Run(direct3DApplicationSource);
return 0;
}
You're using a lot of namespaces. It would appear that
EventRegistrationToken
Is defined in
Windows::Foundation; //windows.winmd
And again in eventtoken.h. Not sure which namespace this would apply to, could be global. Ditch the
using namespace Windows::Foundation;
and then you can access the respective implementations like this:
//eventtoken.h impl
EventRegistrationToken();
//the one in Foundation namespace:
Windows::Foundation::EventRegistrationToken();
Although it looks like you don't need this function, so it may not matter, this is just for example, and for how... since you need to remove this namespace, how you can now access the other members of this namespace.
I imagine you coud safely do this as well, though I don't necessarily recommend it:
using namespace Windows;
Foundation::EventRegistrationToken();
I had this same issue with just WP8 SDK projects.
Fix: Remove using Windows::Foundation from the .h file and use the full namespace for calling your object types.
Windows::Foundation::IAsyncOperation<String^> ^Blah();
instead of
IAsyncOperation<String^> ^CreateSampleData();

How to convert a string array element to string?

I have a string array
public: array<String ^> ^ sss;
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
array<String ^> ^ sss = gcnew array<String ^>(3);
sss[0]="asdasd";
sss[1]="s115ss";
sss[2]="s115ss";
}
I need to show the 1st element into a textbox.
I used
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
textBox2->Text = sss[0];
}
Vc++ gave System.NullReferenceException. Why? And how to fix it?
The error:
An unhandled exception of type 'System.NullReferenceException' occurred in test000.exe
Additional information: Object reference not set to an instance of an object.
Your code shouldn't compile, unless you also have a field called sss. If that's the case, you want to set the value of that field in your constructor, not of some unrelated local variable with the same name:
array<String ^> ^ sss;
public:
Form1(void)
{
InitializeComponent();
sss = gcnew array<String ^>(3);
sss[0]="asdasd";
sss[1]="s115ss";
sss[2]="s115ss";
}

How to hand a file path (text box value) to a file reading function in c++?

I created a file chooser for windows it returns me a chosen file path. I want to read the given file but I do not know how to pass the file path to the right function.
File Form1.h I have a button action and inside of it I can get openFileDialog1->FileName but I do not know how to pass this variable to a readFile() function inside of main.cpp file.
I created a method to return the path:
System::String^ filePath;
....
private: System::String^ getPath() { return filePath; }
Here is the file-pickers code:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Stream^ myStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->InitialDirectory = "c:\\";
openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 2;
openFileDialog1->RestoreDirectory = true;
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK ){
if ( (myStream = openFileDialog1->OpenFile()) != nullptr ){
// Insert code to read the stream here.
textBox1->Text = openFileDialog1->FileName; //text box displays the chosen path
myStream->Close();
}
}
}
The variable is set on button click:
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
filePath = textBox1->Text;
}
How to call the return methods in my main.cpp:
#include "stdafx.h"
#include "Form1.h"
using namespace main;
using namespace std;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
System::String^ p1 = /*Something missing her?*/getPath1(); //I am guessing it should look like this...
return 0;
}
Put the file name in a public property (public field, if it's what you prefer) in the Form1 class (or make your getPath() method public) then:
Form1^ form = gcnew Form1();
Application::Run(form);
String^ p1 = form->FileName;

Resources