如何在鼠标移动时显示文本框c#

时间:2016-05-04 07:20:52

标签: c# winforms

我想在窗体上显示鼠标悬停的继承文本框控件。但是不显示文本。以下是我的代码。

        private ChartCalloutBox m_calloutbox = null;
        public Form2()
        {
            InitializeComponent();
            this.MouseMove += Form2_MouseMove;

        }

        void Form2_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_calloutbox == null)
            {
                m_calloutbox = new ChartCalloutBox();
            }
            m_calloutbox.Location = e.Location;
            m_calloutbox.Show();
        }

        internal class ChartCalloutBox : TextBox
        {

            public ChartCalloutBox()
            {               
                InitializeComponent();
            }

            private void InitializeComponent()
            {
                this.SuspendLayout();
                this.Location = new System.Drawing.Point(350, 170);
                this.ClientSize = new System.Drawing.Size(130, 40);
                this.Size = new System.Drawing.Size(130, 40);
                this.BackColor = System.Drawing.Color.Black;
                this.ForeColor = System.Drawing.Color.Brown;
                this.Name = "CalloutBox";
                this.Text = "Callout Rect";
                this.ResumeLayout(false);
                // 
            }
        }

有关如何在鼠标悬停时显示文本框的任何帮助。并且文本框的位置应根据鼠标位置进行更改。

谢谢, Bharathi。

1 个答案:

答案 0 :(得分:2)

将控件添加到控件集合中。

代码应该像

        void Form2_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_calloutbox == null)
            {
                m_calloutbox = new ChartCalloutBox();
                this.Controls.Add(m_calloutbox);
            }
            m_calloutbox.Location = e.Location;
        }