tags:

views:

61

answers:

3

Hi, I have a form in c# with a multiline textInput which content I need to be updated form an object outside the form. How can I achieve this ? Using events ? Passing the textInput to the object's contructor ?

Thanks in advance.

+1  A: 

Hi, i would not pass the text to the object. If you just need it as initialization value, passing the text to the constructor of the form would be fine. But not other way round.

Very simple solution: Give your form a public SetTextValue(string Text) method, that sets the text.

Events will work also, but seems a bit overpowered for such a simple problem.

Marks
Thanks for your reply ... and how do I get a reference to the "Form1" object from the external object ?
xain
You could create a presenter (MVP) to look after updating the form. The presenter takes an instance of the form and your "external object" (the model) in it's constructor. The presenter subscribes to some event(s) on the model. When the appropriate event is fired, then the presenter can update the form.
Peter Kelly
+1  A: 

There are a lot of ways to accomplish this depending on the specifics of what you're working on.

Update the text field from within the form?

txtField.Text = someObject.SomeProperty;

Set the value in the form's constructor?

SomeFormClass form1 = new SomeFormClass(aString);
form1.Show();

Call a method on the form from an external object?

public void SetText(string text) { txtField.Text = text; }

form1.SetText(aString);

Use data binding?

txtField.DataBindings.Add(new Binding("Text", someObject, "SomeProperty");

It's hard to answer without knowing more details.

Phil Lamb
Thanks for your reply, actually the DataBindigs approach is what I need. It's not working though, where should the binding go ? I tried putting it in the Form's contructor and in the _Load method with no luck.
xain
If you want to use data binding I'd start reading here: http://msdn.microsoft.com/en-us/library/c8aebh9k.aspxFor the data bound control to update when the object it is bound to changes that object will need to implement INotifyPropertyChanged.
Phil Lamb
+1  A: 

Have a look at the MVP pattern - you could have your form implement an IView interface. Your other object would be the Presenter that would call, for example, IView.UpdateText() when something changes (or have your view subscribe to the presenters events - i prefer the method approach).

This separates your concerns and makes your solution more testable as you can mock up implementations of IView, IPresenter and IModel.

The form should check if this.InvokeRequired == true to determine if the incoming request is on the UI thread. If it is not you will need to use a delegate.

public delegate void UpdateTextDelegate(string text);

public void UpdateText(string text)
{
  // Check we are on the right thread.
  if (!this.InvokeRequired)
  {
      // Update textbox here
  }
  else
  {
      UpdateTextDelegate updateText = new UpdateTextDelegate(this.UpdateText);

      // Executes a delegate on the thread that owns the control's underlying window handle.
      this.Invoke(updateText, new object[] { text });
  }

}

Peter Kelly