Click here to Skip to main content
15,881,844 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 :


C#
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
Updated 22-Feb-13 11:19am
v3
Comments
Sergey Alexandrovich Kryukov 22-Feb-13 17:29pm    
Oh, no!
—SA

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
 
Share this answer
 
Comments
rooja_59 23-Feb-13 6:27am    
your advice will help me .
Sergey Alexandrovich Kryukov 23-Feb-13 19:33pm    
I'm sure it will if you listen to it.
—SA
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.
 
Share this answer
 
Comments
rooja_59 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900