How to enter autotext into a selected control using a HotKey.

missionarymike
missionarymike used Ask the Experts™
on
Hello Experts.

I am using a timer and key combination to use hotkeys, however I can't figure out how to enter the text into a control without first using a button or some kind of reference.  Any ideas on how to get the controls "address" without having a static button?

The code is below.

I though of having a variable be set every time a control is selected but that would require a lot of redundant code.

Mike
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
 
        '// Timer interval is set to 1
 
        Dim D As Boolean = GetAsyncKeyState(Keys.D)
        Dim Ctrl As Boolean = GetAsyncKeyState(Keys.ControlKey)
 
 
        If D And Ctrl = True Then
 
            '// I am only using the clipboard as I though it would paste correctly
            Clipboard.SetText(Now)
 
            '// This works, but only because I have "hard coded" the controls address
            'TextBox1.Text = Clipboard.GetText()
 
            '// I would like this to work by "pasting" the GetText at the cursor position
            Clipboard.GetText()
 
        End If
 
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Are you trying to get this to work while:
(a) Your app is in Focus?
(b) Your app is NOT in Focus?

Do you want the value to be pasted:
(c) Into a control in YOUR app?
(d) Into the currently focused app?

Commented:
Whether it's in focus or not you can add this to your timer tick event:

foreach (Control ctrl in this.Controls)
            {
                Point screenPos = MousePosition;
                screenPos = this.PointToClient(screenPos);
                if (screenPos.X >= ctrl.Location.X && screenPos.Y >= ctrl.Location.Y && screenPos.X <= (ctrl.Width + ctrl.Location.X) && screenPos.Y <= (ctrl.Height + ctrl.Location.Y))
                    ctrl.Text = Clipboard.GetText();
            }

Author

Commented:
I am trying to do something like:
Ctrl + D enters the current date into the selected textbox.  The issue is that I only want place the cursor in the box and the initiate the hotkey.
 
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Author

Commented:
Omega2K, what is the Vb code.
 

Commented:
To get the hovered control use this method

public Control GetHoveredControl()
{
       foreach (Control ctrl in this.Controls)
            {
                Point screenPos = MousePosition;
                screenPos = this.PointToClient(screenPos);
                if (screenPos.X >= ctrl.Location.X && screenPos.Y >= ctrl.Location.Y && screenPos.X <= (ctrl.Width + ctrl.Location.X) && screenPos.Y <= (ctrl.Height + ctrl.Location.Y))
                    return ctrl;
            }
}

Commented:
er I mean this method:

public Control GetHoveredControl()
{
       foreach (Control ctrl in this.Controls)
            {
                Point screenPos = MousePosition;
                screenPos = this.PointToClient(screenPos);
                if (screenPos.X >= ctrl.Location.X && screenPos.Y >= ctrl.Location.Y && screenPos.X <= (ctrl.Width + ctrl.Location.X) && screenPos.Y <= (ctrl.Height + ctrl.Location.Y))
                    return ctrl;
            }
           return null;
}

Commented:
VB.NET code is:

Public Function GetHoveredControl() As Control
    For Each ctrl As Control In Me.Controls
        Dim screenPos As Point = MousePosition
        screenPos = Me.PointToClient(screenPos)
        If screenPos.X >= ctrl.Location.X AndAlso screenPos.Y >= ctrl.Location.Y AndAlso screenPos.X <= (ctrl.Width + ctrl.Location.X) AndAlso screenPos.Y <= (ctrl.Height + ctrl.Location.Y) Then
            Return ctrl
        End If
    Next
    Return Nothing
End Function
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
That would only work if ALL controls were directly contained by the form.  If you have nested controls then you'll need a more sophisticated routine...

Author

Commented:
All my controls witll be contained in the form.  However, the GetHoveredControl doesn't work with the cursor or selected textbox, only where the mouse is.  If the mouse isn't over a textbox or control, it throws an error.
Any way to make it work with the selected control?

Author

Commented:
I forgot to mention, that it also lets you change the .Text property of buttons and labels.

Commented:
Public Function GetHoveredControl() As Control
    For Each ctrl As Control In Me.Controls
        Dim screenPos As Point = MousePosition
        screenPos = Me.PointToClient(screenPos)
        If screenPos.X >= ctrl.Location.X AndAlso screenPos.Y >= ctrl.Location.Y AndAlso screenPos.X <= (ctrl.Width + ctrl.Location.X) AndAlso screenPos.Y <= (ctrl.Height + ctrl.Location.Y) Then
            Return ctrl
        End If
    Next
    Return Me.ActiveControl
End Function

Author

Commented:
That works better.  But if your mouse is over a control, but a textbox is selected, the the control.text is changed and not the textbox.  If you are not hovering over a control, but have selected a textbox, then it works great.
Mike

Author

Commented:
Okay, I commented out everything except the Return ActiveControl and it works great except for when the button is selected.  Any way to exclude controls other that input boxes?

Commented:
what controls do you want to change then? You would need to validate for each control then. Every single form control has a Text property.

Author

Commented:
Okay, on a completely different wavelength, this works.  I didn't realize the active control object.
I label all my controls btnName for buttons, txtName for textboxes, lblName for lables.
 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        '// Timer interval is set to 1
        Dim D As Boolean = GetAsyncKeyState(Keys.D)
        Dim Ctrl As Boolean = GetAsyncKeyState(Keys.ControlKey)
        If ActiveControl.Name.Contains("txt") Then
            If D And Ctrl = True Then
                ActiveControl.Text = Now
            End If
        End If
    End Sub

Open in new window

Author

Commented:
Is this a common way of using hotkeys?  Or am I trying to reinvent the wheel?  It seemed a lot easier in VBA.
Mike
Commented:
You can use hot keys like that, but you are checking whether the control type improperly

Here is how you would validate if it's a textbox:
If TypeOf ActiveControl Is TextBox Then
'Do whatever
End If

Author

Commented:
That is more efficient, in case a misname a control.
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial