คำตอบ How to manipulate checkbox control?

  • Thursday, August 25, 2011 6:46 AM
     
      Has Code

    Hi,

     

    I've been working with a checkbox control and can't realy figure out how to manipulate it.

     

     

    Scenarios:

    1. How to make the checkbox control to toggle only with "true" and "False" value?  Get rid of the "dash sign".

    2. I want to control the value of the other check boxes based from a certain checkbox value.  Let say when I click on the "First Quarter", all data referenced to first quarter would be filtered and the other check boxes would appear "False" or blank.  That same feature would also apply to other check boxes.  The query work fine but I cant make the other check boxes apper "false" or "blank". A radio button might work fine but LS has no radio button control. I used this code but no success at all.

     Private Sub PlanDetailBy_ActiveStatus_PreprocessQuery(OfficeName As String, Quarter1 As System.Nullable(Of Boolean), Quarter2 As System.Nullable(Of Boolean), Quarter3 As System.Nullable(Of Boolean), Quarter4 As System.Nullable(Of Boolean), ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.ProcurementPlanDetails))
    
          If Not (Quarter1.HasValue) Then Quarter1 = False
    
          If (Quarter1) Then
    
            Quarter2 = False
            Quarter3 = False
            Quarter4 = False
    
            query = From p In query
                Where p.PurchaseOn = 1
                Select p
    
          End If
    
    
          If Not (Quarter2.HasValue) Then Quarter2 = False
    
          If (Quarter2) Then
            Quarter1 = False
            Quarter3 = False
            Quarter4 = False
            query = From p In query
                Where p.PurchaseOn = 2
                Select p
    
          End If
    
          If Not (Quarter3.HasValue) Then Quarter3 = False
    
          If (Quarter3) Then
            Quarter1 = False
            Quarter2 = False
            Quarter4 = False
            query = From p In query
                Where p.PurchaseOn = 3
                Select p
    
          End If
          If Not (Quarter4.HasValue) Then Quarter4 = False
    
          If (Quarter4) Then
    
            Quarter1 = False
            Quarter2 = False
            Quarter3 = False
            query = From p In query
                Where p.PurchaseOn = 4
                Select p
    
          End If
        End Sub
    

    Your help would be very much appreciated...

     

     

    Nante

     

     

     

     

     

     

     

     

All Replies

  • Thursday, August 25, 2011 7:13 AM
     
     Proposed Answer Has Code

    Hi Nante

    Your CheckBox are local property. If so, you need when createing Local property on Screen to choose Boolean type.

    For instance, if you have three cb and have code like this (C#):

     

    partial void CB1_Changed()
    {
    	if (CB1 == true)
    	{
    		CB2 = false;
    		CB3 = false;
    	}
    	else
    	{
    		CB2 = true;
    		CB3 = false;
    	}
    }
    

    when you click on CB1 you got:

     

    and click again on CB1

    I think this would help.

    Spaso Lazarevic

     


  • Thursday, August 25, 2011 7:28 AM
    Moderator
     
     Answered Has Code

    I'm assuming that the screen members that the four check boxes are bound to are local screen properties (they show up as top-level members in the screen members list). I'll refer to these screen members as Quarter1, Quarter2, Quarter3, and Quarter4.

    1. The screen members for your check boxes need to be non-nullable. The way to do this is to select the screen member (e.g. Quarter1) and check on the Is Required property in the property sheet. The check boxes will then only have two states: checked and unchecked.

    2. For each conditional in your PreprocessQuery method, it looks like you try to set the other parameters to False. This will not work because these parameters are only inputs to the query and setting them will not change the value of the source of the parameter because the method signature does not implement the parameters as ByRef (they are, in fact, ByVal parameters). What you really want to do is for each screen member in the screen designer is to write code that when it changes to true to set the other members to false. To do this, click on the screen member (e.g. Quarter1), select the drop down arrow next to Write Code in the toolbar, and select Quarter1_Changed. Then write code that changes the other members if this member is set to true. For example:

    Private Sub Quarter1_Changed()
      If Me.Quarter1 Then
        Me.Quarter2 = False
        Me.Quarter3 = False
        Me.Quarter4 = False
      End If
    End Sub
    

    Do this for each of the four Boolean screen members that the check boxes are bound to and you should get the behavior you want at runtime.


    Justin Anderson, LightSwitch Development Team
  • Thursday, August 25, 2011 8:59 AM
    Moderator
     
     

    Nante, if your properties are nullable, you'll get three choices of values, ticked (true) , unticked (false), & dash (null).

    If you only want true/false, make sure you'r properties are NOT nullable.


    Yann

    (plus ça change, plus c'est la même chose!)

  • Thursday, August 25, 2011 9:10 AM
     
     

    And NOT nullable means that when you are creating local properties: Is Required is ckecked like as:

    Sorry Yann, I need to put picture.

     

  • Thursday, August 25, 2011 10:09 AM
     
     

    Hi,

     

    I realy appreciate your efforts in helping here. Thank you so much folks.

     

    Nante

    • Marked As Answer by RKage Thursday, August 25, 2011 10:11 AM
    • Unmarked As Answer by RKage Thursday, August 25, 2011 10:11 AM
    •  
  • Friday, August 26, 2011 12:23 AM
    Moderator
     
     

    No problem!

    Anything that helps people understand is a good thing.


    Yann

    (plus ça change, plus c'est la même chose!)

  • Thursday, September 01, 2011 8:29 AM
     
     

    And if the boolean is NOT a local property? If it is, say, a propery in a sql table?

    I am not able to select neither "Required" in the data designer nor "Is Required" in the properties - it is greyed out; disabled... I guess with good reason...

    Can I get the same effect with custom validation somehow? ... "null = false" kinda stuff??

  • Thursday, September 01, 2011 8:48 AM
     
     

    Okay - so turns out that wasn't hard at all. In the custom validation sub put:

    If Not Me.myProperty.HasValue Then Me.myProperty = False

    Please advice me if this has any drawbacks... And if I extended this thread for no reason, I apologise :)