Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#获取组件#x27;在运行时是什么值?_C#_Winforms_Components_Notifyicon - Fatal编程技术网

C#获取组件#x27;在运行时是什么值?

C#获取组件#x27;在运行时是什么值?,c#,winforms,components,notifyicon,C#,Winforms,Components,Notifyicon,我正在尝试创建一个可以继承的NotifyIcon,以便添加自己的属性等。通过查看其他人编写的组件类,我取得了一些进展,如下所示,并且可以将组件拖到表单中。老实说,我对自己在做什么一无所知,而且在互联网上似乎没有任何有用的教程 在您看到的PrepareIcon方法中,弹出的消息框显示为空,尽管我已尝试更改设计器默认值notifyiconheritable1。我在designer中看到NotifyIcon出现,所以我完全不知道它是如何工作的 问题是,;这有什么不对,或者我做错了什么,我是不是在浪费时

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

在您看到的
PrepareIcon
方法中,弹出的消息框显示为空,尽管我已尝试更改设计器默认值
notifyiconheritable1
。我在designer中看到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;
        }
    }
}
以下是设计器中显示的属性:


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

NotifyIconInheritable icon = new NotifyIconInheritable();
icon.Text = "Some text";
parent.Controls.Add(icon);
您可以看到,直到构造函数运行之后,才设置属性。因此,在调用
PrepareIcon
时,
文本实际上是空的


其他建议:正如Henk所说,在代码的这一点上,您真的不应该调用
Dispose()
。您也不应该显示来自构造函数的消息框,尽管希望它只是用于测试目的。最后,因为您正在从构造函数调用
PrepareIcon
。很抱歉,单击
将始终为
null

,但您的问题是什么?更重要的是,任何设计器生成的代码都会添加到组件的
InitializeComponent
方法中。在方法调用之后,他应该能够安全地调用
PrepareIcon
。将
PrepareIcon()
移动到
InitializeComponent()
之后,似乎不会更改消息框的输出。您没有考虑更大的问题。
InitializeComponent
方法用于当前组件,在设计器中设置任何子控件属性,这些子控件的更改将在此处发生。
NotifyIconInheritable
的实例被添加到控件树上下一级的另一个控件中,它位于父控件的
InitializeComponent
方法中,在该方法中设置了
Text
属性。因此在本例中,在此控件的
InitializeComponent
方法之后移动
prepareCon
,仍会在设置属性之前调用它。您可能需要考虑的是将<代码> PraveReimix>代码>调用到控件事件生命周期之后的某个地方,可能在<代码> OnPaint <代码>或附近某个地方?一个组件是否有<代码> OnPaint <代码>事件?