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
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