0

Possible Duplicate:
Panel not getting focus

im test the Panel control in winform, and I got into a problem.

i have this 2 events that i added to the panel but non of them fire:

 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);
    }

i got a another control on the Form to test the focus(a button).

why onFocus and lostFocus dosnt fire?

(sory for my english)

Community
  • 1
  • 1
samy
  • 1,949
  • 6
  • 39
  • 64

2 Answers2

1

Following is a selectable panel (inherited from regular panel)

Got from Panel not getting focus

Try this. Add this class in your project. Just changing namespace yourApplicaionName. Compile your project. Then you would see selectablePanel in your toolbox. You can use it instead of normal panel. Hope you will be able to get focus on this panel

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);
            }
        }
    }
}
Community
  • 1
  • 1
Sami
  • 8,168
  • 9
  • 66
  • 99
  • This looks mostly copied from [Hans Passant](http://stackoverflow.com/a/3562449/719186). You should attribute the code to him. – LarsTech Oct 26 '12 at 17:25
  • Yes. Thanks @LarsTech ... I had lost the link. Otherwise just sharing the link was enough :) – Sami Oct 27 '12 at 05:01
0

First lostFocus will set the height to 1 and the onGotFocus will do multiplying 1*1 which is 1 effectively not changing anything.

Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
  • you are right, and i just tried it with diffrent code. but is still dosnt work becuse, panles give focus to the first child add'd to them. check Ic link – samy Oct 26 '12 at 16:42