Attempting to invoke a function causes an identifier not found error - c++-cli

I want to call a function in my source from a button-click handler, but I get an identifier not found error.
In my header I have this code:
private:
System::Void onepone_Click(System::Object^ sender, System::EventArgs^ e)
{
AddOnePlusOne();
}
I have in my source (among other stuff):
#include "MyForm.h"
void AddOnePlusOne()
{
int one = 1;
int two = one + one;
MessageBox::Show("one plus one equals " + two.ToString());
}
This function works fine if I put it into the header. But it feels strange to me to run all code from the header.
What am I doing wrong?

The problem is because decoration of function void AddOnePlusOne() is not available in header file MyForm.h. So when its call is encountered in function private: System::Void onepone_Click(System::Object^ sender, System::EventArgs^ e) then compiler is unable to identify it.
Provide declaration of function void AddOnePlusOne() in header MyForm.h and it will work fine.

Related

C++/CLI Windows Form change gif then to image

The code is simple. After a button press, change current image to the gif, then to the image after the gif.
The goal is have the gif play, then switch to the jpg. From the get go, this does not work. I've tried different timing techniques such as using Sleep(), for loops, and a mix of the two. This doesn't this doesn't give the gif time to play at all. The image remains as the primary, delays for however long and jumps to the jpg.
After a button press how can I change images, even gifs, in a specified order with a specified delay time between each?
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
pictureBox1->Load("gif");
pictureBox1->Load("jpg");
}
Thanks to some help from Michail who posted here, I was able to find a solution to the problem. The form had a predetermined jpg in pictureBox1. This code allows a gif to play for a determined amount of time in pictureBox1. Once that time has been met, it changes the image in pictureBox1 to a different jpg than before. This method could also be used as a timed loading screen if one finds a way to apply it as such. Hope this helps someone in the future.
#pragma endregion
public:
static int second = 0;
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
if(second != 42)
{
second++;
}
if(second >= 42)
{
pictureBox1->Load(".jpg");
timer1->Enabled=false;
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
pictureBox1->Load(".gif");
timer1->Enabled=true;
}
Try this
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
pictureBox1->Load("gif");
Timer->Enabled=true;
}
private: System::Void Timer_tick(System::Object^ sender, System::EventArgs^ e)
{
pictureBox1->Load("jpg");
}

Drawing on Form_Load C++/CLI

I'm drawing something on my panel with a method and when I call this method via a button it does draw, but when I call this method on Form_Load it doesn't draw anything. When I debug it, it actually goes through the code without any problem, but still it won't draw anything.
below you can see the Form_Load and the Button_Click events:
private: System::Void SelectElementForm_Load(System::Object^ sender, System::EventArgs^ e)
{
if (ElementList->Count > 0)
{
Index = 0;
DrawLinesInLayout();
}
}
and the button
private: System::Void btnLeft_Click(System::Object^ sender, System::EventArgs^ e)
{
if (ElementList->Count > 0)
{
if (Index + 1 > 1)
{
Index--;
DrawLinesInLayout();
}
else
{
Index = ElementList->Count - 1;
DrawLinesInLayout();
}
}
}
When I use the Paint-event it works for when my form pops up. But then I got the code twice in my program which is kinda pointless.
So my questions are:
Why isn't my Form_load using my method correctly and my button is?
Can I call the Paint event on a button click?
As Hans Passant mentioned, you can't draw on something that isn't there yet.
Solution: draw after it is created.
Invalidate(); doesn't work since there are variables that change in the drawing method (that's why there is an Indexchange in each call event).
Instead of using the Load event, use the Shown event. This will draw the lines on the form:
private: System::Void SelectElementForm_Shown(System::Object^ sender, System::EventArgs^ e)
{
if (ElementList->Count > 0)
{
Index = 0;
DrawLinesInLayout();
}
}

RoutedEventArgs and AllFramesEventArgs

I am trying to call a function whose parameters are object sender and RoutedEventsArg e. I need those parameters since I have created a button on the main window related to this function and when I click the button it links to my function.
protected void StartRecord(object sender,RoutedEventsArg e)
{
// some stuff that creates a button and then does stuff
}
In another function, I need to call the above function stated above, but this second function has a parameter of AllFramesReadyArg e, not RoutedEventsArg e. So how do i call out the first function
void sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
{
this.StartRecord(sender, e);
// does not work since parameter calls for RoutedEventArgs
}
Your StartRecord is not part of the Kinect Toolbox. You appear to have written it and given it those two arguments. It doesn't need them, nor do you necessarily need the function.
You also do not want to be calling StartRecord in AllFramesReady. The AllFramesReady callback is fired every time all the frames are ready for processing (hence the function name), which happens roughly 30 times a second. You only need to tell it to record once.
Per your other question, StartRecord is a callback to a button -- it shouldn't be called in code. It is called when the user hits the associated button.
Just looking at the Kinect Toolbox code and the callbacks, your code should look something like this:
KinectRecorder _recorder;
File _outStream;
bool _isRecording = false;
private void KinectSetup()
{
// set up the Kinect here
_recorder = new KinectRecorder(KinectRecordOptions.Skeleton, _outStream);
// some other stuff to setup
}
private void sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
{
SkeletonFrame skeleton;
if (_isRecording && skeleton != null)
{
_recorder.Record(skeleton);
}
}
public void StartRecord(object sender, RoutedEventsArg e)
{
_isRecording = !_isRecording;
}

Trying to get HorizontalOffset, always returns 0

I am writing an app with C#/xaml for Windows8 Metro.
I have a Scrollviewer and would like to get the horizontaloffset.
I tryed it with this:
private void ScrollViewer_ViewChanged_1(object sender, ScrollViewerViewChangedEventArgs e)
{
int i = Convert.ToInt32(GetValue(ScrollViewer.HorizontalOffsetProperty));
}
but i is always 0, althrough in the debugger it shows me an offset of 221.09 and i scrolled down!
Michael
If you scrolled down - your horizontal offset wouldn't change unless you scrolled horizontally.
Perhaps your event handler isn't on the ScrollViewer itself and if that's the case - you would need to call GetValue on the SV itself, e.g.
private void ScrollViewer_ViewChanged_1(object sender, ScrollViewerViewChangedEventArgs e)
{
int i = Convert.ToInt32(myScrollViewer.GetValue(ScrollViewer.HorizontalOffsetProperty));
}
or better yet just do this:
private void ScrollViewer_ViewChanged_1(object sender, ScrollViewerViewChangedEventArgs e)
{
int i = Convert.ToInt32(myScrollViewer.HorizontalOffset);
}

How to pass variable from one button click function to another?

Its kinda dummy question, but how to pass variable from one button click function to another? lets say I have
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
string x;
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
passWord(y);
}
and I want to pass variable x to function passWord(y) which launches when I click button3.
Any ideas?
well there are many ways to do it. one method is define a class member, such as
String ^ x = "some text";
then you can use this string in both button event handlers