Winform TextBox属性 [英] Winform TextBox Properties

查看:57
本文介绍了Winform TextBox属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



我的winform在面板内和面板外有各种texbox。

我想设置像backcolor这样的属性,在另一个班级启用等。

但我们无法做到。



我也尝试过这样:foreach(控件c在控件中),但无法访问该控件退出面板。



请建议我最好。



--- Anil Kumar。



感谢您的回复。在那种情况下,我已经实现了datagridview的成功代码,如下所示: -



具有DataGridView的表单代码:

==== ==================================

Hi Experts,

My winform has various texboxes inside panels and outside the panels.
I want to set their properties like backcolor, enabled etc in a separate class.
but we are unable to do it.

I have also tried like this: "foreach(Control c in Controls)", but unable to access that control exits in panel.

Kindly suggest me the best.

--- Anil Kumar.

Thanks for prompt replies. In that scenario i have already implemented successful code for datagridview as below: -

code for the form having DataGridView:
======================================

...
....
...
ConnetDataBase conDB = new ConnetDataBase();
...
...
conDB.FillDataGrid(Sql, dgvUserList);



类的代码: -

================


code for class: -
================

class ConnetDataBase
    {

public void FillDataGrid( string Sql,DataGridView dgvName)
{
    try
    {
        connection.Open();
        adapter = new SqlDataAdapter(Sql, connection);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
         dgvName.DataSource = ds.Tables[0];
        connection.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

}



---------------- -----------

这适用于从任何表单调用的任何datagridview。

但是对于文本框或其他控件,如何实现它?



--- Anil kumar


---------------------------
This works fine for any datagridview calling from any forms.
But for textbox or other controls how can it may be implemented ?

--- Anil kumar

推荐答案

如果你想访问面板内的控件你必须解决面板的控件集合。



所以为了让这个工作你必须检查控件类型,如果它是一个面板,你将不得不重新审视所有面板再次控制。
If you want to access the controls inside a panel you have to address the panel's controls collection.

So to get this working you'll have to check the controls type and if it's a panel you'll have to go over all the panels controls again.


这个片段可以帮助你:



This snippet might help you :

foreach (Control cntol in this.Controls)
{
    Type type = cntol.GetType();
    //if (type.Name == "Panel")
    if(cntol is Panel)
    {
        foreach (Control panelControls in cntol.Controls)
        {
            //Access the panel's controls here
        }
    }
    else
    {
        // Access the controls outside of the panels here
    }
}


如果未明确设置,则会继承许多控件的背景颜色。



在一般情况下,假设您想要根据业务逻辑中的某些内容设置这些控件属性,那么最好是使用数据绑定而不是编写显式属性操作代码,这可能会非常复杂并且难以快速地保持同步。为表单创建演示者(或控制器或模型 - 视图)类,如果从逻辑上不同的位置提取数据,则可能更多,并且数据将控件的属性绑定到该类的属性。
The background colour of many controls is inherited, if you don't set it explicitly.

In the general case, assuming you are wanting to set these control properties based on something in your business logic, it is better to use data binding than to write explicit property manipulation code, which can get very complex and hard to keep in sync surprisingly quickly. Create a presenter (or controller or model-view) class for the form, or perhaps more if it is pulling data from logically different locations, and data bind the properties of the controls to properties of that class.


这篇关于Winform TextBox属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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