Focus Loss

G

Guest

I am using VS 2005 to develope a WindowsForms application. In the application
I have a TextBox control which I am trying to use as data input to an
incremental search and positioning of the current row in a DataGridView,
which is sourced through a DataSource to a DataSet containing a DataTable.
The code I am using is:

private void BaseLocation_toolStripTextBox_TextChanged(object
sender, EventArgs e)
{
if (BaseLocation_toolStripTextBox.Text.Length > 0)
{
string searchStr = "PLACENAME like '" +
BaseLocation_toolStripTextBox.Text + "%'";
DataRow[] selectedRows =
registrationPlaceListDataSet.RegistrationPlaceList.Select(searchStr);
if (selectedRows.Length > 0)
{
int selectedKey = (int)selectedRows[0]["KeyField"];
int index =
registrationPlaceListBindingSource.Find("KeyField", selectedKey);
registrationPlaceListBindingSource.Position = index;
}
}
}


The TextBox looses focus after entry of a character and if I click it to
reestablish focus it looses focus on each additional character entered.

Any help in understanding what is happening and how I might retain focus in
the TextBox will be appreciated.

Regards;
 
J

justin creasy

I am using VS 2005 to develope a WindowsForms application. In the application
I have a TextBox control which I am trying to use as data input to an
incremental search and positioning of the current row in a DataGridView,
which is sourced through a DataSource to a DataSet containing a DataTable.
The code I am using is:

private void BaseLocation_toolStripTextBox_TextChanged(object
sender, EventArgs e)
{
if (BaseLocation_toolStripTextBox.Text.Length > 0)
{
string searchStr = "PLACENAME like '" +
BaseLocation_toolStripTextBox.Text + "%'";
DataRow[] selectedRows =
registrationPlaceListDataSet.RegistrationPlaceList.Select(searchStr);
if (selectedRows.Length > 0)
{
int selectedKey = (int)selectedRows[0]["KeyField"];
int index =
registrationPlaceListBindingSource.Find("KeyField", selectedKey);
registrationPlaceListBindingSource.Position = index;
}
}
}

The TextBox looses focus after entry of a character and if I click it to
reestablish focus it looses focus on each additional character entered.

Any help in understanding what is happening and how I might retain focus in
the TextBox will be appreciated.

Regards;

Arlyn_L,

I was able to replicate your problem. I made a project in VS 2005 with
a TextBox and a DataGridView. Using a TextChanged event of the TextBox
I call the DataGridView's Select function. This is slightly different
from your code, but causes the same effect to occur. All I did was add
a call to the TextBox's Focus() function at the end of my event.

So you would add BaseLocation_toolStripTextBox.Focus(); as the very
last line in your event. I know this is somewhat of a hack, so if this
causes problems in other places in your project please post it's
results. Hope this helps!

~ Justin Creasy
www.immergetech.com
www.immergecomm.com
 
G

Guest

I had already tried adding a TextBox.Focus() at the end of the selected rows
if. That did not work in my case. Focus was still lost.

Any other suggestions?

Best regards;
--
Arlyn_L


justin creasy said:
I am using VS 2005 to develope a WindowsForms application. In the application
I have a TextBox control which I am trying to use as data input to an
incremental search and positioning of the current row in a DataGridView,
which is sourced through a DataSource to a DataSet containing a DataTable.
The code I am using is:

private void BaseLocation_toolStripTextBox_TextChanged(object
sender, EventArgs e)
{
if (BaseLocation_toolStripTextBox.Text.Length > 0)
{
string searchStr = "PLACENAME like '" +
BaseLocation_toolStripTextBox.Text + "%'";
DataRow[] selectedRows =
registrationPlaceListDataSet.RegistrationPlaceList.Select(searchStr);
if (selectedRows.Length > 0)
{
int selectedKey = (int)selectedRows[0]["KeyField"];
int index =
registrationPlaceListBindingSource.Find("KeyField", selectedKey);
registrationPlaceListBindingSource.Position = index;
}
}
}

The TextBox looses focus after entry of a character and if I click it to
reestablish focus it looses focus on each additional character entered.

Any help in understanding what is happening and how I might retain focus in
the TextBox will be appreciated.

Regards;

Arlyn_L,

I was able to replicate your problem. I made a project in VS 2005 with
a TextBox and a DataGridView. Using a TextChanged event of the TextBox
I call the DataGridView's Select function. This is slightly different
from your code, but causes the same effect to occur. All I did was add
a call to the TextBox's Focus() function at the end of my event.

So you would add BaseLocation_toolStripTextBox.Focus(); as the very
last line in your event. I know this is somewhat of a hack, so if this
causes problems in other places in your project please post it's
results. Hope this helps!

~ Justin Creasy
www.immergetech.com
www.immergecomm.com
 
V

VJ

I am sure you would have tried BaseLocation_toolStripTextBox.Select().. that
would work. the .Select() works more reliably than .Focus() for me.

VJ
 
L

Linda Liu [MSFT]

Hi Arlyn,

Based on my understanding, you have a ToolStrip with a ToolStripTextBox and
a DataGridView on your form. When the user inputs some text in the
ToolStripTextBox, the DataGridView will set its current row to match the
text in the ToolStripTextBox. The problem is that the ToolStripTextBox
looses focus after entry of a character. If I'm off base, please feel free
to let me know.

I performed a test on this issue but I couldn't reproduce the problem on my
side. I also performed a test on TextBox, but I found the problem doesn't
exist on TextBox, either.

The code of the TextChanged event handler of the ToolStripTextBox in my
test is the same as yours.

It seems that the focus may be taken from the ToolStripTextBox at somewhere
else in your actual project.

If possible, could you please send me a sample project that could just
reproduce the problem? To get my actual email address, remove 'online' from
my displayed email address.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Problen Solved!

The problem was caused by actions in the DataGridView Selection Changed
event which changed the contents of other controls notably a WebBrowser
control. The solution was to inhibit these actions when the TextBox had
focus and to call the event code on leaving the TextBox.

Thank you all for your assistance.

Best regards;
 
Top