Example analysis of C implementation of value transfer between forms

  • 2021-09-05 00:40:22
  • OfStack

In this paper, an example is given to analyze the method of C # to realize value transfer between forms. Share it for your reference, as follows:

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

Suppose we need to click a button in the main form FMMain to open the subform FMChild and pass a value to the subform FMChild. In general, the code for clicking the button to display the subform FMChild is:


FMChild fmChild = new FMChild();
fmChild.ShowDialog();
fmChild.Dispose();

If we need to pass the values of string and strValueA in the main form FMMain to FMChild, we first do the following for strValueA:


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 that displays the child form to one of the following two.

Method 1:


FMChild fmChild = new FMChild();
fmChild.ShowDialog(this);
fmChild.Dispose();

Method 2:


FMChild fmChild = new FMChild();
FMChild.Owner = this;
fmChild.ShowDialog();
fmChild.Dispose();

Then declare a main form FMMain object in the modified subform FMChild,

FMMain fmMain;

Add the following code to the string strValueA where you need to use the main form FMMain:
fmMain = (FMMain)this.Owner;

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

At this point, if you need to pass string strValueB from the child form FMChild to the main form FMMain, process string strValueB as well.


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

Then you are closing the subform code fmChild. Dispose (); After that, you can write 1 code to save or process strValueB of FMChild, such as:

string strTmp = fmChild.StrValueB;

Note that the child form FMChild displayed in the Visual C # Smart Device Pocket PC 2003 device application can only be used:


FMChild fmChild = new FMChild();
FMChild.Owner = this;
fmChild.ShowDialog();
fmChild.Dispose(); 

ShowDialog () is not overloaded in the device application of Visual C # Smart Device Pocket PC 2003.

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


Related articles: