Question

I have a special label in my form, that should show in a tooltip some text. The label is declared as private class in the form (nested control), and should "see" the ToolTip control of the parent form.

Here is the code. Surely, I obtains errors here, because the constructor is called before the private control addition in the owner form control collection...

Edit: Is there a possibility do not pass the form1 or the toolTip control in constructor?

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1() 
        {
            this.InitializeComponent();

            FormLabel myFormLabel = new FormLabel("uraaaaa!");

            this.Controls.Add(myFormLabel);

            myFormLabel.Location = new Point(20, 20);
        }

        private class FormLabel : Label
        {
            public FormLabel(string toolTip) : base()
            {
                this.Text = toolTip.ToUpperInvariant();

                (this.FindForm() as Form1).toolTip1.SetToolTip(this, toolTip);
            }
        }
    }
}
Was it helpful?

Solution 3

A temporary solution can be like:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1(){
            this.InitializeComponent();
            FormLabel myFormLabel = new FormLabel("uraaaaa!");
            this.Controls.Add(myFormLabel);
            myFormLabel.Location = new Point(20, 20);
        }

    private class FormLabel : Label
    {
        private string toolTipText;
        public FormLabel(string toolTip) : base() {                
            this.BorderStyle = BorderStyle.FixedSingle;
            this.toolTipText = toolTip.ToUpperInvariant();
        }

        protected override void OnParentChanged(EventArgs e) {
            Form1 f1 = (this.Parent as Form1);
            if (f1 != null)
                f1.toolTip1.SetToolTip(this, this.toolTipText);
        }
    }
}
}

OTHER TIPS

Why not just pass the form into the constructor of FormLabel?

public Form1() 
{
    this.InitializeComponent();
    FormLabel myFormLabel = new FormLabel(this, "uraaaaa!");
    this.Controls.Add(myFormLabel);
    myFormLabel.Location = new Point(20, 20);
}

private class FormLabel : Label
{
    public FormLabel(Form1 form, string toolTip) : base()
    {
        this.Text = toolTip.ToUpperInvariant();
        form.toolTip1.SetToolTip(this, toolTip);
    }
}

I would expect that to work... if it doesn't, please give details of what errors you're seeing. I'm assuming there's a good reason to do this in real life - it feels a bit convoluted to me at the moment.

You can use any instance of ToolTip to set a tooltip - You may find it easier to create a new instance of ToolTip, rather than re-using the one on the Form:

public FormLabel(string toolTip) : base()
{
    this.Text = toolTip.ToUpperInvariant();

    ToolTip myToolTip = new ToolTip();
    myToolTip.SetToolTip(this, toolTip);
}

Alternatively you could explicitly pass an instance of ToolTip to the control, like this:

public Form1() 
{
    this.InitializeComponent();

    FormLabel myFormLabel = new FormLabel("uraaaaa!", this.toolTip1);
    this.Controls.Add(myFormLabel);
    myFormLabel.Location = new Point(20, 20);
}

private class FormLabel : Label
{
    public FormLabel(string text, ToolTip toolTip) : base()
    {
        this.Text = text.ToUpperInvariant();
        toolTip.SetToolTip(this, text);
    }
}

Does this help clarify things a little?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top