re-parenting a user control causes tooltips to remember original form

T

timnels

I have an issue where I have a user control that is launched into a
floating form. At some point later, I allow the user to "unfloat" the
user control by reparenting it on a split container in another form.
Problem is if I wake a tooltip when the window is floated, and then try
the same thing when it is reparented, the app crashes with " Cannot
access a disposed object.Object name: 'Form'.

Presumably, this is a result of the tooltips still keeping a reference
to the original form (container).

I know that tooltips have to be found using reflection and the controls
IContainer, but I am not sure how to go about this.

Is there a way to "reorient" all the tooltips to the new container?
 
A

Architect

Hi,

To "unhook" a tooltip from a control call:

tooltip.SetToolTip(control, string.Empty);

To add it back:

tooltip.SetToolTip(control, "Your tooltip text");


You have to remove tooltip before you destroy the form and add it back
when you create it again.

Regards,
Valentin Ivanov
 
T

timnels

Nope. I don't have a handle to the tooltip. I have a handle to the
form. I need to iterate the controls and their components and find
(using reflection since components are a private variable) the tool
tips.
 
T

timnels

BTW... the ToolTip is on a ToolStripItem and it seems to hold a
reference to the old form.
 
T

timnels

Smallest example of issue below. Click the "open" button, hover over
the menu item to see the tool tip, click the "copy" button... now hover
over the menu item on the new form.... boom exception

--------- CUT HERE ----

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ToolTipIssue
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new InitialForm());
}
}
public class InitialForm : Form
{
public InitialForm()
{
Button b = new Button();
b.Text = "Open Form";
Controls.Add(b);
b.Click += new EventHandler(b_Click);
}

void b_Click(object sender, EventArgs e)
{
MyForm f = new MyForm();
f.Show();
}
}
public class MyForm : Form
{
Panel p;
public MyForm()
{
p = new Panel();
ToolStrip t = new ToolStrip();
ToolStripButton b = new ToolStripButton();
b.Text = "Hello";
b.ToolTipText = "Hello ToolTip";
t.Items.Add(b);
p.BorderStyle = BorderStyle.Fixed3D;
p.Controls.Add(t);
Controls.Add(p);

Button bt = new Button();
bt.Text = "Copy";
bt.Click += new EventHandler(b_Click);
bt.Location = new System.Drawing.Point(0, 100);
Controls.Add(bt);
}

void b_Click(object sender, EventArgs e)
{
Form f = new Form();
f.Controls.Add(p);
Close();
f.Show();
}
}
}
 
A

Architect

Hi,

The example doesn't show the problem because you are closing main form
before showing the second form which will lead to application end.
But even in this example tooltip is not tied to a form so it should be fine
to move panel around.

You can try it by commenting "Close();" call.

Regards,
Valentin Ivanov.



Smallest example of issue below. Click the "open" button, hover over
the menu item to see the tool tip, click the "copy" button... now hover
over the menu item on the new form.... boom exception

--------- CUT HERE ----

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ToolTipIssue
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new InitialForm());
}
}
public class InitialForm : Form
{
public InitialForm()
{
Button b = new Button();
b.Text = "Open Form";
Controls.Add(b);
b.Click += new EventHandler(b_Click);
}

void b_Click(object sender, EventArgs e)
{
MyForm f = new MyForm();
f.Show();
}
}
public class MyForm : Form
{
Panel p;
public MyForm()
{
p = new Panel();
ToolStrip t = new ToolStrip();
ToolStripButton b = new ToolStripButton();
b.Text = "Hello";
b.ToolTipText = "Hello ToolTip";
t.Items.Add(b);
p.BorderStyle = BorderStyle.Fixed3D;
p.Controls.Add(t);
Controls.Add(p);

Button bt = new Button();
bt.Text = "Copy";
bt.Click += new EventHandler(b_Click);
bt.Location = new System.Drawing.Point(0, 100);
Controls.Add(bt);
}

void b_Click(object sender, EventArgs e)
{
Form f = new Form();
f.Controls.Add(p);
Close();
f.Show();
}
}
}

BTW... the ToolTip is on a ToolStripItem and it seems to hold a
reference to the old form.
 
