0

I need to invoke my windows form controls because they are on another thread. I need to do a check which one is focussed but i have a weird feeling that my code could be 10000 times simpler. Can someone look for me and tell me what i am doing wrong. The code works. But it just feels nasty. Hope to hear from you.

My invoke class (Got this from another stackoverflow question, so not mine):

    using System;
using System.ComponentModel;

namespace System {
    public static class ISynchronizeInvokeExtensions {
        public static void InvokeEx<T>(this T @this, Action<T> action) where T : ISynchronizeInvoke {
            if (@this.InvokeRequired) {
                @this.Invoke(action, new object[] { @this });
            } else {
                action(@this);
            }
        }
    }
}

My code to check for the focussed one:

private bool test; 
private void setText(string text) {
            this.InvokeEx(x => test = x.textBox1.Focused);
            if (test) {
                this.InvokeEx(x => x.textBox1.Text = text);
            }
            this.InvokeEx(x => test = x.textBox2.Focused);
            if (test) {
                this.InvokeEx(x => x.textBox2.Text = text);
            }
            this.InvokeEx(x => test = x.textBox3.Focused);
            if (test) {
                this.InvokeEx(x => x.textBox3.Text = text);
            }
            this.InvokeEx(x => test = x.textBox4.Focused);
            if (test) {
                this.InvokeEx(x => x.textBox4.Text = text);
            }
            this.InvokeEx(x => test = x.textBox5.Focused);
            if (test) {
                this.InvokeEx(x => x.textBox5.Text = text);
            }
            this.InvokeEx(x => test = x.textBox6.Focused);
            if (test) {
                this.InvokeEx(x => x.textBox6.Text = text);
            }
            this.InvokeEx(x => test = x.textBox7.Focused);
            if (test) {
                this.InvokeEx(x => x.textBox7.Text = text);
            }
        }
Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
Kevin Walter
  • 6,507
  • 8
  • 28
  • 32
  • You'll still need to use InvokeEx to call one of the solutions in the linked question. – Eric J. Jul 22 '15 at 20:04
  • Okay but is there a better way to get the Focused attribute from a textbox using the invoke function. For example: Can i use my current invoke function within a if statement? That would make it allot better for me :) – Kevin Walter Jul 22 '15 at 20:10
  • Basically i want to do this: if (this.InvokeEx(x => test = x.textBox1.Focused)) { But that just doesn't work – Kevin Walter Jul 22 '15 at 20:11
  • You should be able to do something like `if (test) this.InvokeEx(x => { if (x.textBox1.Focused) x.textBox1.Text = text });`. However it's rather brittle code to do that for each of your textboxes. – Eric J. Jul 22 '15 at 20:15
  • Thank you sooo much. Now i know i can write code withing a invoke function. That will get me in the right direction. – Kevin Walter Jul 22 '15 at 20:18
  • When more than a single statement you need the { }. I think you don't actually need them with the `if` but I wasn't sure. You could e.g. write `x => { var temp = "Hello World"; x.textBox1.Text = temp; }` – Eric J. Jul 22 '15 at 20:21

0 Answers0