Form lost focus

A

Alain Dekker

I've got a problem where if I respond to the ListView::OnActivate Windows
message to load a form, the form has mouse capture (I can click its
buttons). But recently, I was trying to solve a problem where the user
accidentally double-clicks the ListView and what happens is that they get
the child form loaded, click Ok on that, and then it pops up a second time.

So to solve the problem, I removed all events in the ListView and put in a
timer running a 200ms interval. The timer code looks like this:

private void tmrSelection_Tick(object sender, EventArgs e)
{
// Disable timer
tmrSelection.Enabled = false;
if (m_bExiting)
return;

// See if any icons in the list have had focus set to them
if (lstDetector.FocusedItem != null)
{
m_strFocusedItem = lstDetector.FocusedItem.Text;
lstDetector.FocusedItem.Focused = false;
ListView.SelectedIndexCollection indexes = lstDetector.SelectedIndices;
foreach (int nIndex in indexes)
lstDetector.Items[nIndex].Selected = false;

Application.DoEvents();
AllocMenu(m_strFocusedItem); // Load child form
}

// Restart timer
tmrSelection.Enabled = true;
}

This loads the child form nicely...but the child form (usually, but
confusingly not always) does not have the capture! No amount of clicking the
form responds to mouse events. I have to switch focus to another application
(such as Task Manager) and back and then it works fine.

I've tried setting any and all of the following, and nothing solves the
problem:
this.Capture = true;
this.TopMost = true;

btnOk.Focus(); // A button on the child form
this.Focus();
this.BringToFront();
this.Activate();

How do I ensure that the child form has focus and will respond to mouse
click events? What is wrong with my approach that is causing the child form
to be inactive (but visible)?

Thanks,
Alain
 
A

Alain Dekker

Thanks Peter. I also thought my approach was going about it the wrong way,
but nevertheless couldn't see anythign fundamentally wrong with it. I'll see
what I can do to approach the problem in the way you approach, specifically
"mimimse the chance of it happening". The confirm-alert method wouldn't sit
well with our customers (who are not techy at all).

Thanks,
Alain

Peter Duniho said:
I've got a problem where if I respond to the ListView::OnActivate Windows
message to load a form, the form has mouse capture (I can click its
buttons). But recently, I was trying to solve a problem where the user
accidentally double-clicks the ListView and what happens is that they get
the child form loaded, click Ok on that, and then it pops up a second
time.

So to solve the problem, I removed all events in the ListView and put in
a
timer running a 200ms interval. [...]

I haven't had time to provide an in-depth discussion about your question.
Unfortunately, it appears no one else has either.

In lieu of a comprehensive reply, I will offer this suggestion: your
original approach to dealing with the accidental double-click was
misguided, if well-intentioned. You should in general not spend time
trying to harden your UI against that sort of accidental input.

Instead, just make sure that accidental inputs (whatever they may be)
cannot do anything destructive, or at least minimize the chances of it.
Typically the two main approaches to that is to always provide a way to
undo an action, or to display an alert allowing the user to confirm the
destructive action. The corollary to this is to design the UI so that
even a sequence of easily-caused accidental inputs is not going to cause
problems; that is, if you choose the confirm-alert route, make sure that
the same input that led to the alert is not the input that will confirm
the action.

Your focus problem is fundamentally a result of approaching the problem
incorrectly in the first place. Rather than trying to work around the new
focus problem, IMHO you should go back and remove the original fix, and
approach that original problem differently.

Pete
 

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