Link to home
Create AccountLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

How to properly construct text boxes with lookup buttons to get information

I have a C# winform with server lookup buttons. For each lookup button there is a matching text box. There is a leave event attached to each text box.Using the first one as an example:

txtProjectNumber has a leave event that calls a method to query the database and get information related to the job specified.
Now if I click the project lookup button I want it to provide a lookup box of potential job numbers.

The problem I have is that since the lookup button moves the focus off the project text box, the project text box leave event fires.
Is there some way I can have the leave event sense that the project lookup button was clicked and not fire?
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of rwheeler23

ASKER

I was able to get this to work. For my edification, can you please explain what is happening here? I can understand the first part but how does buttonPressed.Any(x => x) function?
        {
            var tb = sender as TextBox;
            var buttonPressed = new[]
            {
                Equals(tb, textBox1) && Equals(ActiveControl, button1),
                Equals(tb, textBox2) && Equals(ActiveControl, button2),
                Equals(tb, textBox3) && Equals(ActiveControl, button3)
            };

            if (buttonPressed.Any(x => x))
            {
                MessageBox.Show($"{ActiveControl.Name} pressed for {tb.Name}");
            }
            else
            {
                MessageBox.Show($"{tb.Name} has been left");
            }
        }
So buttonPressed is an array of logical boolean checks.  Any is an Enumerable method which checks to see if any of (whatever) meets the requirements.  So for example, you could do something like:
var sets = new[]
{
    new[] { 1, 3, 5, 7, 8 },
    new[] { 1, 3, 5, 7, 9 }
};

foreach (var set in sets)
{
    if (set.Any(x => x % 2 == 0))
    {
        Console.WriteLine("Set contains at least one even value");
    }
    else
    {
        Console.WriteLine("Set contains all odd values");
    }
}

Open in new window

Which gives the following output -
User generated image
Since the checks are already boolean checks in the code I provided, I simply have to resolve for x.

-saige-
Interesting, so you are using the lambda expression of modulos 2 to look for an even number in each of the 2 sets. I learn something new every day.
Brilliant, thank you.