0

I’m trying to access a toolstrip button in windows forms using button name in windows forms but facing null reference exception as end result. Normally I can invoke button using its name, but i am facing issue in invoke the button in toolstrip button 2. Automation element recognize till toolstrip 1 after adding it can't recognize toolstrip button 2 This issue occur in N unit framework and access button using automation element.I can able to invoke toolstrip button 1 but not toolstrip button 2. Can anyone guide me for this issue ?

Code snippet for accessing button:

    public  void ClickButton()
    {
        object objPattern;
        AutomationElement click = AutomationElement.FindFirst(TreeScope.Descendants,
         new PropertyCondition(AutomationElement.NameProperty, "toolStripButton2"));
        click.TryGetCurrentPattern(InvokePattern.Pattern, out objPattern);
        ((InvokePattern)objPattern).Invoke();
    }

Code snippet for toolstrip:

enter code here
// toolStrip1
        // 
        this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.toolStripButton1,
        this.toolStripComboBox1,
        this.toolStripButton2});
        this.toolStrip1.Location = new System.Drawing.Point(0, 0);
        this.toolStrip1.Name = "toolStrip1";
        this.toolStrip1.Text = "toolStrip1";
        // 
        // toolStripButton1
        // 
        this.toolStripButton1.Name = "toolStripButton1";
        this.toolStripButton1.Text = "toolStripButton1";
        // 
        // toolStripComboBox1
        // 
        this.toolStripComboBox1.Name = "toolStripComboBox1";
        this.toolStripComboBox1.Size = new System.Drawing.Size(121, 25);
        // 
        // toolStripButton2
        // 
        this.toolStripButton2.Name = "toolStripButton2";
        this.toolStripButton2.Text = "toolStripButton2";
        //
    private System.Windows.Forms.ToolStrip toolStrip1;
    private System.Windows.Forms.ToolStripButton toolStripButton1;
    private System.Windows.Forms.ToolStripComboBox toolStripComboBox1;
    private System.Windows.Forms.ToolStripButton toolStripButton2;
Santhosh
  • 1
  • 3
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Stefan Dec 21 '20 at 08:37
  • i bet it's because of one extra white space in your `new PropertyCondition(AutomationElement.NameProperty, ” toolStripButton2”)` (see `” toolStripButton2”`) – vasily.sib Dec 21 '20 at 08:41
  • No @vasily.sib it doesn't work – Santhosh Dec 21 '20 at 08:46
  • Where are you setting `WindowElement`? It's null, from the code you're showing here. – Jimi Dec 21 '20 at 09:46
  • Window element declared in class itself @Jimi – Santhosh Dec 21 '20 at 09:59
  • Also, note that the `AutomationElement.NameProperty` refers to the Text of an UI Element, not its Name. Here, it's the same value, but it may not be in the actual code -- The element is **declared**, but it's never set to anything, so it's **null**. You should have `WindowElement = AutomationElement.FromHandle(this.toolStrip1.Handle);` as the first line of that code. Then search in `TreeScope.Children`, not `TreeScope.Descendants`. – Jimi Dec 21 '20 at 09:59
  • using the code i can access toolstrip1 button can't access toolstrip2 can you please give some explanation or some other way to access it? – Santhosh Dec 21 '20 at 10:05
  • What `toolStrip2`? There's not such thing here. Read my previous comment and do as described. – Jimi Dec 21 '20 at 10:07
  • toolstrip button 2 I'm adding buttons as items in toolstrip i can able to access toolstrip button 1 using its text but can't access toolstrip button 2 – Santhosh Dec 21 '20 at 10:10
  • Did you change the code as described? -- As shown here, the two ToolStripButtons don't have a click handler, so when you invoke the Click action, nothing will happen. -- Is this your real code? Are you trying to automate your own application or is it a Window of another app? Or a Window of the same app automated from another? – Jimi Dec 21 '20 at 10:17
  • I use class library for N unit framework there, button access code will be there using that open a form in another sample(from class library i'm trying to perform button click in win forms sample) which contains toolstrip items i'm trying to click that. Your code doesn't work for this condition because toolstrip added in another sample not this one. – Santhosh Dec 21 '20 at 10:24
  • It's not really clear what you're saying here. Do you have access to the Form instance? If so, then your code could be: `WindowElement = AutomationElement.FromHandle([The Form Instance].Handle);`. Now, `WindowElement` is the Element representing the Form Window. Find the ToolStrip, which is child Element of `WindowElement`. When you have found the ToolStrip Element, find its child ToolStripButton (still keeping in mind that `AutomationElement.NameProperty` represents the Element Text). – Jimi Dec 21 '20 at 10:47
  • Thanks for your help can you please help me how to access button using it's name? – Santhosh Dec 21 '20 at 11:08
  • I'm using N unit framework its separate application and winforms sample is separate application.I'm trying to click toolstrip button in winform sample using N unit framework.so cant use form instance in it @Jimi – Santhosh Dec 21 '20 at 11:20
  • Then get the Window where the ToolStrip is located from the RootElement: `WindowElement = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, [Some Condition]);`, the rest is the same. – Jimi Dec 21 '20 at 11:34
  • That doesn't work for me – Santhosh Dec 31 '20 at 05:07

0 Answers0