T

timnels

I beg to differ. The start up form is "InitialForm" it never gets
closed. I ran this and it reproduces the error.

Hi,

The example doesn't show the problem because you are closing main form
before showing the second form which will lead to application end.
But even in this example tooltip is not tied to a form so it should be fine
to move panel around.

You can try it by commenting "Close();" call.

Regards,
Valentin Ivanov.

Smallest example of issue below. Click the "open" button, hover over
the menu item to see the tool tip, click the "copy" button... now hover
over the menu item on the new form.... boom exception
--------- CUT HERE ----
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ToolTipIssue
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new InitialForm());
}
}
public class InitialForm : Form
{
public InitialForm()
{
Button b = new Button();
b.Text = "Open Form";
Controls.Add(b);
b.Click += new EventHandler(b_Click);
}
void b_Click(object sender, EventArgs e)
{
MyForm f = new MyForm();
f.Show();
}
}
public class MyForm : Form
{
Panel p;
public MyForm()
{
p = new Panel();
ToolStrip t = new ToolStrip();
ToolStripButton b = new ToolStripButton();
b.Text = "Hello";
b.ToolTipText = "Hello ToolTip";
t.Items.Add(b);
p.BorderStyle = BorderStyle.Fixed3D;
p.Controls.Add(t);
Controls.Add(p);
Button bt = new Button();
bt.Text = "Copy";
bt.Click += new EventHandler(b_Click);
bt.Location = new System.Drawing.Point(0, 100);
Controls.Add(bt);
}
void b_Click(object sender, EventArgs e)
{
Form f = new Form();
f.Controls.Add(p);
Close();
f.Show();
}
}
}
 
A

Architect

Yes, you right. I was modifying my existing sandbox solution and used
"MyForm" in the Application.Run call.
Anyways I was able to reproduce the issue. In my case it gives me:

FatalExecutionEngineError was detected
Message: The runtime has encountered a fatal error. The address of the error
was at 0x7f4bb7a3, on thread 0x1f94. The error code is 0xc0000005. This
error may be a bug in the CLR or in the unsafe or non-verifiable portions of
user code. Common sources of this bug include user marshaling errors for
COM-interop or PInvoke, which may corrupt the stack.


I would say that it is a real bug in the CLR. I would report it to
Microsoft.
Meanwhile I'll try to help you and look if there is a workaround.

Regards,
Valentin Ivanov.


I beg to differ. The start up form is "InitialForm" it never gets
closed. I ran this and it reproduces the error.

Hi,

The example doesn't show the problem because you are closing main form
before showing the second form which will lead to application end.
But even in this example tooltip is not tied to a form so it should be
fine
to move panel around.

You can try it by commenting "Close();" call.

Regards,
Valentin Ivanov.

Smallest example of issue below. Click the "open" button, hover over
the menu item to see the tool tip, click the "copy" button... now hover
over the menu item on the new form.... boom exception
--------- CUT HERE ----
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ToolTipIssue
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new InitialForm());
}
}
public class InitialForm : Form
{
public InitialForm()
{
Button b = new Button();
b.Text = "Open Form";
Controls.Add(b);
b.Click += new EventHandler(b_Click);
}
void b_Click(object sender, EventArgs e)
{
MyForm f = new MyForm();
f.Show();
}
}
public class MyForm : Form
{
Panel p;
public MyForm()
{
p = new Panel();
ToolStrip t = new ToolStrip();
ToolStripButton b = new ToolStripButton();
b.Text = "Hello";
b.ToolTipText = "Hello ToolTip";
t.Items.Add(b);
p.BorderStyle = BorderStyle.Fixed3D;
p.Controls.Add(t);
Controls.Add(p);
Button bt = new Button();
bt.Text = "Copy";
bt.Click += new EventHandler(b_Click);
bt.Location = new System.Drawing.Point(0, 100);
Controls.Add(bt);
}
void b_Click(object sender, EventArgs e)
{
Form f = new Form();
f.Controls.Add(p);
Close();
f.Show();
}
}
}
On Jan 3, 11:31 am, (e-mail address removed) wrote:
BTW... the ToolTip is on a ToolStripItem and it seems to hold a
reference to the old form.
(e-mail address removed) wrote:
Nope. I don't have a handle to the tooltip. I have a handle to the
form. I need to iterate the controls and their components and find
(using reflection since components are a private variable) the tool
tips.
To "unhook" a tooltip from a control call:
tooltip.SetToolTip(control, string.Empty);
To add it back:
tooltip.SetToolTip(control, "Your tooltip text");
You have to remove tooltip before you destroy the form and add it
back
when you create it again.
Regards,
Valentin Ivanov
On Jan 3, 9:08 am, (e-mail address removed) wrote:
I have an issue where I have a user control that is launched
into a
floating form. At some point later, I allow the user to
"unfloat"
the
user control by reparenting it on a split container in another
form.
Problem is if I wake a tooltip when the window is floated, and
then
try
the same thing when it is reparented, the app crashes with "
Cannot
access a disposed object.Object name: 'Form'.
Presumably, this is a result of the tooltips still keeping a
reference
to the original form (container).
I know that tooltips have to be found using reflection and the
controls
IContainer, but I am not sure how to go about this.
Is there a way to "reorient" all the tooltips to the new
container?
 
