如何更改 WinForm 应用程序 PictureBox 中现有 ToolTip 控件的文本?
我有一个 winform 应用程序,它具有 PictureBoxes
的动态编号(基于数据库值)。每个 P-Box 都有一个 Tooltip
控件。
如何更改工具提示文本而不发生任何内存泄漏?现在,我有以下代码,但它正在泄漏内存=>以前的 ToolTip 控件不会被 GC 处理。
顺便说一句,这是一个后台线程,正在尝试更新主 UI 线程......
if (pictureBox == null || !pictureBox.IsHandleCreated) {
continue;
}
Action setTooltipAndImage = () => {
var toolTip = new ToolTip();
GameServer tempGameFile = gameServer;
toolTip.SetToolTip(pictureBox, string.Format(...));
pictureBox.Image = Resources.RedButton;
};
if (pictureBox.InvokeRequired) {
pictureBox.Invoke(setTooltipAndImage);
} else {
setTooltipAndImage();
}
正如我所说 - 这可以工作,但它正在泄漏。
有人有什么建议吗?
I have a winform application which has a dynamic number (based on a database value) of PictureBoxes
. Each P-Box has a Tooltip
control.
How can I change the ToolTip Text without having any memory leaks? Right now, I've got the following code, but it's leaking memory => the previous ToolTip controls are not getting GC'd.
BTW, this is a background thread that is trying to update the main UI thread....
if (pictureBox == null || !pictureBox.IsHandleCreated) {
continue;
}
Action setTooltipAndImage = () => {
var toolTip = new ToolTip();
GameServer tempGameFile = gameServer;
toolTip.SetToolTip(pictureBox, string.Format(...));
pictureBox.Image = Resources.RedButton;
};
if (pictureBox.InvokeRequired) {
pictureBox.Invoke(setTooltipAndImage);
} else {
setTooltipAndImage();
}
As I said - this works but it's leaking.
Anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要每次都创建新的工具提示。使用可视化设计器将工具提示添加到表单,就像添加任何其他控件或组件一样。每次在表单的工具提示上调用
toolTip.SetToolTip(...)
。当表单被释放时,工具提示也会被释放。Don't create a new ToolTip each time. Add a ToolTip to the form using the visual designer, like you would for any other control or component. Call
toolTip.SetToolTip(...)
on the form's tool tip each time. The ToolTip will be disposed when the Form is disposed.是的,您不需要每次都创建一个新的工具提示,单个工具提示即可。如果您不知道需要多少个工具提示,也没有问题,因为如果只有一个工具提示,例如
toolTip1
,那么您每次想要更改工具提示标题和控件时都可以使用以下命令一些事件。 每个表单只需要一个 ToolTip 实例。Yes, you do not need to create a new ToolTip each time, a single ToolTipwill do. There is no issue if you do not know how many ToolTips you want, because if there is only one ToolTip say
toolTip1
, then you can use the following every time you want to change the ToolTip caption and control on some event. You only need one ToolTip instance per form.每个表单只需要一个 ToolTip 实例。
toolTip.SetToolTip(control,caption)
- 可以与多个控件一起使用,您可以为每个控件设置标题toolTip.ToolTipTitle
- 设置工具提示标题,该标题是与工具提示绑定的所有控件的标题,例如:
toolTip.SetToolTip(control, caption)
- can use with many control, you can set caption for each controltoolTip.ToolTipTitle
- set tool tip title, the title is one for all control bonded with tool tipfor example :