2013-07-10 82 views
1

的SelectedItemIndex这里的情景:对齐工具提示智能感知

我工作的一个代码编辑器(的WinForms),我用一个RichTextBox和组件作为智能感知。

当按下 “”在RichTextBox中,智能感知将出现并且其内部的每个对象都有不同的工具提示。

是这样的:
http://oi44.tinypic.com/2lbzu1.jpg

现在

工具提示位置是按照SelectedIndex我想出了这个代码:当智能感知项目达到12以上(需注意,

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); 
       } 
} 

问题智能感知有一个过滤器,过滤文本(startswith),其一直类型Richtextbox像智能感知在Visual Studio) ,它会自动有一个滚动(因为它达到其最大尺寸)01现在,问题是,当使用滚动 工具提示将不会跟随其Selecteditemindex现在。

控制智能感知就像一个列表框。(因为我前面提到的,我使用的组件)

现在我的问题是如何让工具提示始终遵循的智能感知的SelectedItemIndex

回答

3

如果用这个重构你的代码,它将使您更容易了很多。

void SetToolTip(Intellisense intellisenseitem) 
{ 
    toolTip.ToolTipTitle = title; 
    toolTip.Show(text, this, Width + 3, SelectedItemIndex + (15 * selectedItemIndex), 3000); 
} 

一旦滚动条开始移动,您应该使用滚动索引而不是选定项目索引。从理论上说,你根本不应该使用Selected Item Index,而应该使用Selected Item position(我不确定List Box是否向公众公开Selected Item Position,你可能需要挖掘Reflection并获得私人领域所选项目位置)。

编辑

你需要的是:

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); 
} 
+0

任何建议,先生,我该怎么办了滚动的解决方案吗? – Elegiac

+0

+1先生,如果你能帮助我如何解决这个问题的滚动生病接受这个答案 – Elegiac

+0

看到我的编辑您的解决方案。 – Jegan