It is written that this is impossible because the object is not inherited from Control and belongs to ToolStrip, but I checked with Window Scanner that such a descriptor exists and the object can be controlled.
Asked
Active
Viewed 139 times
2 Answers
0
The problem has been solved specifically for the ToolStripTextBox:
IntPtr handle = tstb.Control.Handle;

Antinet
- 1
- 2
-
`ToolStripTextBox` doesn't derive from `ToolStripItem`, it derives from `ToolStripControlHost`. It has a handle because it actually hosts a TextBox Control. You should fix your question to explain what you're really after: `ToolStripItems` or just UI elements that derive from `ToolStripControlHost` (as `ToolStripComboBox`, but not `ToolStripButton`, for example). If you need to *control* these elements, you can use UI Automation. – Jimi Jun 09 '20 at 08:24
-
If you need to access the Components from your app, you can also use reflection, or, inside the same Form class, just the `components` private Field. – Jimi Jun 09 '20 at 08:31
0
if you are doing some Desktop Application Automation by yourself (since UIautomation is just ridiculously badly designed ), you can design a method which returns a Control class this way:
public virtual Control FindControlByName(string name)
{
[...]
if (control is ToolStrip)
{
var toolStrip = control as ToolStrip;
for (int i=0; i<toolStrip.Items.Count; i++)
{
ToolStripItem currrentItem = toolStrip.Items[i];
ToolStripTextBox textBox = currrentItem as ToolStripTextBox;
if (textBox != null)
{
if (textBox.Name == name)
{
return textBox.Control;
}
}
}
}
}
return null;
}
If you can speak with developer of TargetApp, ask him to change the ToolStripLabel by a ToolStripTextBox . Indeed you will benefit from ToolStripControlHost : https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.toolstripcontrolhost?view=windowsdesktop-7.0
which is also a ToolStripItem, but which exposes the 'Automation crucial element' : Control property.
Just set the ToolStripTextBox as readonly and with no border , and with a width, and done !

Thierry Brémard
- 625
- 1
- 5
- 14