Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to count Label controls count in Xamarin?

How to count Label controls count in Xamarin?

Scheduled Pinned Locked Moved C#
questioncsharpmobilewinformstutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Dunlop
    wrote on last edited by
    #1

    Hi, I want to write a code like this that runs perfectly in WinForms:

    int count = 0;
    foreach (Control c in this.Controls)
    {
    if (c.GetType() == typeof(Label))
    if (((Label)c).BackColor == Color.Red)
    count++;
    }

    But in Xamarin, I cannot use Control class. There is no namespace for it. How can I do the same in Xamarin?

    R 1 Reply Last reply
    0
    • A Alex Dunlop

      Hi, I want to write a code like this that runs perfectly in WinForms:

      int count = 0;
      foreach (Control c in this.Controls)
      {
      if (c.GetType() == typeof(Label))
      if (((Label)c).BackColor == Color.Red)
      count++;
      }

      But in Xamarin, I cannot use Control class. There is no namespace for it. How can I do the same in Xamarin?

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      Your WinForms code would be better written as:

      int count = 0;
      foreach (Label l in this.Controls.OfType<Label>())
      {
      if (l.BackColor == Color.Red)
      {
      count++;
      }
      }

      Or simply:

      int count = this.Controls.OfType<Label>().Count(l => l.BackColor == Color.Red);

      Xamarin forms doesn't have a similar concept. You might be able to do something similar by walking the visual tree - eg: the urban canuk, eh: VisualTreeHelper for Xamarin.Forms[^] However, it would almost certainly be better to use MVVM concepts to achieve your goal.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups