Search code examples
.netvb.netradio-buttongroupbox

Getting Which Radio Button is Checked in a Groupbox


Found this in here. Could someone explain how this works? especially starting from the Function line.

 Dim rButton As RadioButton = 
    GroupBox1.Controls
   .OfType(Of RadioButton)
   .Where(Function(r) r.Checked = True)
   .FirstOrDefault()

REFERENCE: How to get a checked radio button in a groupbox?


Solution

  • That's called LINQ.

    Givin a collection of objects (so is GroupBox1.Controls, a collection of RadioButton objects), you can query the collection. So, you have a query that retrieves the First RadioButton (or null if there aren't any by using FirstOrDefault) from the RadioButton collection that satisfy the condition of being checked (Function(r) r.Checked = True it's a lambda expression). Because Controls is a collection of objects, you need to cast the to RadioButton so, you can access the IsChecked property. Hope the explanation helps; anyway you must check the LINQ reference for VB