2013-07-10 50 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