First time creating a form

This is a discussion on First time creating a form within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm using visual C++ to try and create a window which will display various bits and bobs from my ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    36

    First time creating a form

    Hello,

    I'm using visual C++ to try and create a window which will display various bits and bobs from my program. For example, I have a text box which I want to fill with a list of strings generated by my program.

    I have a function called addText() which I wrote in the form header file.

    Code:
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
    
    		void addText(){
    			 String ^ b = gcnew String("asd");
    			textBox1->AppendText(b);
    		}
    ...
    My question is, how can I call this from my main cpp file? (forget for now that it doesn't have any parameters).

    This is a section from my cpp file:

    Code:
    using namespace form;
    
    [STAThreadAttribute]
    int main(array<System::String ^> ^args)
    {
    	// Enabling Windows XP visual effects before any controls are created
    	Application::EnableVisualStyles();
    	Application::SetCompatibleTextRenderingDefault(false); 
    	// Create the main window and run it
    	Application::Run(gcnew Form1());
    	Form1 fas;
    	fas.addText();
    
    	return 0;
    }
    This currently will compile, but will not display the text correctly. My confusion lies in how to access the form which is running. Presumably, a form is created, runs and is displayed by this line:
    Application::Run(gcnew Form1());

    By creating 'fas' am I not just creating a new form, which will not be displayed? How should I access the member functions of the form that is being displayed?

    Cheers,

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Not that I speak Managed at all, but the obvious thing to me would be
    Code:
    int main(array<System::String ^> ^args)
    {
    	// Enabling Windows XP visual effects before any controls are created
    	Application::EnableVisualStyles();
    	Application::SetCompatibleTextRenderingDefault(false); 
    	// Create the main window and run it
    	Form1 fas;
    	fas.addText();
            Application::Run(fas);
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    36
    Thanks, I had tried that before but I got slightly confused by the error message it gives
    Code:
    1>.\form.cpp(18) : error C2665: 'System::Windows::Forms::Application::Run' : none of the 3 overloads could convert all the argument types
    1>        c:\windows\microsoft.net\framework\v2.0.50727\system.windows.forms.dll: could be 'void System::Windows::Forms::Application::Run(System::Windows::Forms::Form ^)'
    1>        c:\windows\microsoft.net\framework\v2.0.50727\system.windows.forms.dll: or       'void System::Windows::Forms::Application::Run(System::Windows::Forms::ApplicationContext ^)'
    1>        while trying to match the argument list '(form::Form1)'

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So the first choice there is a Form ^, and I think ^ means reference, right? Or is it pointer? If it's pointer, maybe pass the managed-equivalent-of-&fas in. If it's reference I don't know how to create a reference.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    36
    Thanks very much, this code now works. Also, congratulations on hitting 10k posts!

    Code:
    		Form1^ fas;
    		fas = gcnew Form1();
    		fas->addText();
    		Application::Run(fas);
    Surely there has to be an easier way than this to create a window and some output lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Representing floats with color?
    By DrSnuggles in forum C++ Programming
    Replies: 113
    Last Post: 12-30-2008, 08:11 AM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 03:44 AM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-02-2006, 11:46 PM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21