cannot convert from 'System::Windows::Forms::Form^ to 'MyApplication::Form1 ^'

Hi guys,

I am working on my application using with native c++. I am trying to create a code as public testmycode() function to linking up with the code that I have input them for each line in the form2_load event, so in that way it would make it shorter and it will be much easy for me to read the code in each line, but I can't be able to get rid of the error I am getting. No one would know how to help me and most of them don't know what I am trying to do, so I hope one of you will know what to do and how to help me out. I have stored the code in the form2.cpp.

The error I am getting is: error C2440: 'initializing' : cannot convert from 'System::Windows::Forms::Form ^' to 'MyApplication::Form1 ^'

The error are jumping on this line:

Form1^ form1 = form1->Owner;

Here's the current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#include "StdAfx.h"
#include "Form2.h"
#include "Form1.h"

using namespace System::Windows::Forms;

using namespace MyApplication;

Object ^testmycode()
{
	try
	{
		Form1^ form1 = form1->Owner;
		//Address of URL
		String ^URL = "http://www.mysite.com/myscript.php?user=" + form1->label1->Text + "&pass=" + form1->label2->Text;
		HttpWebRequest ^request = safe_cast<HttpWebRequest^>(WebRequest::Create(URL));
		HttpWebResponse ^response = safe_cast<HttpWebResponse^>(request->GetResponse());
		StreamReader ^reader = gcnew StreamReader(response->GetResponseStream());
		String ^str = reader->ReadToEnd();

  Form2^ form2 = gcnew Form2();
		if (str->Contains(form2->TextBox1->Text))
		{
			//Let Start the timer
			form2->timer1->Interval = 1000;
			form2->timer1->Enabled = true;
		}
		else
		{
			form2->Label2->Visible = true;
		}
	}
	catch (Exception ^ex)
	{

	}
return nullptr;
}

System::Void Form2::Form2_Load(System::Object^  sender, System::EventArgs^  e) 
{

    Form1 ^form1 = dynamic_cast<Form1 ^>(Owner);
	if (form1->Menuitems1->Checked == true)
	{
    testmycode();
  }
	if (form1->Menuitems2->Checked == true)
	{
    testmycode();
  }
	if (form1->Menuitems3->Checked == true)
	{
    testmycode();
  }
	if (form1->Menuitems4->Checked == true)
	{
    testmycode();
  }
	if (form1->Menuitems5->Checked == true)
	{
    testmycode();
  }
}


I have got an error: error C2065: 'Owner' : undeclared identifier.

I don't know what I suppose to do so.

Any help would be much appreicated.

Thanks in advance.
closed account (3hM2Nwbp)
What you're trying to do there is implicitly cast a base class to a derived class. I don't think that's what you want to do in this case.

If I might make a suggestion, you might want to pass a Form1 handle and a Form2 handle into the method. That way you'll have control over what's in the forms when you run your test.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "StdAfx.h"
#include "Form2.h"
#include "Form1.h"

using namespace System::Windows::Forms;

using namespace MyApplication;

Object ^testmycode(Form1^ form1, Form2^ form2)
{
	try
	{
		//Address of URL
		String ^URL = "http://www.mysite.com/myscript.php?user=" + form1->label1->Text + "&pass=" + form1->label2->Text;
		HttpWebRequest ^request = safe_cast<HttpWebRequest^>(WebRequest::Create(URL));
		HttpWebResponse ^response = safe_cast<HttpWebResponse^>(request->GetResponse());
		StreamReader ^reader = gcnew StreamReader(response->GetResponseStream());
		String ^str = reader->ReadToEnd();

		if (str->Contains(form2->TextBox1->Text))
		{
			//Let Start the timer
			form2->timer1->Interval = 1000;
			form2->timer1->Enabled = true;
		}
		else
		{
			form2->Label2->Visible = true;
		}
	}
	catch (Exception ^ex)
	{

	}
return nullptr;
}

System::Void Form2::Form2_OnTestButtonClick(System::Object^, System::EventArgs)
{
  // If you're sure that this->Owner is indeed a Form1
  testmycode(dynamic_cast<Form1^>(this->Owner), this);
}

/*System::Void Form2::Form2_Load(System::Object^  sender, System::EventArgs^  e) 
{

    Form1 ^form1 = dynamic_cast<Form1 ^>(Owner);
	if (form1->Menuitems1->Checked == true)
	{
    testmycode(form1, this);
  }
	if (form1->Menuitems2->Checked == true)
	{
    testmycode(form1, this);
  }
	if (form1->Menuitems3->Checked == true)
	{
    testmycode(form1, this);
  }
	if (form1->Menuitems4->Checked == true)
	{
    testmycode(form1, this);
  }
	if (form1->Menuitems5->Checked == true)
	{
    testmycode(form1, this);
  }
}*/


* You may want to move this topic to the Windows sub-forum.
Last edited on
closed account (z05DSL3A)
I am working on my application using with native c++.

That is C++/CLI not C++.
Topic archived. No new replies allowed.