2010-08-30 90 views
4

我問密切相關,這一段時間前一個問題: Alternative way to notify the user of an error工具提示氣球顯示位置(錯誤通知)

總之,我試圖找到一個快速簡便的方法來通知錯誤的用戶,而無需使用彈出窗口。

現在我已經實現了這個使用工具提示baloons。問題是,即使我給它一個大概的位置,氣泡的小尖端部分根據消息的大小改變位置(見附圖)。通常,我會使用SetToolTip()併爲其分配一個控件,以便它始終指向該控件。但是,該控件是狀態欄中的標籤或圖像。

private void ShowTooltipBalloon(string title, string msg) 
{ 
    if (this.InvokeRequired) 
    { 
     this.BeginInvoke(new EventHandler(delegate { ShowTooltipBalloon(title,msg); })); 
    } 
    else 
    { 
     ToolTip tt = new ToolTip(); 
     tt.IsBalloon = true; 
     tt.ToolTipIcon = ToolTipIcon.Warning; 
     tt.ShowAlways = true; 
     tt.BackColor = Color.FromArgb(0xFF, 0xFF, 0x90); 
     tt.ToolTipTitle = title; 

     int x = this.Width - lblLeftTarget.Width - lblVersion.Width - toolStripStatusLabel8.Width - 10; 
     int y = this.Height - lblLeftConnectImg.Height - 60; 
     tt.Show(msg, this, x, y, 5000); 
    } 
} 

這是很離譜的要求範圍,但我的老闆是細節上的堅持己見,所以除了解決這一點,我必須解決它快。我需要一些相對容易實現的東西,它不會讓目前正在發佈的軟件「搖擺」。

這就是說,當然我會聽任何建議,不管它是否可以實施。至少我可能會學到一些東西。 alt text

*編輯:看來我的圖像沒有顯示。我不知道這是不是我的電腦。哦...

+1

如果不創建自己的工具提示窗體,則需要控制線條長度(即在50個字符後使用Environment.NewLine)以確保標準寬度。 – 2011-10-11 14:17:05

回答

1

我知道這是一個相當古老的問題,我想我錯過了你的分娩死線近4年... ...但我相信這能解決您遇到的問題:

private void ShowTooltipBalloon(string title, string msg) 
{ 
    if (this.InvokeRequired) 
    { 
     this.BeginInvoke(new EventHandler(delegate { ShowTooltipBalloon(title, msg); })); 
    } 
    else 
    { 
     // the designer hooks up to this.components 
     // so lets do that as well... 
     ToolTip tt = new ToolTip(this.components); 

     tt.IsBalloon = true; 
     tt.ToolTipIcon = ToolTipIcon.Warning; 
     tt.ShowAlways = true; 
     tt.BackColor = Color.FromArgb(0xFF, 0xFF, 0x90); 
     tt.ToolTipTitle = title; 

     // Hookup this tooltip to the statusStrip control 
     // but DON'T set a value 
     // because if you do it replicates the problem in your image 
     tt.SetToolTip(this.statusStrip1, String.Empty); 

     // calc x 
     int x = 0; 
     foreach (ToolStripItem tbi in this.statusStrip1.Items) 
     { 
      // find the toolstrip item 
      // that the tooltip needs to point to 
      if (tbi == this.toolStripDropDownButton1) 
      { 
       break; 
      } 
      x = x + tbi.Size.Width; 
     } 

     // guestimate y 
     int y = -this.statusStrip1.Size.Height - 50; 
     // show it using the statusStrip control 
     // as owner 
     tt.Show(msg, this.statusStrip1, x, y, 5000); 
    } 
}