Hi,

I'm using a Form generated application.

Its definition starts with:

public ref class Form1 : public System::Windows::Forms::Form
	{

	public:
		Form1(void)
		{

When I'm inside one of its method, I can use calls like:

this->StartPosition = FormStartPosition::Manual;

thanks to the this pointer.

How can I do this kind of calls from a routine that's not part of the class?

I obviously need a handle to the instance of the class, but I don't know how to get it.

The app starts with:

Application::Run(gcnew Form1());

but trying to change into:

Form1 ff = gcnew Form1();
Application::Run(ff);

gives a compiler error.

I'm new to C++, and there is probably something basic that I don't understand.

Thanks.

Form1 ff = gcnew Form1();

it probably want's you to declare 'ff as a 'Form1*', but i could be wrong since i've never seen the 'gcnew' operator before.

Form1 ff = gcnew Form1();

it probably want's you to declare 'ff as a 'Form1*', but i could be wrong since i've never seen the 'gcnew' operator before.

Probably Form^ ff , depending on which version of CLR.


>>I'm new to C++,
If you are trying to learn c++ than stay away from CLR and Forms because that is almost another language in itself.

Edited 6 Years Ago by Ancient Dragon: n/a

Probably Form^ ff , depending on which version of CLR.

That creates a generic Form form, but not an instance of Form1.

If you are trying to learn c++ than stay away from CLR and Forms because that is almost another language in itself.

I have a project that needs to be written in C++/CLI, and Forms seem easy to program, aside the problem mentionned above.

I meant to say Form1^ ff = gnew Form1; Current version of CLR does not allow you to use * instead of ^. And you can not use gnew without the ^.

I meant to say Form1^ ff = gnew Form1; Current version of CLR does not allow you to use * instead of ^. And you can not use gnew without the ^.

"Form1^ ff = gcnew Form1" works, thanks, but ff cannot be declared as global or static variable.

So, I'm still stuck about how to access it from a method that does not belong to the Form1 class.

Do you have freedom in your class design that you could instantiate it within the Form1.h code and pass your form1 in as a parameter to the constructor? (or better yet whatever individual UI components you needed instead of the whole thing)

Do you have freedom in your class design that you could instantiate it within the Form1.h code and pass your form1 in as a parameter to the constructor? (or better yet whatever individual UI components you needed instead of the whole thing)

My code was generated within Visual Studio 2008, and I don't think that I can modify it this way.

Besides, it seems that getting the main window handle must be a common need, and I'd like ot find out how it's done for my own education.

This article has been dead for over six months. Start a new discussion instead.