以下是场景:
我正在开发一个代码编辑器(Winforms),我使用一个RichTextBox和一个组件来充当智能感知。
当按下".“在RichTextBox中,将出现智能感知,其中的每个对象都有不同的工具提示。
如下所示:

现在,为了让工具提示位置遵循SelectedIndex,我想出了以下代码:
public void SetToolTip(Intellisense intellisenseitem)
{
if (selectedItemIndex == 0)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex, 3000);
}
if (selectedItemIndex == 1)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + 15, 3000);
}
if (selectedItemIndex == 2)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + 30, 3000);
}
if (selectedItemIndex == 3)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + 45, 3000);
}
if (selectedItemIndex == 4)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + 60, 3000);
}
if (selectedItemIndex == 5)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + 75, 3000);
}
if (selectedItemIndex == 6)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + 90, 3000);
}
if (selectedItemIndex == 7)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + 105, 3000);
}
if (selectedItemIndex == 8)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + 120, 3000);
}
if (selectedItemIndex == 9)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + 135, 3000);
}
if (selectedItemIndex == 10)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + 150, 3000);
}
if (selectedItemIndex == 11)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + 165, 3000);
}
if (selectedItemIndex >= 12) //still needed to fix
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + 165, 3000);
}
}问题是当智能感知项目达到12以上时(请注意,智能感知有一个过滤器来过滤在Richtextbox中键入的文本(startswith),就像visual studio中的智能感知一样),它将自动有一个滚动(因为它达到了最大尺寸),现在的问题是,当使用滚动时,工具提示不会跟随它的Selecteditemindex索引。
control intellisense就像一个列表框(因为我之前提到过它是我使用的一个组件)
现在我的问题是如何让工具提示始终遵循智能感知的SelectedItemIndex。
发布于 2013-07-10 16:52:23
如果你用这个来重构你的代码,它会让你的代码变得更容易。
void SetToolTip(Intellisense intellisenseitem)
{
toolTip.ToolTipTitle = title;
toolTip.Show(text, this, Width + 3, SelectedItemIndex + (15 * selectedItemIndex ), 3000);
}一旦滚动条开始移动,您就应该使用滚动索引而不是所选项目索引。理论上你根本不应该使用Selected Item Index,而应该使用Selected Item position (我不确定列表框将Selected Item Position公开给公众,你可能需要挖掘反射并获取selected item position的私有字段)。
编辑
您需要的是:
void SetToolTip(Intellisense intellisenseitem)
{
toolTip.ToolTipTitle = title;
var x = Width + 3;
// get the rectangle of the selected item, the X, Y position of the rectangle will be relative to parent list box.
var rect = listBox1.GetItemRectangle(listBox1.SelectedIndex);
var y = listBox1.Location.Y + rect.Y; // Add ListBox Y and the Selected Item Y to get the absolute Y.
toolTip.Show(text, this, x, y, 3000);
}https://stackoverflow.com/questions/17565751
复制相似问题