VC++ 2010 - if button1 is clicked - visual-studio-2010

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)
{
// ...

Related

Visual Studio 2022 C++ How to make a calculator with 2 textboxes to accept ONLY numbers

textBox3 is just the result textBox , all i want is a validation check for textBox1 and textBox2 so the only input is numbers
#pragma endregion
private: System::Void Calculate(System::Object^ sender, System::EventArgs^ e) {
if (sender == button_plus) {
textBox3->Text = Convert::ToString(Convert::ToDouble(textBox1->Text) + Convert::ToDouble(textBox2->Text));
}
else if (sender == button_minus) {
textBox3->Text = Convert::ToString(Convert::ToDouble(textBox1->Text) - Convert::ToDouble(textBox2->Text));
}
else if (sender == button_multi) {
textBox3->Text = Convert::ToString(Convert::ToDouble(textBox1->Text) * Convert::ToDouble(textBox2->Text));
}
else if (sender == button_div) {
textBox3->Text = Convert::ToString(Convert::ToDouble(textBox1->Text) / Convert::ToDouble(textBox2->Text));
}
else if (sender == button_exit) {
this->Close();
}
}
I fianlly figured it out i just make a try, catch and solve this one
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
double num1;
try {
num1 = System::Double::Parse(this->textBox1->Text);
}
catch (System::Exception^ exception) {
num1 = 0;
}
this->textBox1->Text = num1.ToString();}
private: System::Void textBox2_TextChanged(System::Object^ sender, System::EventArgs^ e) {
double num2;
try {
num2 = System::Double::Parse(this->textBox2->Text);
}
catch (System::Exception^ exception) {
num2 = 0;
}
this->textBox2->Text = num2.ToString();}

C++ multiple actions at once

I'm making a program and I have a lot of labels that when you click on them they do the same thing and instead of writing them around 40 times is there a way to reduce it
private: System::Void lblHB15_Click(System::Object^ sender, System::EventArgs^ e) {
if (HBColumn1 == true) {
drop (1);
}
if (row1 == 6) {
btnRow1->Enabled = false;
HBColumn1 = false;
}
}

Function definition to use across project Visual C++

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.

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);

Wait for button press

Is it possible to make a function wait until a specific button is pressed?
For example I have a function called foo() and i want it to act like this:
void foo(int a, int b)
{
int result = a*b;
if(some_button got pressed)
MessageBox::Show(result.ToString());
}
Note that i don't want code like this inside the button:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
foo1(num1,num2);
}
void f1(int a, int b)
{
int result = a*b;
MessageBox::Show(result.ToString());
}

Resources