Unable to access Form1 on button click of form2 - winforms

My application is having 2 forms in windows form application (vc++). My first form is Form1.h and second is form2.h. What I am trying to do is calling form2 on the click of button1 (which is on the Form1.h). For calling form2 from Form1 I have used this code
#include “Form1.h”
......
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
form2^ fm=gcnew form2;
fm->Visible=true;
this->Visible=false;
}
This is working fine. But I am unable to call the Form1 on the click of the button (Which is on form2). As soon as I m including Form1.h in the form2.h file, It is giving the following error:-
>d:\vs\testformapp\testformapp\Form1.h(81): error C2065: 'form2' : undeclared identifier
1>d:\vs\testformapp\testformapp\Form1.h(81): error C2065: 'fm' : undeclared identifier
1>d:\vs\testformapp\testformapp\Form1.h(81): error C2061: syntax error : identifier 'form2'
1>d:\vs\testformapp\testformapp\Form1.h(82): error C2065: 'fm' : undeclared identifier
1>d:\vs\testformapp\testformapp\Form1.h(82): error C2227: left of '->Visible' must point to class/struct/union/generic type
And I just want Form1->Visible=true; on the button click of the form2. I don’t want to Initialize Form1 on the button click of form2 because it will initialize all parameters of form1 and my application requirement is not like that.
Can anybody please tell me any solution for this problem.
Any help will be appreciated.

Related

Xtrareport Databinding format string , "Object reference not set to an instance of an object" devexpress 12.2.7

I want to display thousand separator in Xtrareport, this code is work perfectly in DevXpress ver 14, but in ver 12.2.7, it's make an error:
An unhandled exception of type 'System.NullReferenceException' occurred in Kho3.exe
Additional information: Object reference not set to an instance of an object.
My code:
private void xxSL_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
xxSL.DataBindings["Text"].FormatString = "{0:N0}";
}
How to fix it?
DevExpress 12.2.x XRLable just don't have the DataBindings["Text"]. So I can't use it, for anyone get into this problem like me

Change label text from different header file, Visual C++ 2010?

I am using Visual C++ 2010 Express. I have a form (Form1.h) that contains a button (btn1) and a label (label1).
When I click the button, I would like to call a function from a different header file (testing.h) that will then proceed to change the text in the label.
What I have is something like this...
Form1.h
#include "testing.h"
... standard form code generated by Visual Studio
private: System::Windows::Forms::Label^ label1;
...
private: System::Void btn1_Click(System::Object^ sender, System::EventArgs^ e) {
testfunc1();
}
};
Where testing.h is something like...
#ifndef _TESTING_FUNCS
#define _TESTING_FUNCS
void testfunc1(){
label1->Text = "Text has been changed from outside.";
}
#endif
When I try to compile and run it, I get errors saying that 'label1' is an undeclared identifier (within testing.h), and an error referring to the "left of '->Text' must point to class/struct/..."
I am new to C++ and usually use Java, so there are a few new things for me here. To me, there are two obvious options:
1) Pass the label to the function as an argument
2) Somehow access the label from the testing.h header file, sans reference
But I'm not really sure how to do either.
The label is a private variable of a class and just like in Java is not accessible from the outside, especially not in static context. You can pass the label, or you can create an accessor function in your Form and pass the whole form.
Example for passing the label:
void testfunc1(System::Windows::Forms::Label^ someLabel)
{
someLabel->Text = "Text has been changed from outside.";
}
Calling it:
System::Void btn1_Click(System::Object^ sender, System::EventArgs^ e)
{
testfunc1(label1);
}

WinForms Designer Exception

A WinForms form that includes a UserControl throws an exception when I attempt to display it in design mode, but runs properly when the program is ran or debugged.
The designer says:
The variable 'fpInfoA' is either undeclared or was never assigned.
ResearchTool fMain.Designer.cs Line:282 Column:1
Call Stack
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)
However, it looks like the variable is assigned as I would expect in InitializeComponent
private void InitializeComponent()
{
// ... (Order of statements is same as in actual code) ...
this.tpFpA = new System.Windows.Forms.TabPage();
this.fpInfoA = new ResearchTool.FPInfo();
// ...
this.tpFpA.Controls.Add(this.fpInfoA); // THIS LINE BLOWS UP IN DESIGN MODE
}
Thoughts on how to track down this issue? For example, is there a way to debug initialization of the designer?
One workaround in case you can't fix the issue, would be to surround the offending bits of code with checks for DesignMode.
As in:
private void InitializeComponent()
{
...
if(!DesignMode)
{
this.fpInfoA = new ResearchTool.FPInfo();
}
...
}
This can also speed it up a little bit if it's doing things that aren't needed in design mode and that are quite slow, like connecting to databases or similar.
As Hans Olsson said, this potentially could be resolved by checking for design-mode and disabling offending logic.
This error will also trigger if there is any issue with the constructor of your UserControl. If there is an exception caused when the designer instantiates your UserControl, the designer will fail. In my case, the failure resulted in the same "[...] is either undeclared or was never assigned" error.
For example, see the following user control:
public class MyUserControl : UserControl {
public MyUserControl()
{
InitializeComponent();
throw new Exception(); //Causes a designer error.
}
}
Now, when observing the designer for a form that contains this MyUserControl, we will see something similar to the following:
I cannot say if the designer is like this for previous versions of Visual Studio; but as for Visual Studio 2017, you can clearly see what happened.
The designer failed because a System.Exception was thrown. As a result, the variable [REDACTED] was thought to be undeclared or never assigned when in fact the auto-generated designer code was correct. The issue was with the MyUserControl's constructor.
Now, if you need to put logic that depends on external services/resources inside the control's constructor, you need to indicate that it should only occur during runtime. Alternatively, you can provide mock-up resources for design-time.
To do this, you can use the LicenseManager and check its current UsageMode.
The modified code below only throws the exception in runtime now, and the designer doesn't have the error anymore.
public class MyUserControl : UserControl {
public MyUserControl()
{
InitializeComponent();
if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
{
throw new Exception(); //No longer fails in design-time.
}
}
}
You will find the information on how to trace design time code execution at:
What information do you need to fix a problem, which occurs with your products at design time?

Problem in VisualStudio2010 c++ class of WinForms

There is a line #include "Algorithm.h" in Form1.h
Algorithm.h and Form1.h are the automatic codes of winforms in VisualStudio2010, and they are in the same directory.
I added only 2 lines; the "include" and the instruction in line 40 (this line is in the definition of the class Form1 - "public ref class Form1 : public System::Windows::Forms::Form"
what is the problem??
thanks!

Open New Form. error c3767 candidate function(s) not accessible

I want to open one form from another.
I'm having no troubles doing this with blank project. Start new, make 2 forms, put button on first, use this code
Form2 ^ form = gcnew Form2;
form->ShowDialog();
Also adding the include file at the top...
I'm getting this error
error c3767 candidate function(s) not accessible
I've gone through my project and compared it to the really basic one I tried as an example...I've been searching google for hours and trying all sorts of different things, but none of the other peoples problems are related to opening another form...
If anyone could shed any light on this for me, it would be awesome
Thanks
Simon
It looks like the form's constructor is not public. This example form generates that exact same error message:
public ref class Form2 : public System::Windows::Forms::Form
{
//public: // <=== Remove this comment to fix C3767
Form2(void)
{
InitializeComponent();
}
// etc...
};

Resources