A

Architect

Ok,

The problem lies in the ToolStrip control (too bad we don't have access to
the tooltip).
The only way to solve it is to recreate toolstrip.

Add a method to you user control which will
1. remove existing toolstrip
2. create new toolstrip
3. readd all items to the new toolstrip
4. add new toolstrip

Call that method each time you transfer your user control.

Regards,
Valentin Ivanov.

Architect said:
Yes, you right. I was modifying my existing sandbox solution and used
"MyForm" in the Application.Run call.
Anyways I was able to reproduce the issue. In my case it gives me:

FatalExecutionEngineError was detected
Message: The runtime has encountered a fatal error. The address of the
error was at 0x7f4bb7a3, on thread 0x1f94. The error code is 0xc0000005.
This error may be a bug in the CLR or in the unsafe or non-verifiable
portions of user code. Common sources of this bug include user marshaling
errors for COM-interop or PInvoke, which may corrupt the stack.


I would say that it is a real bug in the CLR. I would report it to
Microsoft.
Meanwhile I'll try to help you and look if there is a workaround.

Regards,
Valentin Ivanov.


I beg to differ. The start up form is "InitialForm" it never gets
closed. I ran this and it reproduces the error.

Hi,

The example doesn't show the problem because you are closing main form
before showing the second form which will lead to application end.
But even in this example tooltip is not tied to a form so it should be
fine
to move panel around.

You can try it by commenting "Close();" call.

Regards,
Valentin Ivanov.

message
Smallest example of issue below. Click the "open" button, hover over
the menu item to see the tool tip, click the "copy" button... now
hover
over the menu item on the new form.... boom exception

--------- CUT HERE ----

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ToolTipIssue
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new InitialForm());
}
}
public class InitialForm : Form
{
public InitialForm()
{
Button b = new Button();
b.Text = "Open Form";
Controls.Add(b);
b.Click += new EventHandler(b_Click);
}

void b_Click(object sender, EventArgs e)
{
MyForm f = new MyForm();
f.Show();
}
}
public class MyForm : Form
{
Panel p;
public MyForm()
{
p = new Panel();
ToolStrip t = new ToolStrip();
ToolStripButton b = new ToolStripButton();
b.Text = "Hello";
b.ToolTipText = "Hello ToolTip";
t.Items.Add(b);
p.BorderStyle = BorderStyle.Fixed3D;
p.Controls.Add(t);
Controls.Add(p);

Button bt = new Button();
bt.Text = "Copy";
bt.Click += new EventHandler(b_Click);
bt.Location = new System.Drawing.Point(0, 100);
Controls.Add(bt);
}

void b_Click(object sender, EventArgs e)
{
Form f = new Form();
f.Controls.Add(p);
Close();
f.Show();
}
}
}

On Jan 3, 11:31 am, (e-mail address removed) wrote:
BTW... the ToolTip is on a ToolStripItem and it seems to hold a
reference to the old form.

