2012-10-26 91 views
0

可能重複:
Panel not getting focusWinForm的。調整面板的onfocus

IM測試在winform面板控制和我有一個問題。

我有這2個活動,我添加到面板,但其中不火:

private void panel_onFocus(object sender, EventArgs e) 
    { 
     panel1.Size = new Size(panel1.Size.Width, panel1.Size.Height * panel1.Size.Height); 
    } 

    private void panel_lostFocus(object sender, EventArgs e) 
    { 
     panel1.Size = new Size(panel1.Size.Width, panel1.Size.Height/panel1.Size.Height); 
    } 

我得到了表格,測試的重點(按鈕)上另一個控制。

爲什麼onFocus和lostFocus dosnt會失火?

(索裏,我的英語)

+0

嗯......我不知道(它的一段時間),但面板甚至可以有焦點? – musefan

+0

thx我不知道,小組不能確定焦點。 – samy

回答

0

首先引發LostFocus將高度設置爲1,onGotFocus會做乘法1 * 1是1有效地不改變任何東西。

+0

你是對的,我只是試着用不同的代碼。但仍然是工作理由,Panles把重點放在第一個孩子身上。檢查IC鏈接 – samy

1

以下是可選擇的面板(來自常規面板繼承)

接到Panel not getting focus

試試這個。在你的項目中添加這個類。只需更改namespace yourApplicaionName。編譯你的項目。然後你會在你的工具箱中看到selectablePanel。您可以使用它而不是普通面板。希望你能夠專注於這個面板

using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace yourApplicaionName 
{ 
    class selectablePanel : Panel 
    { 
     public selectablePanel() 
     { 
      this.SetStyle(ControlStyles.Selectable, true);    
      ResizeRedraw = true; 
      this.TabStop = true; 
     } 

     protected override void OnMouseDown(MouseEventArgs e) 
     { 
      this.Focus(); 
      base.OnMouseDown(e); 
     } 

     protected override bool IsInputKey(Keys keyData) 
     { 
      if (keyData == Keys.Up || keyData == Keys.Down) return true; 
      if (keyData == Keys.Left || keyData == Keys.Right) return true; 
      return base.IsInputKey(keyData); 
     } 

     protected override void OnEnter(EventArgs e) 
     { 
      this.Invalidate(); 
      base.OnEnter(e); 
     } 

     protected override void OnLeave(EventArgs e) 
     { 
      this.Invalidate(); 
      base.OnLeave(e); 
     } 

     protected override void OnPaint(PaintEventArgs pe) 
     { 
      base.OnPaint(pe); 
      if (this.Focused) 
      { 
       var rc = this.ClientRectangle; 
       rc.Inflate(-2, -2); 
       ControlPaint.DrawFocusRectangle(pe.Graphics, rc); 
      } 
     } 
    } 
} 
+0

這看起來大部分從[Hans Passant](http://stackoverflow.com/a/3562449/719186)複製。你應該把代碼歸功於他。 – LarsTech

+0

是的。謝謝@LarsTech ...我失去了鏈接。否則只是分享鏈接就足夠了:) – Sami