用户控件,其他开发人员可以向其添加控件并“继承”用户控件。我控件上所有控件的行为 [英] user control that other developers can add controls to it and "inherit" the behavior of all controls on my control

查看:156
本文介绍了用户控件,其他开发人员可以向其添加控件并“继承”用户控件。我控件上所有控件的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,对于令人困惑的标题感到抱歉...暂时找不到正确的词。
出于好奇,我正在使用c#用户控件,我创建了一个从面板构建的控件,一个用作过滤器的文本框以及一些标签/按钮/等等...被过滤。
当您更改文本框的文本时,面板上的所有控件都是可见/不可见的,具体取决于它们的Text属性是否包含文本框的Text。非常简单。



但是我希望此用户控件能够使使用它的用户可以向其放置更多标签或控件,并且它们的行为相同,我可以



当我编辑控件(向其添加控件)时,它按预期工作,新控件的行为与旧控件相同没有代码修改的代码,但仅当我在编辑用户控件而不是使用它时才使用。



将用户控件拖到窗体上时,无法添加控件...当我尝试向控件添加标签时-它只是添加到窗体而不是控件中,因此文本框不会影响添加的标签。如果我希望能够将控件添加到表单中,然后向该控件添加一些控件,该怎么办?



我对某些指针很满意。
是相关代码:

  private void textBox1_TextChanged(object sender,EventArgs e)
{
foreach(panel1.Controls中的控件c)
{
if(c.Text.Contains(textBox1.Text))
{
c.Visible = true;
}
else
{
c.Visible = false;
}
}
}

编辑-图片







如您所见-我在过滤器文本框中键入1,并且除button1之外的所有控件现在都不可见-当然,行为标签也不正确。



Jim,谢谢。

解决方案

您描述了我几乎从不使用 UserControls 的原因之一。



您可以将其设置为不是 UserControl的类。



,即使其成为 Panel (或 FlowLayoutPanel 的简单子类)为了方便起见,同时在我的测试中将其放下)。

  class FilterPanel:FlowLayoutPanel 
{
TextBox tb_filterBox {get;组; }
标签st_filterLabel {get;组; }
public FilterPanel()
{
st_filterLabel = new Label();
st_filterLabel.Text =过滤器:;
this.Controls.Add(st_filterLabel);
tb_filterBox = new TextBox();
this.Controls.Add(tb_filterBox);
// st_filterLabel.Location = new Point(10,10); // FLP不需要
// tb_filterBox.Location = new Point(100,10); //将其用于面板!

tb_filterBox.TextChanged + = tb_filterBox_TextChanged;
}

void tb_filterBox_TextChanged(对象发送者,EventArgs e)
{
foreach(此控件中的ctl)
{
if (ctl!= tb_filterBox&& ctl!= st_filterLabel)
ctl.Visible = ctl.Text.Contains(tb_filterBox.Text);
}
}
}

现在将其放在表单(或其他内容),您(或任何人)可以在设计器中将 Controls 放到其上,它们将成为其 Controls 集合,就像您想要的那样,并且将按预期方式运行。.



关于控件子类化的两个说明:




  • 如果在开发过程中破坏了一个,使用它的表单也将被破坏,直到解决问题为止。因此,请多加注意!


  • 为使设计器显示控件,它总是需要一个无参数的构造函数,如上面的构造函数。即使您希望能够上交参数,也必须有一个无参数的构造函数,否则设计器会遇到麻烦!



hi all and sorry for the confusing title... can't find the right words yet. just out of curiosity, I am playing with c# user control, I created a control that is built from a panel, a textbox that is being used as a filter, and some labels / buttons/ etc... that are being filtered. when ever you change the text of the textbox, all the controls on the panel are visible / invisible depending if their Text property contains the Text of the textbox. very simple.

but I want this user control to be such that the user that uses it can drop more labels or controls to it and they will behave the same, I can't figure out how to do that..

when I am editing the control (adding controls to it), it works as expected and the new controls behave as the old ones without code modifications, but only when I am editing the user control and not when using it.

when I am dragging the user control to a form, I can not add controls to it... when I try to add a label to the control - it is just added to the form and not to the control and therefore the text box is not influencing the added label. what should I do if I want to be able to add the control to a form and then add some controls to the control?

I will be happy for some pointers. here is the relevant code:

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        foreach (Control c in panel1.Controls)
        {
            if (c.Text.Contains(textBox1.Text))
            {
                c.Visible = true;
            }
            else
            {
                c.Visible = false;
            }
        }
    }

edit - pictures added.

as you can see - i typed 1 in the filter text box and all the controls except button1 are now invisible - and of course the bad behaving label.

Thanks, Jim.

解决方案

You describe one of the reasons why I almost never use UserControls. Anything that isn't done to the original UC must be done in code..

You can instead make it a class that is not a UserControl, ie make it a simple subclass of Panel (or FlowLayoutPanel as I do here merely for convenience, while dropping stuff on it during my tests).

class FilterPanel : FlowLayoutPanel
{
    TextBox tb_filterBox { get; set; }
    Label st_filterLabel { get; set; }
    public FilterPanel()
    {
        st_filterLabel = new Label();
        st_filterLabel.Text = "Filter:";
        this.Controls.Add(st_filterLabel);
        tb_filterBox = new TextBox();
        this.Controls.Add(tb_filterBox);
        // st_filterLabel.Location = new Point(10, 10);  // not needed for a FLP
        // tb_filterBox.Location = new Point(100, 10);   // use it for a Panel!

        tb_filterBox.TextChanged += tb_filterBox_TextChanged;
    }

    void tb_filterBox_TextChanged(object sender, EventArgs e)
    {
        foreach(Control ctl in this.Controls)
        {
            if (ctl != tb_filterBox && ctl != st_filterLabel)
                ctl.Visible = ctl.Text.Contains(tb_filterBox.Text);
        }
    }
}

Now after placing it on a form (or whatever) you (or whoever) can drop Controls onto it in the designer and they'll be part of its Controls collection, just like you want it and will behave as expected..

Two notes on subclassing Controls:

  • If you break one during developement, the Form(s) using it will be broken, too, until you fix the problem. So take a little extra care!

  • For the Designer to display the Control it always needs to have one parameterless constructor, like the one above. Even if you prefer to have the ability to hand in parameters, one parameterless constructor must still be there or the designer will get into trouble!

这篇关于用户控件,其他开发人员可以向其添加控件并“继承”用户控件。我控件上所有控件的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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