Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
i am trying to clear some properties of all controls in a panel on a form after an button click
i send panel which contains some controls as text boxes ,check boxes and radio buttons to a function of a class to clear text of text boxes , check state of check boxes and radio buttons but ,
when i get the type of control of panel in code , i do not access to .text properties or check state according to the type of control , is any way to rich them ?
 
thank u .
 

here is my code :
 

public int clearControls(Control  typicalPanel)
       {
 
          foreach(Control ctr in typicalPanel.Controls )
          {
              System.Type InstanceType = typicalPanel.GetType();
 
           switch (InstanceType.Name)
           {
 
               case "RadioButton" :
 
                        // here i can not access checked property

                       ctr.Checked = false;
                  
                   break;
 
               case "CheckBox" :
                
 
                       // here i can not access checked property

                       ctr.Checked = false;
                
                   break;
 
               case "TextBox":
 
                          // here i can not access .text property

                      ctr.Text  ="";
                 
                   break;
           }
Posted 22-Feb-13 11:16am
Edited 22-Feb-13 11:19am
v3
Comments
Sergey Alexandrovich Kryukov at 22-Feb-13 17:29pm
   
Oh, no!
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The question is totally wrong. You cannot "convert" one control type to another, in principle. This is not how reference-type objects work.
 
Don't mix up this with type cast of reference types and assignment compatibility between variables/members. You always can, for example, assign your control type, to say, the variable/member Control, but it won't change the run-time type of your control. Nothing actually can change it. To get it, you need to learn about inheritance, references and reference types, assignment compatibility, run-time vs compile-time types.
 
And I want to warn you: you cannot really do any UI development until you learn this, and a lot more; you actually need to know very well all the OOP topics, and also interfaces, delegates types and instances, events and handlers, to be able to do just the bare minimum. And right now, we are discussing topic well below the "real" OOP which essentially starts somewhere at the level of abstract and virtual methods, overrides, late binding and polymorphism. You need to know it all, no exclusions. Learn it all from scratch, use some basic manual, do simple exercises, better as some console-only application. Before you learn it all, don't even think about UI, otherwise you would only waste your time and get a lot of frustration.
 
—SA
  Permalink  
Comments
rooja_59 at 23-Feb-13 6:27am
   
your advice will help me .
Sergey Alexandrovich Kryukov at 23-Feb-13 19:33pm
   
I'm sure it will if you listen to it.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi
 
when you access the children of a control using the Controls property it returns you a collection that is of type Control, but it does not tell you which type exactly.
 
What you could to is cast your objects to the type you are expecting.
 
for example if it is a Textbox : ((TextBox)crt).Text = string.Empty;
if is is a CheckBox : ((CheckBox)crt).Checked = false;
 
etc..
 
Valery.
  Permalink  
Comments
rooja_59 at 23-Feb-13 6:17am
   
thank u Valery it was completely helpful !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 325
1 Sergey Alexandrovich Kryukov 249
2 OriginalGriff 230
3 George Jonsson 130
4 ChauhanAjay 104
0 OriginalGriff 6,464
1 Sergey Alexandrovich Kryukov 5,915
2 CPallini 5,175
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100