gaining focus on a text box after moving it

J

jamie

In my code I have a datagrid. When the currentCellchanged event is fired I
draw a textbox overtop of the new currentCell. This works perfectly. I
then call the textbox.focus(); this doesn't work for somereason. I'm
assuming the currentCellchanged event sets focus to the cell after setting
focus to the text box.

Here is the code
Rectangle r = dataGrid1.GetCellBounds(dataGrid1.CurrentCell.RowNumber,
dataGrid1.CurrentCell.ColumnNumber);

r = dataGrid1.RectangleToScreen(r);

this.textBox1.Top = r.Top -56; // -56 gets the spacing exactly over
the grid

this.textBox1.Left = r.Left-1; // more spacing over the grid

this.textBox1.Width = r.Width;

this.textBox1.Height = r.Height;

this.textBox1.Focus();

Any suggestions?

Jamie
 
J

John Socha-Leialoha

In my code I have a datagrid. When the currentCellchanged event is fired I
draw a textbox overtop of the new currentCell. This works perfectly. I
then call the textbox.focus(); this doesn't work for somereason. I'm
assuming the currentCellchanged event sets focus to the cell after setting
focus to the text box.

Here is the code
Rectangle r = dataGrid1.GetCellBounds(dataGrid1.CurrentCell.RowNumber,
dataGrid1.CurrentCell.ColumnNumber);

r = dataGrid1.RectangleToScreen(r);

this.textBox1.Top = r.Top -56; // -56 gets the spacing exactly over
the grid

this.textBox1.Left = r.Left-1; // more spacing over the grid

this.textBox1.Width = r.Width;

this.textBox1.Height = r.Height;

this.textBox1.Focus();

Any suggestions?

Jamie

I'd suggest setting a timer where you have the call to Focus(). Then in
the timer's event handler, call Focus(). This will allow the screen to
update to reflect your changes before you set the focus. See if that
works....

-- John
 
J

jamie

Well having a constantly firing timer is a pretty ugly hack, but it does
work. Thanks
 
J

John Socha-Leialoha

Well having a constantly firing timer is a pretty ugly hack, but it does
work. Thanks

Yes, it's a bit of a hack. What I usually do is disable the timer
immediately in the event handler so it doesn't keep firing.

Now that you have this working, you might also try calling the Form's
and/or TextBox's Update() method before the call to Focus (instead of
setting a timer) and see if that works. It's been a while since I've
dealt with this so I don't remember all the incantations that will work.

-- John
 
J

jamie

I couldn't get any combination of the update to work thats why I threw this
message out there. I'm going with an activate timer in the original event.
the timer fires and sets focus then deactivates the timer. Some
documentation is going to be necessary to explain this one but other then
that It's not that bad. I've created worse ;)
 
Top