Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.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# 设置x&;工具提示上的y。是否以某种方式设置持续时间?_C#_Winforms_Tooltip - Fatal编程技术网

C# 设置x&;工具提示上的y。是否以某种方式设置持续时间?

C# 设置x&;工具提示上的y。是否以某种方式设置持续时间?,c#,winforms,tooltip,C#,Winforms,Tooltip,当我调用ToolTip.Show()时,如下所示 ToolTip.Show(Message, MyControl); 一切都很完美,当MyControl失去焦点时,它会显示并消失。但是,工具提示的位置在MyControl上,因此我想添加一个偏移量 ToolTip.Show(Message,MyControl,10,-20); 工具提示确实按照我想要的方式定位,它在鼠标悬停时显示,但在MyControl失去焦点时不再消失。这种行为类似于将持续时间设置得非常高 当我查看ToolTip.Show(

当我调用ToolTip.Show()时,如下所示

ToolTip.Show(Message, MyControl);
一切都很完美,当MyControl失去焦点时,它会显示并消失。但是,工具提示的位置在MyControl上,因此我想添加一个偏移量

ToolTip.Show(Message,MyControl,10,-20);
工具提示确实按照我想要的方式定位,它在鼠标悬停时显示,但在MyControl失去焦点时不再消失。这种行为类似于将持续时间设置得非常高

当我查看ToolTip.Show()定义时,调用它的一种方法如下:

public void Show(string text, IWin32Window window, int x, int y);
那么,当我只添加x&y偏移量而不触及持续时间时,工具提示如何突然停止消失呢

以下是完整代码:

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

namespace WFA
{
  public class Form1 : Form
  {
    public Form1()
    {
        UC UC = new UC();
        this.Controls.Add(UC);
    }
  }

  public class UC : UserControl
  {
    String Message = "Hello!";
    PictureBox MyControl = new PictureBox();
    ToolTip ToolTip = new ToolTip();

    public UC()
    {
        MyControl.ImageLocation = "http://i.stack.imgur.com/CR5ih.png";
        MyControl.Location = new Point(100, 100);
        this.Controls.Add(MyControl);
        MyControl.MouseHover += ShowToolTip;
    }

    private void ShowToolTip(object sender, EventArgs e)
    {
        ToolTip.Show(Message, MyControl,10,-20); // this will never disappear??
      //ToolTip.Show(Message, MyControl); // this disappears after leaving
    }
  }
}
你应该使用
Show(字符串文本、iwin32窗口、int x、int y、int持续时间)

默认情况下,如果未指定任何“超时”,则仅当父窗体被停用时,工具提示才会隐藏

如果要在鼠标离开控件时隐藏工具提示,必须通过调用
tooltip.hide
手动隐藏

public UC()
{
    BorderStyle = BorderStyle.FixedSingle;
    MyControl.ImageLocation = "http://i.stack.imgur.com/CR5ih.png";
    MyControl.Location = new Point(100, 100);
    this.Controls.Add(MyControl);
    MyControl.MouseHover += ShowToolTip;

    //Subscribe MouseLeave and hide the tooltip there
    MyControl.MouseLeave += MyControl_MouseLeave;
}

void MyControl_MouseLeave(object sender, EventArgs e)
{
    ToolTip.Hide(MyControl);
}

为什么?我不想更改持续时间,根据定义,我不需要..调用
工具提示时,
MyControl
是否由表单作为父项。显示
?它位于加载到外部程序状态栏的UserControl中如何将控件添加到外部程序?那是插件吗?或者如何?你能发布一个简短但完整的示例来演示这个问题吗?嘿,Sriram,这没关系,我添加了一些代码来演示这个问题。嘿,Sriram,但是在我使用ToolTip.Show(Message,MyControl)的情况下,我也没有指定超时,但它仍然可以完美地工作,而不必订阅MouseLeave?“所以我还是不明白?”巴斯蒂安,这很复杂,我的朋友。当您不传递位置时,使用代码,工具提示将随鼠标移动。每个重载的行为都不同。当您通过该位置时,工具提示将不会跟随鼠标。如果你想让它跟随你,你需要隐藏和显示它。有那么多复杂的事情。每当我使用工具提示时,我都会手动隐藏它,这就是我的建议。