2011-08-29 171 views
0

我有一個RichTextBox和兩個按鈕的用戶控件。我試圖在ToolStripButton上點擊ToolStripDropDown。我使用ToolStripControlHost將我的控件放在ToolStripDrowDown上。當我在窗體工具欄上單擊ToolStripButton時,我會在某個位置顯示下拉菜單,並將焦點放在ToolStripControlHost控件上。鼠標指針保持在ToolStripButton之上,遊標位於RichTextBox上。但是當我開始編輯RichTextBox鼠標指針消失時,只有當它不在矩形中時才能看到它。我該如何解決它?在ToolStripDropDown上編輯文本框時,鼠標光標消失

這裏是我的代碼:

private void toolBtnNote_Click(object sender, EventArgs e) 
{ 

    dropDownClosed = false; 
    noteChanged = false; 

    tsdd = new ToolStripDropDown(); 
    this.tsdd.Opened += new EventHandler(tsdd_Opened); 
    this.tsdd.AutoSize = true; 

    NoteEdit ne = new NoteEdit(); 
    ne.NoteText = note ?? ""; 
    // appears when user clicks first button at my control 
    ne.OkClick += new NoteEdit.NoteEditEvent(ne_OkClick); 
    // appears when user clicks second button at my control 
    ne.CancelClick += new NoteEdit.NoteEditEvent(ne_CancelClick); 

    this.tbh = new ToolStripControlHost(ne, "noteEdit"); 
    this.tbh.Padding = new Padding(0); 
    this.tbh.AutoSize = false; 
    this.tbh.Size = ne.Size; 

    this.tsdd.Items.Add(tbh); 
    this.tsdd.Padding = new Padding(0); 
    this.tsdd.Closing += new ToolStripDropDownClosingEventHandler(tsdd_Closing); 
    // show toolstripdrowdown at specific position at DataGridView   
    this.tsdd.Show(dgvMarks, cellRect.Location + new Size(0, cellRect.Height)); 

    while (!this.dropDownClosed) 
    { 
     Application.DoEvents(); 
    } 

    if(noteChanged) {...} 
} 

void ne_CancelClick() 
{ 
    tsdd.Close(); 
} 

void ne_OkClick() 
{ 
    noteChanged = true; 
    tsdd.Close(); 
} 

void tsdd_Opened(object sender, EventArgs e) 
{ 
    tbh.Focus(); 
} 

void tsdd_Closing(object sender, ToolStripDropDownClosingEventArgs e) 
{ 
    dropDownClosed = true; 
} 

回答

0

我找到了解決方案。不是最好的,但它的工作。我只是模擬點擊在RichTextBox與WinApi像這樣:

public class WinApiHelper 
{ 
    private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002; 
    private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004; 

    [DllImport("user32.dll")] 
    private static extern void mouse_event(
     UInt32 dwFlags, // motion and click options 
     UInt32 dx, // horizontal position or change 
     UInt32 dy, // vertical position or change 
     UInt32 dwData, // wheel movement 
     IntPtr dwExtraInfo // application-defined information 
     ); 

    public static void SendClick(Point location) 
    { 
     Cursor.Position = location; 
     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr()); 
     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr()); 
    }  
} 

private void toolBtnNote_Click(object sender, EventArgs e) 
{ 
     dropDownClosed = false; 
     noteChanged = false; 

     dgvMarks.Focus(); 
     tsdd = new ToolStripDropDown(); 
     this.tsdd.Opened += new EventHandler(tsdd_Opened); 
     this.tsdd.AutoSize = true; 

     NoteEdit ne = new NoteEdit(); 
     ne.NoteText = note ?? ""; 
     ne.OkClick += new NoteEdit.NoteEditEvent(ne_OkClick); 
     ne.CancelClick += new NoteEdit.NoteEditEvent(ne_CancelClick); 

     this.tbh = new ToolStripControlHost(ne, "noteEdit"); 
     this.tbh.Padding = new Padding(0); 
     this.tbh.AutoSize = false; 
     this.tbh.Size = ne.Size; 

     this.tsdd.Items.Add(tbh); 
     this.tsdd.Padding = new Padding(0); 
     this.tsdd.Closing += new ToolStripDropDownClosingEventHandler(tsdd_Closing); 

     this.tsdd.Show(dgvMarks, cellRect.Location + new Size(0, cellRect.Height)); 
     // emulate click on richtextbox at dropdown 
     WinApiHelper.SendClick(tsdd.Location + new Size(ne.Rtb.Location) + new Size(5, 5)); 

     while (!this.dropDownClosed) 
     { 
      Application.DoEvents(); 
     } 

     if (noteChanged) 
     {...} 
} 
0

這是鼠標光標當你開始編輯一個文本框,或RichTextBox中隱藏正常的行爲。當你移動鼠標時它應該被恢復。您還應該注意,如果您嘗試在TextChanged中調用它或類似的名稱,調用Cursor.Show將不會產生效果。

+0

感謝您的要求。是的,它可以恢復,但僅限於窗體顯示矩形。所以,當鼠標在我的表單上時,光標消失。我需要點擊我的表格來顯示光標。奇怪的是,當我從ContextMenuStrip項目點擊顯示相同的toolstripdropdown(相同的代碼)時,一切都正常,並且當我移動鼠標時,光標在表單上結束。只有當我從ToolBar按鈕調用toolstripdropdown時纔會出現問題。看起來焦點停留在ToolStripButton。 – mao