简体   繁体   English

自定义控件未更新C#

[英]Custom Control Not Updating C#

I cannot for the life of me get a custom control updating itself. 我一生无法获得自定义控件进行自我更新。 In the below example, "test label1" works and outputs as expected. 在下面的示例中,“ test label1”按预期方式工作并输出。 However when I try and get it to do the same thing later on, it will not. 但是,当我尝试让它以后做相同的事情时,它不会。 I've tried calling Update and Invalidate, but still nothing. 我试过调用Update和Invalidate,但还是没有。 Am I missing something? 我想念什么吗?

Swapping the points around doesn't work either, so it doesn't look like it's just off screen for example. 交换点也不起作用,例如,它看起来好像不在屏幕上。

public partial class AdvancedListControl : UserControl
{
    public void createAnotherLabel()
    {
        Controls.Add(new Label { Location = new Point(14, 14), AutoSize = true, Text = "test label2 - test" }); // this won't create
        // me.Controls.Add(new Label { Location = new Point(14, 14), AutoSize = true, Text = "test label2 - test" });
        // Update();
        // Invalidate();
    }

    private AdvancedListControl me;

    public AdvancedListControl()
    {
        InitializeComponent();

        me = this;
        Controls.Add(new Label { Location = new Point(26, 26), AutoSize = true, Text = "test label1" });
    }
}

Thanks. 谢谢。

该控制方法必须由最终用户操作(如按钮的事件处理程序)驱动的控件调用,从而导致页面回发,这将导致该控件触发该方法并以更新控件状态呈现页面。

Meh! Me! Noob mistake. 菜鸟的错误。 I did the same as Jashaszun and created a new project, and yes it did work. 我和Jashaszun做的一样,并创建了一个新项目,是的,它确实有效。 Turns out I had two copies of the advanced control on a form, and the one that was hiding was the one I was hitting. 原来,我在表单上有两份高级控件,而隐藏的是我所击中的一份。 It's been a long day. 这是漫长的一天。 ;-) ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM