【问题标题】:C# Getting a component's values at runtime?C# 在运行时获取组件的值?
【发布时间】:2011-03-27 04:34:02
【问题描述】:

我正在尝试创建一个可以继承的 NotifyIcon,以便我可以添加自己的属性/等。通过查看别人写的一个组件类,我已经取得了一些进展,如下图所示,并且可以将组件拖入表单中。老实说,我对自己在做什么一无所知,而且互联网上似乎根本没有任何有用的教程。

在您看到的PrepareIcon 方法中,弹出的消息框显示为空白,即使我已尝试更改设计器默认值notifyIconInheritable1。我在设计器中看到 NotifyIcon 出现,所以我完全不知道它是如何工作的。

问题是;这有什么问题或我做错了什么,我是在浪费时间,根本不应该尝试这个吗?

namespace AwesomeNotifyIcon
{
    [DefaultProperty("Text"), DefaultEvent("Click"), Description("Displays an icon in the notification area, on the right side of the Windows taskbar, during run time")]
    public partial class NotifyIconInheritable : Component
    {
        private NotifyIcon notifyIcon;

        public NotifyIconInheritable()
        {
            //PrepareIcon();
            InitializeComponent();
        }

        public NotifyIconInheritable(IContainer container)
        {
            if (container != null)
            {
                container.Add(this);
            }
            PrepareIcon();
            InitializeComponent();
        }

        [Category("Appearance"), Description("The icon to associate with the balloon ToolTip."), DefaultValue(ToolTipIcon.None)]
        public ToolTipIcon BalloonTipIcon { get; set; }

        [Category("Appearance"), Description("The text to associate with the balloon ToolTip.")]
        public string BalloonTipText { get; set; }

        [Category("Appearance"), Description("The title of the balloon ToolTip.")]
        public string BalloonTipTitle { get; set; }

        [Category("Appearance"), Description("The icon to display in the system tray.")]
        public Icon Icon { get; set; }

        [Category("Appearance"), Description("The text that will be displayed when the mouse hovers over the icon.")]
        public string Text { get; set; }

        [Category("Behaviour"), Description("The shortcut menu to show when the user right-clicks the icon.")]
        public ContextMenuStrip ContextMenuStrip { get; set; }

        [Category("Behaviour"), Description("Determines whether the control is visible or hidden."), DefaultValue(false)]
        public bool Visible { get; set; }

        [Category("Data"), Description("User-defined data associated with the object.")]
        public object Tag { get; set; }

        [Category("Action"), Description("Occurs when the component is clicked.")]
        public event EventHandler Click;

        private void PrepareIcon()
        {
            notifyIcon = new NotifyIcon();
            notifyIcon.Dispose();
            MessageBox.Show(this.Text);

            if (Click != null)
                notifyIcon.Click += Click;
        }
    }
}

这是在设计器中看到的属性:

http://cl.ly/1vIF/content http://cl.ly/1vIF/content

【问题讨论】:

  • 很抱歉,您的问题到底是什么?

标签: c# winforms components notifyicon


【解决方案1】:

当您使用设计器添加控件时,它会生成如下所示的代码:

NotifyIconInheritable icon = new NotifyIconInheritable();
icon.Text = "Some text";
parent.Controls.Add(icon);

您可以看到,直到构造函数运行之后,您的属性才被设置。所以,当您调用PrepareIcon 时,Text 确实为空。

其他建议:正如 Henk 所说,您真的不应该在代码中此时调用 Dispose()。您也不应该显示来自构造函数的消息框,尽管希望这只是为了您的测试目的。最后,因为您从构造函数调用PrepareIcon,所以.Click 将始终是null

【讨论】:

  • 更重要的是,任何设计器生成的代码都会添加到组件的InitializeComponent 方法中。在该方法调用之后,他应该可以安全地调用PrepareIcon
  • PrepareIcon() 移动到InitializeComponent() 之后似乎并没有改变消息框的输出。
  • 你没有考虑更大的图景。 InitializeComponent 方法适用于当前组件,在设计器中设置任何子控件属性,这些子控件的更改将发生在这里。 NotifyIconInheritable 的实例被添加到控件树中下一层的另一个控件中,并且它在该父控件的 InitializeComponent 方法中,其中设置了 Text 属性。
  • 所以在这种情况下,在 this 控件的 InitializeComponent 方法之后移动 PrepareIcon 仍然会在设置属性之前调用它。您可能需要考虑将 PrepareIcon 调用移到控件事件生命周期的后期,可能在 OnPaint 之前或附近的某个地方?
  • 组件是否有OnPaint事件?
猜你喜欢
  • 1970-01-01
  • 2011-12-31
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
相关资源
最近更新 更多