(e-mail address removed) wrote:
Nope. I don't have a handle to the tooltip. I have a handle to
the
form. I need to iterate the controls and their components and find
(using reflection since components are a private variable) the tool
tips.

Hi,

To "unhook" a tooltip from a control call:

tooltip.SetToolTip(control, string.Empty);

To add it back:

tooltip.SetToolTip(control, "Your tooltip text");

You have to remove tooltip before you destroy the form and add it
back
when you create it again.

Regards,
Valentin Ivanov

On Jan 3, 9:08 am, (e-mail address removed) wrote:

I have an issue where I have a user control that is launched
into a
floating form. At some point later, I allow the user to
"unfloat"
the
user control by reparenting it on a split container in another
form.
Problem is if I wake a tooltip when the window is floated, and
then
try
the same thing when it is reparented, the app crashes with "
Cannot
access a disposed object.Object name: 'Form'.

Presumably, this is a result of the tooltips still keeping a
reference
to the original form (container).

I know that tooltips have to be found using reflection and the
controls
IContainer, but I am not sure how to go about this.

Is there a way to "reorient" all the tooltips to the new
container?
 
A

Architect

Hmm, for some reason my last reply is not here.

Anyways. The only way I made it to work is by recreating toolstrip control
each time I "migrate" the panel.

Regards,
Valentin Ivanov.

I beg to differ. The start up form is "InitialForm" it never gets
closed. I ran this and it reproduces the error.

Hi,

The example doesn't show the problem because you are closing main form
before showing the second form which will lead to application end.
But even in this example tooltip is not tied to a form so it should be
fine
to move panel around.

You can try it by commenting "Close();" call.

Regards,
Valentin Ivanov.

Smallest example of issue below. Click the "open" button, hover over
the menu item to see the tool tip, click the "copy" button... now hover
over the menu item on the new form.... boom exception
--------- CUT HERE ----
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ToolTipIssue
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new InitialForm());
}
}
public class InitialForm : Form
{
public InitialForm()
{
Button b = new Button();
b.Text = "Open Form";
Controls.Add(b);
b.Click += new EventHandler(b_Click);
}
void b_Click(object sender, EventArgs e)
{
MyForm f = new MyForm();
f.Show();
}
}
public class MyForm : Form
{
Panel p;
public MyForm()
{
p = new Panel();
ToolStrip t = new ToolStrip();
ToolStripButton b = new ToolStripButton();
b.Text = "Hello";
b.ToolTipText = "Hello ToolTip";
t.Items.Add(b);
p.BorderStyle = BorderStyle.Fixed3D;
p.Controls.Add(t);
Controls.Add(p);
Button bt = new Button();
bt.Text = "Copy";
bt.Click += new EventHandler(b_Click);
bt.Location = new System.Drawing.Point(0, 100);
Controls.Add(bt);
}
void b_Click(object sender, EventArgs e)
{
Form f = new Form();
f.Controls.Add(p);
Close();
f.Show();
}
}
}
On Jan 3, 11:31 am, (e-mail address removed) wrote:
BTW... the ToolTip is on a ToolStripItem and it seems to hold a
reference to the old form.
(e-mail address removed) wrote:
Nope. I don't have a handle to the tooltip. I have a handle to the
form. I need to iterate the controls and their components and find
(using reflection since components are a private variable) the tool
tips.
To "unhook" a tooltip from a control call:
tooltip.SetToolTip(control, string.Empty);
To add it back:
tooltip.SetToolTip(control, "Your tooltip text");
You have to remove tooltip before you destroy the form and add it
back
when you create it again.
Regards,
Valentin Ivanov
On Jan 3, 9:08 am, (e-mail address removed) wrote:
I have an issue where I have a user control that is launched
into a
floating form. At some point later, I allow the user to
"unfloat"
the
user control by reparenting it on a split container in another
form.
Problem is if I wake a tooltip when the window is floated, and
then
try
the same thing when it is reparented, the app crashes with "
Cannot
access a disposed object.Object name: 'Form'.
Presumably, this is a result of the tooltips still keeping a
reference
to the original form (container).
I know that tooltips have to be found using reflection and the
controls
IContainer, but I am not sure how to go about this.
Is there a way to "reorient" all the tooltips to the new
container?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top