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);
}
}