locked
Programatically setting values to controls within a Group box RRS feed

  • Question

  • Hi,

    I have a Group box containing a Table Layout, with 4 columns: 2 of them contain only checkboxes and the other 2 contain date pickers.

    Is there a way to add a command that sets all the checkboxes value to True and all the dates to Today, cycling thru them? (without referring to each control's name, so if a checkbox and a datepicker are added, the code still works). It does not have to affect controls outside the Group box or Table Layout.

    thanks.

    Nico.


    Monday, September 23, 2013 2:43 PM

All replies

  • You can access the

    GroupBox.Controls

    property, which gives you a list of controls in that groupbox. If you want to get a set of controls based on type, go

    var radiobuttons = GroubBox.Controls.OfType<RadioButton>();

    • Proposed as answer by Jeff Lefebvre Monday, September 23, 2013 3:34 PM
    Monday, September 23, 2013 3:33 PM
  • Thank you Jeff but I couldn't get to the GroupBox object. Could you be more specific? Also I think there must be a foreach somewhere, something like:

    var gb = this.FindControl("GroupBox1");
    
    foreach (Control ctrl in gb.Controls)
    {
      if (ctrl.OfType<RadioButton>() 
      {
        //set value to True
      }
    
      if (ctrl.OfType<DatePicker>()
      {
       //set value to DateTime.Today()
      }
    }

    Thanks.
    Monday, September 23, 2013 7:28 PM
  • If the checkboxes are bound to the ViewModel you should be updating the corresponding properties there. Is the data in the table a bound entity?


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Monday, September 23, 2013 7:37 PM
  • Yes it is bound to data, I just want to save time to the user by adding a command button that does what I explained. It is not a validation.

    If I understand what you are saying, I should set the values to the underlying entity properties?

    Monday, September 23, 2013 7:42 PM
  • If it is bound and checkbox = Boolean property then can't you simply loop around the screen collection and update the date and Boolean?

    I have hacked the Silverlight UI a lot but always try to avoid it if possible. Can't help but think you don't need to hack it here. Get back and we can finish the problem off.


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Monday, September 23, 2013 7:48 PM
  • Dave,

    I did it, but without looping around the screen collection (don't know how...).

    I set every property manually.

    Nico.

    Monday, September 23, 2013 8:42 PM