Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2008

Tip: Looking for answers? Try searching our database.

Can't get ComboBox to show that it has focus

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TOANDO - 06 Mar 2008 01:02 GMT
I have a WinForm with many text boxes and several combo boxes.  I also
have a toolstrip with cut, copy, and paste icons.  When a user clicks
one of the buttons (cut/copy/paste), I want to get the current active
control and if it is a textbox or a combobox, to then copy the
selected text to the clipboard (or do the appropriate option based on
the button).  I can get all of this to work just fine with the
textboxes.  To figure out the active control, I am using the
GetFocus() pointer from "user32.dll".  This is the only way I could
find to do what I needed to do.  Below is an example of how my code
looks.
[DllImport("user32.dll")]
public static extern IntPtr GetFocus();

private void btnCopy_Click(object sender, EventArgs e)
       {
           FocusControl = Control.FromHandle(GetFocus());

           if (FocusControl != null)
           {
               if (FocusControl.GetType().ToString() ==
"System.Windows.Forms.TextBox")
               {
                   TextBox myTextBox = (TextBox)FocusControl;
                   if (!string.IsNullOrEmpty(myTextBox.SelectedText))
                   {
                       Clipboard.SetText(myTextBox.SelectedText);
                   }
               }
               else if (FocusControl.GetType().ToString() ==
"System.Windows.Forms.ComboBox")
               {
                   ComboBox myComboBox = (ComboBox)FocusControl;
                   Clipboard.SetText(myComboBox.SelectedText);
               }
           }
       }

This works great for textboxes, but if the focus is on a combobox, the
FocusControl object is null, so it doesn't fall into the if
statement.  I've tried using the ActiveControl method, but couldn't
figure out how to drill down to the actual control that had focus.
All it gave me back was the control that the textbox was on, not the
actual textbox control.  I'm sure I could iterate through each child
control, but I figured there had to be an easier way.  Any help would
be greatly appreciated.
Kevin Spencer - 06 Mar 2008 12:26 GMT
Hi TOANDO,

The GetFocus() API function returns a handle to the window that has the
KEYBOARD focus.

All you really have to do is to create a field in your Form class that
references a Control. For the Controls that you want to copy from, implement
a Focus event handler that sets the value of the field to that Control. Your
Form will then know at all times which of them has the Focus (if any).

Signature

HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

>I have a WinForm with many text boxes and several combo boxes.  I also
> have a toolstrip with cut, copy, and paste icons.  When a user clicks
[quoted text clipped - 41 lines]
> control, but I figured there had to be an easier way.  Any help would
> be greatly appreciated.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.