I am trying to press the tab button to move the mouse pointer I.e. the tab selects the next optional field, I want my mouse to be on this optional field, so that the mouse can move on the page by clicking on the label.
Otherwise, the way to get the selected item is Coordinate and assign it to the mouse.
– Update information –
My program opens another program, I don’t know how to reference the selected item in the program, this is the problem Place.
Thank you.
private void control_Enter(object sender, EventArgs e)
{
if (sender is Control)
{
var control = (Control)sender;
Cursor.Position = control.PointToScreen(new Point()
{
X = control.Width / 2,
Y = control .Height / 2
});
}
}
Then, you can assign multiple controls to it, so that the cursor “moves” to the center of these controls, For example:
button1.Enter += control_Enter;
Or you can use the property grid to assign in the designer.
< p>One thing to note about this method is that if the user clicks the control with the mouse, the cursor will also be centered. Depending on your application, this may or may not be suitable for you.
According to the question Updates to the new requirements:
Since you may not have the right to modify the source code of the relevant form, you can pass a reference to the form being displayed to the function:
void SubscribeContr olsOnEnter(Form form)
{
foreach (Control control in form.Controls)
{
control.Enter += control_Enter;
}
}< /pre>Or similar, you can traverse the controls contained on the form. If the form has controls nested in a container, you need to use recursion, but you can still use this mode.
For embedding Set of methods, the function of the subscription control may look like this (remember, Form is derived from Control):
void SubscribeNestedControlsOnEnter(Control container)
{
foreach (Control control in container.Controls)
{
if (control.Controls.Count> 0)
{
SubscribeNestedControlsOnEnter(control);
}
else control.Enter += control_Enter;
}
}In this way, when the form is displayed, you can call it in the following way:
p>
Form1 form = new Form1();
SubscribeNestedControlsOnEnter(form);
form.Show();
This is me In the first article, I have tried to find a solution, but to no avail.
I am trying to press the tab button to move the mouse pointer. ie. The tab selects the next optional field, I hope My mouse is on this optional field so that the mouse can move on the page by clicking on the label.
Otherwise, the way to get the selected item is to coordinate and assign it to the mouse.
– Update information –
My program opens another program, and I don’t know how to reference the selected item in the program. This is where the problem occurred.
Thank you.
p>
Add this event handler to the form:
private void control_Enter(object sender, EventArgs e)
{< br /> if (sender is Control)
{
var control = (Control)sender;
Cursor.Position = control.PointToScreen(new Point()
{
X = control.Width / 2,
Y = control.Height / 2
});
}
}
Then you can Specify multiple controls to "move" the cursor to the center of these controls, for example:
button1.Enter += control_Enter;
Or you can Use the property grid for assignment in the designer.
There is one thing to note about this method, that is, if the user clicks the control with the mouse, the cursor will also be centered. Depending on your application, this may suit you , It may not be suitable for you.
Update according to the new requirements of the question:
Since you may not have the right to modify the source code of the relevant form, you can change the The reference is passed to the function:
void SubscribeControlsOnEnter(Form form)
{
foreach (Control control in form.Controls)
{
control.Enter += control_Enter;
}
}
or similar, you can traverse the controls contained on the form. If the form has controls nested in the container, then You need to use recursion, but you can still use this mode.
For nested methods, the function of the subscribed control may look like this (remember, Form is derived from Control):
< /p>
void SubscribeNestedControlsOnEnt er(Control container)
{
foreach (Control control in container.Controls)
{
if (control.Controls.Count> 0)
{
SubscribeNestedControlsOnEnter(control);
}
else control.Enter += control_Enter;
}
}
In this way, when the form is displayed, you can press Called in the following ways:
Form1 form = new Form1();
SubscribeNestedControlsOnEnter(form);
form.Show();
< /p>