检查哪些控件有边框c#

Check which Controls have Borders c#

我正在制作 Winforms 应用程序。因为我想重绘一些边框,所以我想遍历控件并检查哪些控件有边框。不幸的是我不知道如何完成这个。

我知道面板和文本框等有一个 属性 BorderStyle 但我无法在循环浏览控件时访问它。我使用此 link 中的函数:https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.controls?view=netframework-4.8 来遍历控件。

您可以为您使用的每种类型的控件(TextBox、Label 等)使用 foreach

var controlsWithoutBorders = new List<Control>();

// Check textboxes
foreach (var control in controls.OfType<TextBox>())
{
    if (control.BorderStyle != BorderStyle.None)
    {
        controlsWithoutBorders.Add(control);
    }
}

//Check labels
foreach (var control in controls.OfType<Label>())
{
    if (control.BorderStyle != BorderStyle.None)
    {
        controlsWithoutBorders.Add(control);
    }
}

或者,您可以在所有控件上使用单个 foreach,并尝试将控件强制转换为每种类型。如果控件实际上是您要将其转换为的对象,则转换将成功,否则它将 return 为 null(例如,尝试将 Control 转换为 TextBoxLabel 将 return null)

var controlsWithoutBorders = new List<Control>();

foreach (var control in controls)
{
    var controlAsTextBox = control as TextBox;
    var controlAsLabel = control as Label;

    if (controlAsTextBox != null && controlAsTextBox.BorderStyle != BorderStyle.None)
    {
        controlsWithBorders.Add(control);
    }

    if (controlAsLabel != null && controlAsLabel.BorderStyle != BorderStyle.None)
    {
        controlsWithBorders.Add(control);
    }
}

如果你有一个面板,你可以在面板上 foreach。我使用表单加载作为事件

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (var item in this.Controls)
        {
            switch (item.GetType().Name)
            {
                case "Label":
                    if (((Label)item).BorderStyle == BorderStyle.None)
                    {
                        //Your commands
                    }
                break;
                case "TextBox":
                    if (((TextBox)item).BorderStyle == BorderStyle.None)
                    {
                        //Your commands
                    }
               break;
            }
        }
    }

或者您可以动态查看它们

推荐你使用动态方式

这样,您的应用就不会遇到异常或错误

        foreach (var item in this.Controls)
        {
            //Get item from form
            dynamic element = item;
            //check if there is any property called Borderstyle exists
            var res = item.GetType().GetProperties().Where(p => p.Name.Equals("BorderStyle")).FirstOrDefault();
            //if it exists and value is not equal None(Control have border)
            if (res !=null && !(res.GetValue(item).Equals("None")))
            {
            res.SetValue(item, BorderStyle.FixedSingle, null);
            //your other commands
            }
        }

虽然此方法在 运行 时不是最快的,但使用 C# 的动态类型功能可以彻底解决您的问题。考虑下面的代码片段。

   public void DealWithBorder(List<Control> lsControls) {
        foreach(var element in lsControls){
            dynamic dynamicElement = element;
            try{
                BorderStyle style = dynamicElement.BorderStyle;
                // Handle if property does exist in the control
            }
            catch{
                // Handle if property doesnt exist in the control
            }
        }
    }

在英语中,它会尝试将 属性 视为对象中存在,但如果不存在,则会抛出异常。有关动态类型的详细信息,请单击 here