Call a procedure that is on another form - C++ winforms

José Carlos 886 Reputation points
2022-12-02T10:24:16.243+00:00

Hi friends,

I have 2 forms (Form1 and Form2). In Form1 I have a routine that accesses a bank and fills 2 textBoxes. How can I click a button on Form2 and execute this procedure that is on Form1?

I tried everything within my limits, but I couldn’t. And I don’t even know if it’s possible in C++

Thank you

Taveira

Developer technologies C++
0 comments No comments
{count} votes

Accepted answer
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2022-12-05T07:46:35.41+00:00

    Hi @Anonymous ,

    According to your description, I think you are using C++/CLI, so I spent some time to realize your needs.

    In the following code, MyForm is the main form and MyForm1 is the secondary form.

    MyForm.h

    public:  
    MyForm(void)  
    {  
    InitializeComponent();  
    //  
    //TODO: Add the constructor code here  
    //  
    }  
      
                   public:  
                    property System::Windows::Forms::TextBox ^  
                        txt1 {  
                          System::Windows::Forms::TextBox ^ get() {  
                            return textBox1;  
                          } void set(System::Windows::Forms::TextBox ^ value) {  
                            textBox1 = value;  
                          }  
                        }  
      
                        public  
                        : property System::Windows::Forms::TextBox ^  
                          txt2 {  
                            System::Windows::Forms::TextBox ^ get() {  
                              return textBox2;  
                            } void set(System::Windows::Forms::TextBox ^ value) {  
                              textBox2 = value;  
                            }  
                          }  
    

    In this example, I added a button to the secondary form to pass values to the TextBox.

    MyForm1.h

         private:  
                System::Void button1_Click(System::Object ^ sender,  
                                           System::EventArgs ^ e) {  
            MyForm ^ form1 = (MyForm^)Application::OpenForms["MyForm"];  
             
            form1->txt1->Text = "test1";  
            form1->txt2->Text = "test2";  
    

    MyForm.cpp

    #include "MyForm.h"  
    #include "MyForm1.h"  
    using namespace Project50;  
    using namespace System;  
    using namespace System::Windows::Forms;  
      
    [STAThreadAttribute]  
      
        int main(array<System::String ^> ^ args)  
      
    {  
      Application::EnableVisualStyles();  
      Application::SetCompatibleTextRenderingDefault(false);  
      MyForm form;  
      MyForm1 form2;  
      form2.Show();  
      Application::Run(% form);  
      return 0;  
    }  
    

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.