Save from a Toolbarbutton

Save from a Toolbarbutton

Old forum URL: forums.lhotka.net/forums/t/6439.aspx


l.vanderdonk posted on Tuesday, February 17, 2009

Hi There,

Im very ;) new to Csla and tried an example from the book. It works fine when im using a standard windows forms button, but when im using a toolbarbutton the last field i entered will be cleared after the save operation. I hope im not nr gazilion asking this question.

Best Regards,

Laurens

 

 

public partial class InvoiceView : UserControl, ISave
    {
        public InvoiceView()
        {
            InitializeComponent();
   invoice = Invoice.NewInvoice();

   BindUI();
        }
  private Invoice invoice = null;

  private BindingSourceNode _bindingTree = null;

  private void BindUI()
  {
   _bindingTree = BindingSourceHelper.InitializeBindingSourceTree(this.components, invoiceBindingSource);
   _bindingTree.Bind(invoice);
  }

  public void Save()
  {
   if(SaveObject())
    MessageBox.Show("Object saved..");
  }
  public bool SaveObject()
  {
 
   bool ret = false;
   _bindingTree.Apply();
   try
   {
    invoice = invoice.Save();
    ret = true;
   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
   BindUI();
   return ret;
  }

  private void button1_Click(object sender, EventArgs e)
  {
   SaveObject();
  }

    }

tetranz replied on Tuesday, February 17, 2009

I think your problem is that, for some reason, when you move focus from an input control to a toolbar button, the input control does not validate like it does when you move to another "normal" control.

The answer is simply to call this.Validate() in your toolbar button's click event. "this" referring to the form.

l.vanderdonk replied on Tuesday, February 17, 2009

Hi Tetranz,

That's it. Thanks a lot!!

Best Regards,

Laurens

lukky replied on Tuesday, February 17, 2009

I think this has probably bitten everybody at some point.

MS should document this in 3" tall, red blinking letters Wink [;)]

Copyright (c) Marimer LLC