Determine control type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CJack

    Determine control type

    hi, i have a window form with different controls. i want to loop
    through all the controls and write the types and lables of each
    control in a file. I dnt know how to determine the type of a control
    that wether it is a button, lable or text box.
    any body out there to help please.

    thanks in advance
  • Peter Koen

    #2
    Re: Determine control type

    shazalmay@hotma il.com (CJack) wrote in news:2e52a6a3.0 310251301.27521 6a0
    @posting.google .com:
    [color=blue]
    > hi, i have a window form with different controls. i want to loop
    > through all the controls and write the types and lables of each
    > control in a file. I dnt know how to determine the type of a control
    > that wether it is a button, lable or text box.
    > any body out there to help please.
    >
    > thanks in advance
    >[/color]

    foreach(Control c in Controls)
    {
    Button b = c as Button;
    if(n!=null)
    {
    //Button code
    return;
    }

    TextBox tb = c as TextBox;
    if(tb!=null)
    {
    //TextBox code
    return;
    }
    }

    --
    best regards

    Peter Koen
    -----------------------------------
    MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS

    Comment

    • TB

      #3
      Re: Determine control type

      "Peter Koen" <koen-newsreply&snusn u.at> wrote in message
      news:u9PCowzmDH A.2772@TK2MSFTN GP12.phx.gbl...[color=blue]
      > shazalmay@hotma il.com (CJack) wrote in news:2e52a6a3.0 310251301.27521 6a0
      > @posting.google .com:
      >[color=green]
      > > hi, i have a window form with different controls. i want to loop
      > > through all the controls and write the types and lables of each
      > > control in a file. I dnt know how to determine the type of a control
      > > that wether it is a button, lable or text box.
      > > any body out there to help please.
      > >
      > > thanks in advance
      > >[/color]
      >
      > foreach(Control c in Controls)
      > {
      > Button b = c as Button;
      > if(n!=null)
      > {
      > //Button code
      > return;
      > }
      >
      > TextBox tb = c as TextBox;
      > if(tb!=null)
      > {
      > //TextBox code
      > return;
      > }
      > }
      >
      > --[/color]

      Just use object.GetType( ) and Control.Text to get the system type of each
      control and its label.

      e.g.
      foreach (Control c in Controls)
      {
      Console.WriteLi ne("Control Type = {0}, Label = {1}",
      c.GetType(),
      c.Text);
      }

      Since all controls have the Text property, there is no need to coerce the
      controls to their specific type.

      However, if you do need to then the above code certainly does the trick.
      Likewise you can use the "is" keyword if you don't actually need to operate
      on the control.

      if (c is Button)
      // Special case for Button controls

      -- TB


      Comment

      Working...