SoFunction
Updated on 2025-03-09

C# implements the example analysis of inter-form value transmission

This article analyzes the method of C# to realize the value transmission between forms. Share it for your reference, as follows:

1. VS 2005 Visual C# Windows applications and Visual C# smart device Pocket PC 2003 device applications.

Suppose we need to open the subform FMChild when we click a button in the main form FMMain and pass a certain value to the subform FMChild. Generally speaking, when we click the button to display the code of the subform FMChild is:

FMChild fmChild = new FMChild();
();
();

If we need to pass the value of string strValueA in the main form FMMain to FMChild, then we first process strValueA as follows:

private string strValueA;
public string StrValueA
{
get { return strValueA; }
set { strValueA = value; }
}

Make it a property of the main form FMMain, and then modify the code to display the child form to one of the following two types.

Method 1:

FMChild fmChild = new FMChild();
(this);
();

Method 2:

FMChild fmChild = new FMChild();
 = this;
();
();

Then declare a main form FMMain object in the modified child form FMChild.

Copy the codeThe code is as follows:
FMMain fmMain;

Add the following code where you need to use string strValueA of the main form FMMain:
Copy the codeThe code is as follows:
fmMain = (FMMain);

In this way, you can get the value of strValueA in the main form FMMain.

At this time, if you need to pass the string strValueB in the child form FMChild to the main form FMMain, the string strValueB is also processed.

private string strValueB;
public string StrValueB
{
get { return strValueB; }
set { strValueB = value; }
}

Then after you close the child form code();, you can write some code to save or process FMChild's strValueB, for example:

Copy the codeThe code is as follows:
string strTmp = ;

Note that displaying subform FMChild in Visual C# Smart Device Pocket PC 2003 device application can only be used:
FMChild fmChild = new FMChild();
 = this;
();
(); 

ShowDialog() is not overloaded in device applications for Visual C# Smart Device Pocket PC 2003.

I hope this article will be helpful to everyone's C# programming.