Why does calling Focus() not set focus in this instance?
Asked Answered
V

3

2

I adapted a roll-your-own Input box based on this.

I modified the code to the following:

using System;
using System.Windows.Forms;

public static class PromptForText
{
    public static string ShowDialog(string caption, string text)
    {
        Form prompt = new Form();
        prompt.Width = 280;
        prompt.Height = 150;
        prompt.Text = caption;
        Label textLabel = new Label() { Left = 16, Top = 20, Width = 240, Text = text };
        TextBox textBox = new TextBox() { Left = 16, Top = 40, Width = 240 };
        Button confirmation = new Button() { Text = "Okie Doak", Left = 16, Width = 80, Top = 72 };
        confirmation.Click += (sender, e) => { prompt.Close(); };
        prompt.Controls.Add(confirmation);
        prompt.Controls.Add(textLabel);
        prompt.Controls.Add(textBox);
        prompt.StartPosition = FormStartPosition.CenterScreen;
        prompt.ShowDialog();
        textBox.Focus();
        return textBox.Text;
    }
}

I added the "textBox.Focus()" but it doesn't do what I expected it to. I tried it both before and after the call to ShowDialog().

What am I missing? Why doesn't calling focus on the textbox set focus to same?

UPDATE

Controls being created now like so, based on Steve (Wozniak's?) answer:

TextBox textBox = new TextBox() { Left = 16, Top = 40, Width = 240, TabIndex = 0, TabStop = true };
Label textLabel = new Label() { Left = 16, Top = 20, Width = 240, Text = text };
Button confirmation = new Button() { Text = "Okie Doak", Left = 16, Width = 80, Top = 72, TabIndex = 1, TabStop = true };
Videlicet answered 23/5, 2014 at 22:24 Comment(0)
J
2

The call ShowDialog is a blocking call. It means that no code is executed after this call until you (or your user) close the form. At that point the call to Focus has no effect because the form, while still in memory, is hidden and ready to be closed and disposed.

As a simple workaround just set the TabIndex property of the TextBox to be the first control on the form. In this way the focus is handled automatically by the Forms Framework code (need also to have the TabStop property set to true)

 TextBox textBox = new TextBox() 
          { Left = 16, Top = 40, Width = 240, TabIndex = 0, TabStop = true };

After a bit of testing I should add that you need to set the same properties also on the Button control to have it working

 Label textLabel = new Label() { Left = 16, Top = 20, Width = 240, Text = text };
 TextBox textBox = new TextBox()
          { Left = 16, Top = 40, Width = 240, TabIndex = 0, TabStop = true };
 Button confirmation = new Button() 
          { Text = "Okie Doak", Left = 16, Width = 80, Top = 72, TabIndex = 1, TabStop = true };

Another possibility is to add the TextBox as first control in the form's controls collection before adding the Button and the Label

 prompt.Controls.Add(textBox);
 prompt.Controls.Add(confirmation);
 prompt.Controls.Add(textLabel);
Judyjudye answered 23/5, 2014 at 22:33 Comment(0)
D
1

Your Focus command is only happening after the ShowDialog has completed. If you have a look at the ShowDialog documentation - http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx

You will see that ShowDialog only completes when the window is closed.

Druggist answered 23/5, 2014 at 22:34 Comment(0)
P
1

Your form is only visible during the call to ShowDialog(). Why setting focus before ShowDialog() doesn't work I don't know for sure, but probably the focus is getting lost somewhere in the process of loading and displaying the form. As for why after ShowDialog() doesn't work, by that point the form has been shown and closed by the user. It's too late. To set the focus immediately before the form is shown, you could handle prompt.Load with a method that has the focus code in it.

Plenipotentiary answered 23/5, 2014 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.