如何将windows.form.control类型转换为特定类型 [英] how to convert a windows.form.control Type to a particular type

查看:79
本文介绍了如何将windows.form.control类型转换为特定类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在按钮单击后清除窗体上面板中所有控件的某些属性

i发送面板,其中包含一些控件作为文本框,复选框和单选按钮的功能一个类清除文本框的文本,检查复选框和单选按钮的状态但是,当我在代码中获得面板控件的类型时,我不能访问.text属性或检查状态根据控制的类型,有什么方法可以丰富它们吗?



谢谢你。





这是我的代码:





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;
           }

推荐答案

这个问题完全错了。原则上,您不能将一种控制类型转换为另一种控制类型。这不是引用类型对象的工作方式。



不要将它与类型转换的引用类型和赋值兼容性混淆变量/成员。例如,您总是可以指定控件类型,比如变量/ member Control ,但它不会改变运行时你的控制类型。实际上什么都不能改变它。要获得它,您需要了解继承,引用和引用类型,赋值兼容性,运行时与编译时类型。



我想警告你:除非你学到这一点,否则你不能真正做任何UI开发,还有更多;您实际上需要非常了解所有OOP主题,以及接口,委托类型和实例,事件和处理程序,以便能够做到最低限度。而现在,我们正在讨论的主题远远低于真正的OOP,它主要在抽象和虚拟方法,覆盖,后期绑定和多态性的层面上开始。你需要知道这一切,没有排除。从头开始学习,使用一些基本的手册,做简单的练习,更好地作为一些仅限控制台的应用程序。在你学习之前,不要考虑用户界面,否则你只会浪费你的时间并且会感到非常沮丧。



-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


您好



当您使用Controls属性访问控件的子级时,它会返回一个集合,是Control类型,但它并不能确切地告诉你哪种类型。



你可以将你的对象转换为你期望的类型。



例如,如果它是文本框:((TextBox)crt).Text = string.Empty;

如果是CheckBox:((CheckBox)crt).Checked = false;



等等..



Valery。
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.


这篇关于如何将windows.form.control类型转换为特定类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