It's a little bit difficult understanding exactly what you want, but I think it's something like this;
- When button 1 is pressed, store some value in a variable, somewhere.
- When button 2 is pressed, use the value stored by pressing button 1, somehow.
If that's the case something like this might help you get started;
#include "stdafx.h"
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
ref class MyForm : public Form {
private:
Button^ button1;
Button^ button2;
Label^ label;
int result;
public:
MyForm();
void OnClick1(System::Object ^sender, System::EventArgs ^e);
void OnClick2(System::Object ^sender, System::EventArgs ^e);
};
MyForm::MyForm() {
Text = "My Form";
button1 = gcnew Button();
button1->Text = "Button 1 (add 1)";
button1->SetBounds(10, 10, 100, 32);
button1->Click += gcnew System::EventHandler(this, &MyForm::OnClick1);
button2 = gcnew Button();
button2->Text = "Button 2 (add 5)";
button2->SetBounds(10, 50, 100, 32);
button2->Click += gcnew System::EventHandler(this, &MyForm::OnClick2);
label = gcnew Label();
label->Text = "Result area";
label->SetBounds(10, 90, 200, 32);
Controls->Add(button1);
Controls->Add(button2);
Controls->Add(label);
}
void MyForm::OnClick1(System::Object ^sender, System::EventArgs ^e) {
result += 1;
label->Text = String::Format("Pressed 1, result is {0}", result);
}
void MyForm::OnClick2(System::Object ^sender, System::EventArgs ^e) {
result += 5;
label->Text = String::Format("Pressed 2, result is {0}", result);
}
int main(array<System::String ^> ^args) {
MyForm^ form = gcnew MyForm();
Application::Run(form);
return 0;
}
The
MyForm
class holds variables for the buttons, but also a
result
variable that is shared by every method in the class. Thus, when button 1 or 2 modifies the value, the click-handler for the other button can always read it.
Hope this helps,
Fredrik