none
How to create a Dialog Box in Visual C++ CLR?

    Question

  • My main form (FORM1) has a button called "BUTTON1".When user clicks on this Button, a Dialog (showing more options) has to be launched.

    I have been surfing throught the Net, and i didnt found any full example about this(maybe it´s so simple,but i´m so newbie).
     I have found examples for Visual C++ 6 using Dialog Wizzard(?),some MFC(but im using CLR),indeed MSDN shows me old code(which works me partially).

    Well what i have done?
    I have done a Form1(the main app form),including there some controls with a Button called "BUTTON1"
    I have Added a Form2 to the project.I have added some buttons and textboxes in it.

    Then I have created a BUTTON1 Click Event in Form1:
    private: 
    System::Void button1_Click(System::Object * sender, System::EventArgs * e)
    {
    Form *dlg1 = new Form();
    dlg1->ShowDialog();
    }


    Which i have translated to CLI:
    private: 
    System::Void button1_Click(System::Object * sender, System::EventArgs * e)
    {
    Form ^dlg1 = gcnew Form();
    dlg1->ShowDialog();
    }
    (Maybe the translation is Wrong.Anyway i dont understand how my APP can "discover" i want to open Form2 since there isnt any mention to it)



    (Following the steps of MSDN: http://msdn.microsoft.com/en-us/library/aa984354(VS.71).aspx  )

    Thanks :D

    Monday, October 20, 2008 3:39 PM

Answers

  • The best approach would be to add a property/member function to Form2 to expose the current text in textBox1, and have Form1 call that property/member function after the call to ShowDialog. This way Form2 doesn't need to know anything about Form1 and you avoid circular dependencies.
    • Marked as answer by jack 321 Friday, October 24, 2008 2:45 AM
    Monday, October 20, 2008 4:28 PM

All replies

  • The C++/CLI code should be:

    void button1_Click(System::Object^ sender, System::EventArgs^ e) 
        Form2 dlg; 
        dlg.ShowDialog(); 

    The file containing this code should #include "Form2.h".


    Monday, October 20, 2008 4:04 PM
  • Thanks ildjarn , now my Dialog works perfect ;), just one thing more:

    I have created a String^ name variable before Designer in Form1(the main one).
    In Form2 (Dialog Box) there are a ButtonAccept and a textBox1( which accepts Input). When clicking Accept button, textBox1->Text must be saved in "name" variable. But seems "name" is out of the Scope("name" doesnt exist,compiler says).
    How i can deal with this?
    Thanks again



    Monday, October 20, 2008 4:23 PM
  • The best approach would be to add a property/member function to Form2 to expose the current text in textBox1, and have Form1 call that property/member function after the call to ShowDialog. This way Form2 doesn't need to know anything about Form1 and you avoid circular dependencies.
    • Marked as answer by jack 321 Friday, October 24, 2008 2:45 AM
    Monday, October 20, 2008 4:28 PM