Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Passing A Value Between Two Forms using a Property in CSharp
From Wiki
Passing a value between two forms is a common question for people just getting started, and one with many answers. One way to do this is by exposing a property on the form that you need to have receive the input, and passing a reference to this form in your second form's constructor. That method will be demonstrated here.
We will need two forms, one called "Sender" and one called "Receiver". Both should have a multi-line text box and a button.
For "Receiver" I named these controls: DisplayText and OpenSender. For "Sender" they are called: SendingText and SendToReceiver.
These names suck, but they help to keep it simple I guess.
The first thing we need to do is set up a property on Receiver that other classes will be able to access. This is pretty straight forward:
- public string TextDisplayed
- {
- get { return DisplayText.Text; }
- set { DisplayText.Text = value; }
- }
The next thing we need to do is figure out how to figure out how to access this property from our sending form. In this example we'll be doing it by passing a reference in the constructor, and storing this reference in a private variable on our form. So we add this variable to the form (it would be even better to use an interface but that is outside the scope of this discussion):
- private Receiver _sendTo;
And then we need a constructor that will take a Receiver as an argument. This one will also set the "SendingText" to whatever is currently displayed on the receiver.
- public Sender(Receiver sendTo)
- {
- InitializeComponent();
- _sendTo = sendTo;
- SendingText.Text = _sendTo.TextDisplayed;
- }
Now all we need to do is set up the handlers for our buttons. From the Receiver form we only need to open a new Sender (with a reference to the Receiver). So our Receiver button's click method ends up looking like this:
- private void OpenSender_Click(object sender, EventArgs e)
- {
- Sender s = new Sender(this);
- s.Show();
- }
And the Sender's like this:
- private void SendToReceiver_Click(object sender, EventArgs e)
- {
- _sendTo.TextDisplayed = SendingText.Text;
- }
And thats' all there is to it. While something like this might be OK to use once in a while, and on very simple projects, its' important to note that if you find yourself using this technique very often that you might be able to avoid some of the tangles it creates using other techniques that will be covered elsewhere.
This article is part of C Sharp Programming Hacks



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.