tags:

views:

182

answers:

3

i need some ideas

+3  A: 

Basically the same way you'd do anything with another object. You need to have a reference to the other form, and if it's a different type it has to expose the text box you're interested in as a property, or have a method to set the text. So for example, you might have:

public class FirstForm : Form
{
    private TextBox nameInput;
    public TextBox NameInput { get { return nameInput; } }

    ...
}

public class SecondForm : Form
{
    private TextBox otherNameInput;
    private FirstForm firstForm;

    public void CopyValue()
    {
        firstForm.NameInput.Text = otherNameInput.Text;
    }
}

Or putting the textbox responsibility in the first form:

public class FirstForm : Form
{
    private TextBox nameInput;
    public string Name
    { 
        get { return nameInput.Text; } 
        set { nameInput.Text = value; }
    }

    ...
}

public class SecondForm : Form
{
    private TextBox otherNameInput;
    private FirstForm firstForm;

    public void CopyValue()
    {
        firstForm.Name = otherNameInput.Text;
    }
}

There are various other ways to skin the cat, but those are the most common. How you get the reference to the FirstForm into the SecondForm will vary - it may be passed into the constructor for SecondForm, or it could be created by SecondForm itself. That will depend on the rest of your UI.

Note that this assumes the two forms use the same UI thread. It's possible (but relatively uncommon) to have different UI threads for different windows, in which case you'd need to use Control.Invoke/BeginInvoke.

Jon Skeet
this code displays textboxvalue in header of second form
ush
Where it's displayed is fairly irrelevant. Which bit of my answer are you finding hard to implement?
Jon Skeet
Just to confirm best practice here - if I have several POCO's (can I say POCO? :) that need to update a textbox in a form (e.g. progress results), are we saying that I need to pass a reference of the form to every class that I want to talk back to the form?
Vidar
@Vidar: Either the form, or the textbox itself. Or potentially the POCO could expose an event, and the form could subscribe to that event and update the textbox itself.
Jon Skeet
@Jon Skeet: I have gone for the second method for subscribing to events - as I think it should be a bit more elegant - however I think it starts getting messy when you have an object which is publishing an event that makes an instance of another object which also publishes an event. You end up chaining events, am I going wrong here?, its starting to not feel right.
Vidar
@Vidar: Doesn't sound particularly wrong. If you want a "message" to reach from one end of your code to another, you've either got to have links in every step of the chain, or global state (urgh!). One alternative to a fullblown event is to just pass a delegate into the constructor - a sort of "single-handler event" if you will.
Jon Skeet
@Jon Skeet: Thanks for your time Jon, very nice to clear my thinking up there. Cheers
Vidar
A: 

Change the class and override the constructor of the form to pass in the data that you want. In the constructor store the varialble being passed in , into a member variable

RC1140
A: 

You should take Jon's advice. The other way might be something as dirty as this:

// Bad practice
foreach (var child in theOtherForm.Controls){
    if(child.Name == '_otherControlName')
    {
        (child as TextBox).Text = _thisTextBox.text;
    }
}

You may need to check some types and some panel's child controls as well.

zzandy