PropertyGrid steals focus

G

Guest

Hi,

I have a MDI child Form that contains a couple of custom controls (both
Panel based viewers), a DataGrid, and a PropertyGrid.

Try as I might to have the DataGrid receive focus when the MDI child Form is
displayed, the PropertyGrid always grabs it.

I have registered an event handler for the MDI child Form's Load event and
VisibleChanged event and in both have tried to have the DataGrid request
focus and set the CurrentCell to (0,0). This has not worked.

Any ideas?

Thanks.
 
R

Rodger Constandse

Hi,

I've run into a similar issue many times. As far as I can tell, the problem is
that the Form's internal focus setting code runs *after* the Load and
VisibleChanged events are called.

The only solution I have found is to override the SetVisibleCore method in the
Form (MDI child form in your case) and give the focus to the control I want
*after* calling the base class method. Something like this...

protected override SetVisibleCore(bool visible)
{
base.SetVisibleCore(visible);

if (visible)
{
// you can add a check to make sure control is valid if you want
this.myControl.Focus();
}
}

Best regards,

Rodger

Time Management Guide - Make better use of your time
<http://www.TimeThoughts.com/timemanagement.htm>
 
G

Guest

Thanks Roger, that makes sense but still no luck. Maybe my problem is the
code I use to request focus. Using your idea:


protected override void SetVisibleCore(bool visible)
{
base.SetVisibleCore(visible);

if (visible)
{
FocusOnFirstCheckListField();
}
}

private void FocusOnFirstCheckListField()
{
this.dataGridCheckList.Focus();
this.dataGridCheckList.CurrentCell =
new DataGridCell(0, 0);
}
 
R

Rodger Constandse

Hi,

Hmm... The only thing I can think of to do here is to add a handler for the
Enter event on the property grid with some dummy Debug.Write() so you can set a
breakpoint.

Set a breakpoint in the SetVisibleCore method righ were you give the focus to
the data grid, then enable the breakpoing on the PropertyGrid Enter event. This
may show you how the property grid is stealing the focus away from the data grid.


Best regards,

Rodger

Time Management Guide - Make better use of your time
<http://www.TimeThoughts.com/timemanagement.htm>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top