在当前项目的表单中查找表单控件 [英] find form controls in forms in current project

查看:84
本文介绍了在当前项目的表单中查找表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我想在当前项目的所有形式中找到TextBox控件并更改其字体.
我想要使​​用C#Windows应用程序进行解决方案编码.

如果可能的话,请逐步给我解决方案,因为我在编程方面比较新鲜.
感谢所有人

Ruchik.

Hi All

I want to find TextBox control in among all the forms of current project and change their fonts.
I want solution coding with c# windows application.

If possible please give me solution step by step because i m fresher in programming.
Thanks to all

Ruchik.

推荐答案

我不太清楚您想要什么.如果要更改解决方案/项目中所有TextBox控件的字体,恐怕您将不得不在设计器中一次执行一个控件.我之所以这么说并不是因为不可能编写另一个为您解析源文件的应用程序,而是因为您必须查看更改的形式(对其他位置的相对位置会有所保留)控件也具有相同的形式(更不用说表单本身了).

现在,如果要以编程方式更改表单上所有tTextBox控件上的字体,则可以轻松地做到这一点,但这并不妨碍您不必考虑相同的位置并消除已经描述的结果. >
最后,无论您有多少原始代码,对代码的需求通常都不会很好解决.如果要以编程方式更改控件中的字体,这类似于我的操作方式:

I don''t understand exactly what you want. If you want to change the font in all of the TextBox controls in a solution/project, I''m afraid you''re going to have to do that one control at a time in the designer. I''m not saying this because it''s impossible to write another app that parses the source files for you, but because you have to SEE what the change is going to do in the form with ragards to the relative position of the other controls that are also on the same form (not to mention the form itself).

Now, if you want to programatically change the font on all of the tTextBox controls on a form, you can easily do that, but that doesn''t preclude you from having to consider the same positioning and soize ramifications already described.

Lastly, a demand for code typically doesn''t go over very well here, no matter how much of a code virgin you are. If you want to programatically change the font in a control, this is something like the way I''d do it:

private void ChangeFontInTextBoxes(Control container)
{
    foreach (Control ctrl in container.Controls)
    {
        if (ctrl is TextBox)
        {
            // set the font or other desired properties
        }
        else
        {
            if (ctrl.HasChildren)
            {
                ChangeFontInTextBoxes(ctrl);
            }
        }
    }
}



我会用这样的形式来称呼它:



and I''d call it from the form like this:

ChangeFontInTextBoxes(this);



上面的代码使用递归(用谷歌浏览).



The code above uses recursion (google it).


我建​​议不要创建控件,而是创建自己的TextBox类,在构造函数中设置其Font属性,并且在您的所有形式中使用此自定义控件:

Instead of searhing the controls, I suggest to create your own TextBox class, to set its Font property in the construtor, and use this custom control in all your forms:

class MyTextBox : TextBox
{
    public MyTextBox()
    {
        Font = ...;
    }
}


如果尚未在控件(包括TextBox)上显式设置Font,它将从其父容器继承它.因此,要更改整个表单的字体(包括具有默认字体属性的所有子控件),只需在表单上设置Font.我相信,同样的事情也可以与其他容器控件(例如Panels和GroupBoxes)一起使用.
If you haven''t explicitly set the Font on a control (including a TextBox), it will inherit it from its parent container. Thus, to change the font for a whole form (including all the subcontrols with default font attributes), you just need to set Font on the form. The same thing works with other container controls (like Panels and GroupBoxes) too, I believe.


这篇关于在当前项目的表单中查找表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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