Focus not working the way I would expect it.

  • Thread starter Thread starter fhunter
  • Start date Start date
F

fhunter

Hi,
I have a custom control which contains another custom
control.

I am writting something like this:

public LonLatDialog(int aWidth,int aHeight)
{
...
Controls.Add(mDropDownList);
mDropDownList.Hide();
Focus();
}

When I do this and I call Focus(). I would have expected
the control LonLatDialog to get focus and for
this.Focused to be true. But this is not the case
this.Focused never gets set to true and the focus remains
on mDropDownList.

What am I doing incorrectly ?

Thank you
 
I have more info.
If I comment out Controls.Add(mDropDownList) then the
Focus works fine.

I also tried, living the line in but removing eveyrthing
from the Control contructor, essentially making a empty
control (incase I was doing soemthing strange there). No
luck. As soon as I add a control the focus never comes
back to the parent control. How can I get focus back?
 
Yep, looks like a problem here. As a work around, have you tried using the
Parent property to hide/unhide the control? This would allow the
LonLatDialog() to get focus when you didn't want poeple to see/use the
mDropDownList control.

Also, please verify that this code reproduces the problem you're seeing and
I'll enter a report for this issue:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Threading;

public class Test : Form
{
protected override void OnLoad(EventArgs e)
{
this.Text = "Test";

myControlA a = new myControlA();
a.Parent = this;
a.Bounds = new Rectangle(10, 10, 200, 200);
}

public static void Main()
{
Application.Run(new Test());
}
}

public class myControlA : myControlBase
{
public myControlA() : base()
{
this.BackColor = Color.Green;

myControlB b = new myControlB();
b.Bounds = new Rectangle(50, 50, 100, 50);
this.Controls.Add(b);
b.Hide();
this.Focus();
}
}

public class myControlB : myControlBase
{
public myControlB() : base()
{
this.BackColor = Color.Yellow;
}
}

public class myControlBase : Control
{
public myControlBase()
{
this.Text = "no focus";
}

protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(this.BackColor),
this.Bounds);
e.Graphics.DrawString(this.Text, this.Font, new
SolidBrush(this.ForeColor), 10, 10);
}

protected override void OnGotFocus(EventArgs e)
{
this.Text = "focus";
this.Invalidate();
}

protected override void OnLostFocus(EventArgs e)
{
this.Text = "no focus";
this.Invalidate();
}
}


NETCF Beta FAQ's -- http://www.gotdotnet.com/team/netcf/FAQ.aspx
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top