| |
Well this recipe doesn't work. if you can make it work or get something
different that does work,
PLEASE LET ME KNOW. I got this code from a fellow on the
CodeGuru
website, but unfortunately I think that this is a bug with the .Net framework
and there is simply no way to get this to work.
Here's what I've got so far:
using
System;
using
System.Windows.Forms;
using
log4net;
namespace
InternalInstantMessenger
{
///
<summary>
/// Just a small class
to help determine if a control has focus
/// Got the code from
here: http://www.codeguru.com/forum/showthread.php?t=320529
///
</summary>
///
<remarks>I had to
modify it a bit to get it to compile and to add logging but otherwise
/// it's as provided
on that site.
/// It seems to me
that Microsoft never tested this and that I'm the first person ever
/// to try and use
it. None of the .Focused or .ContainsFocus work! Even if I'm typing into the
/// control it still
returns false.
///
</remarks>
///
class FocusHelper
{
private static
readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static public
bool HasFocus(Control control)
{
log.Debug("Checking State of Control: " + control.Name +" Is
it focused? "+control.Focused.ToString());
if (control.Focused)
{
return
true;
}
log.Debug("Checking State of Control: " + control.Name +" Is
it focused? "+control.ContainsFocus.ToString());
if (control.ContainsFocus)
{
return
true;
}
foreach (Control ctrl
in control.Controls)
{
if (HasFocus(ctrl))
{
return
true;
}
}
return
false;
}
}
}
When this is running the log file that it puts out looks like this:
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: ConversationForm Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: ConversationForm Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: Input_Font_Button Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: Input_Font_Button Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: ParticipantsLabel Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: ParticipantsLabel Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: membersListView Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: membersListView Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: InviteOthersButton Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: InviteOthersButton Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: ConversationDisplayBox Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: ConversationDisplayBox Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: MessageInputBox Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: MessageInputBox Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: statusBar Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: statusBar Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: SendButton Is it focused? False
21:51:59,859 [InternalInstantMessenger.FocusHelper.HasFocus(:0)] - Checking State of Control: SendButton Is it focused? False
That's the log snippet from when I was typing into the 'MessageInputBox'
RichTextBox when a new message from another machine arrived. Everything is
doubled because of the two lines of logging per control. The first line
for .Focused, the second is for .ContainsFocus. I am really stumped on
this one, it really seems like the focus code from Microsoft is broken badly.
Any help would be appreciated.
|