Omer
Omer

Reputation: 91

Implementing the Right GUI Behavior for Form_Find

I have a simple application that consists of 2 Forms: Form_Main, and Form_Find.

Form_Main is a big form that has a Grid on it, Form_Find is a small form that has a TextBox and 2 buttons("Previous", "Next") on it.

Form_Find is only shown after the user presses Ctrl-F.

So far everything works good, and I am only left with one problem, a problem that relates to the GUI.

The problem is this:

Form_Find's .TopMost property is set to True.

It works very well when I am in the application, but when I leave the appication open and switch to another application(e.g. a browser), Form_Main is covered by the new window(e.g. the browser) like it should, but Form_Find is still floating and shown, covering a part of the other application's window, and disturbing.

I wanted Form_Find to somehow be a child of Form_Main, so that If I leave/minimize Form_Main, then Form_Find will not continue to appear.. and will only re-appear once I am back to the application - to Form_Main.

To do this, I set Form_Find's .Parent property to the instance of Form_Main, and to make it work, I also needed to write a line before it, which sets Form_Find's .TopLevel property to false. (or else I could not set a Parent to Form_Find)

This actually worked, and now Form_Find behaves like a child form of Form_Main: When I switch from my application to another application, both Form_Main and Form_Find are covered by the new appplication's window.

So this problem was indeed solved, however a new problem has raised due to it, and this new problem is what I am stuck with now.

The new problem is:

Whenever I click the "Next" button in Form_Find, it causes the Grid control on Form_Main to lose focus. And when the Grid on Form_Main loses focus, the Grid takes the cell that was in EditMode, out of EditMode.

And this is my problem.

This did not happen before I set Form_Main to be the Parent of Form_Find. The reason it did not happen, was because when Form_Find is not a child of Form_Main, then when Form_Find gets focussed, the focussed control in Form_Main never knows about it.. Form_Main may lose the focus to another form, but the control inside Form_Main will never know it and will never fire any relevant event about it(only the form will).

So you see, to make the behavior of Form_Find be well, I need both of these things:

1) To have Form_Find somehow a child of Form_Main, so when I switch to another window, Form_Find will not continue floating on the screen, covering other applications 2) To have Form_Find not steal focus from the focussed control on Form_Main, when I click the "Next" button on Form_Find. I did have this as long as Form_Find was not a child of Form_Main, but I lost this once I succeeded in making Form_Find be a child of Form_Main.

So my question to you: What would be the solution to this problem?

Thank you very much

Upvotes: 0

Views: 33

Answers (1)

Steve
Steve

Reputation: 216313

To establish the corrent parent/child behavior requested you need to pass the owner form to the child one when you open the child.

This proof of concept could be tested using LinqPad
(*I am not involved in any way with them, IMHO it is just the most useful tool that you need)

Form f1;
void Main()
{
    f1 = new Form();
    TextBox t1 = new TextBox();
    t1.Location = new Point(0,0);
    TextBox t2 = new TextBox();
    t2.Location = new Point(0,30);
    f1.Controls.AddRange(new Control[] {t1,t2});
    f1.Deactivate += onDeactive;
    f1.Show();

    Form f2 = new Form();
    Button b = new Button();
    b.Click += bClick;
    f2.Controls.Add(b);

    // This line pass the F1 form
    // as owner of F2 establishing the 
    // correct parent/child behavior
    f2.Show(f1);
}

void bClick(object sender, EventArgs e)
{
    Console.WriteLine("Clicked");
    f1.ActiveControl.Text = DateTime.Now.ToString("hh:mm:ss");
}
void onDeactive(object sender, EventArgs e)
{
    Console.WriteLine("Main Deactivated");
}

Upvotes: 1

Related Questions