missionarymike
asked on
How to enter autotext into a selected control using a HotKey.
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
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
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(screenP os);
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();
}
foreach (Control ctrl in this.Controls)
{
Point screenPos = MousePosition;
screenPos = this.PointToClient(screenP
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();
}
ASKER
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.
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.
ASKER
Omega2K, what is the Vb code.
To get the hovered control use this method
public Control GetHoveredControl()
{
foreach (Control ctrl in this.Controls)
{
Point screenPos = MousePosition;
screenPos = this.PointToClient(screenP os);
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;
}
}
public Control GetHoveredControl()
{
foreach (Control ctrl in this.Controls)
{
Point screenPos = MousePosition;
screenPos = this.PointToClient(screenP
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;
}
}
er I mean this method:
public Control GetHoveredControl()
{
foreach (Control ctrl in this.Controls)
{
Point screenPos = MousePosition;
screenPos = this.PointToClient(screenP os);
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;
}
public Control GetHoveredControl()
{
foreach (Control ctrl in this.Controls)
{
Point screenPos = MousePosition;
screenPos = this.PointToClient(screenP
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;
}
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
Any way to make it work with the selected control?
ASKER
I forgot to mention, that it also lets you change the .Text property of buttons and labels.
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
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
ASKER
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
Mike
ASKER
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?
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.
ASKER
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.
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
ASKER
Is this a common way of using hotkeys? Or am I trying to reinvent the wheel? It seemed a lot easier in VBA.
Mike
Mike
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That is more efficient, in case a misname a control.
Thanks.
Thanks.
(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?