2012-10-30 57 views
2

我想在發生錯誤時顯示氣球提示而不是顯示MessageBox。在按鈕上顯示氣球提示點擊

[注]我不希望它在鼠標懸停上顯示。

我都嘗試,但他們實際上顯示在鼠標懸停提示

toolTip1.SetToolTip(); 
toolTip1.Show(); 

回答

2

您可以使用ToolTip Popup event來檢查是否存在工具提示,如果沒有,則取消它。然後,您可以在驗證過程中設置工具提示,然後顯示它。在這個例子中,我設置了一個定時器,在2秒超時後重置工具提示文本。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     toolTip1.IsBalloon = true; 
     toolTip1.Popup += new PopupEventHandler(toolTip1_Popup); 
     toolTip1.SetToolTip(textBox1, ""); 
    } 

    void toolTip1_Popup(object sender, PopupEventArgs e) 
    { 
     if (toolTip1.GetToolTip(e.AssociatedControl) == "") 
      e.Cancel = true; 

    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     timer1.Stop(); 
     toolTip1.RemoveAll(); 

    } 

    private void textBox1_Validating(object sender, CancelEventArgs e) 
    { 
     int temp; 
     if (!int.TryParse(textBox1.Text, out temp)) 
      showTip("Validation Error", (Control)sender); 

    } 

    private void showTip(string message, Control destination) 
    { 
     toolTip1.Show(message, destination); 
     timer1.Start(); 
    } 
} 
+0

你知道當你打開大寫鎖定在密碼文本框中時出現的氣球嗎?,我想做這樣的事情,我想你想念我。或者我誤解了你。 –

+0

@RuneS我給你的代碼將防止鼠標懸停時顯示工具提示,它將顯示驗證錯誤。就我所要看到的大寫鎖定消息的具體細節而言 –

+0

@MarkHall禁用工具提示而不是在試圖顯示時試圖隱藏工具提示會更好嗎?我認爲ToolTipService.IsEnabled = false會做到這一點。 – MikeKulls

0

出乎我的意料,似乎toolTip1.IsOpen = true,將顯示一個提示,並允許它繼續開放。請注意,您需要提供代碼才能關閉它,因爲無論我做什麼,它都不會在我的計算機上自行消失。