¿cómo poner el foco en el cuadro de texto cuando se carga el formulario?


Tengo en mi cuadro de texto del programa C #

Necesito que cuando se inicie el programa, el foco estará en el cuadro de texto

Lo intento en Form_Load:

MyTextBox.Focus();

Pero no funcionará

 126
Author: Adrian Carneiro, 2011-07-06

14 answers

Establece la propiedadActiveControl del formulario y estarás bien.

this.ActiveControl = yourtextboxname;
 283
Author: V4Vendetta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-24 07:31:56

Compruebe su orden de pestañas y asegúrese de que el cuadro de texto esté configurado en cero

 20
Author: Spacemancraig,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-06 13:32:15

Puedes intentar:

MyTextBox.Seleccionar();

Según la documentación:

El método Select activa el control si el control es seleccionable el bit de estilo se establece en true en ControlStyles, está contenido en otro control, y todos sus controles principales son visibles y habilitados.

Primero puede comprobar si el control puede seleccionarse inspeccionando el MyTextBox.CanSelect propiedad.

 13
Author: Neil Knight,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-10 00:02:19

Si solo desea establecer el foco la primera vez que se muestra el formulario, intente manejar el Formulario .Se muestra evento y hacerlo allí. De lo contrario, utilice Control.VisibleChanged .

 11
Author: Andy Johnson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-06 13:36:36

No se puede establecer el foco en un control si no se ha renderizado. Forma.Load () ocurre antes de que los controles sean renderizados.

Vaya a los eventos del formulario y haga doble clic en el evento "Mostrado". En el controlador de eventos que se muestra en el formulario, llame al control.Método Focus ().

    private void myForm_Shown(object sender, EventArgs e)
    {
        // Call textbox's focus method
        txtMyTextbox.Focus();
    }
 6
Author: DavidG,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-31 03:02:21

Textbox.Focus() "Tries" para poner el foco en el elemento textbox. En caso de que la visibilidad del elemento esté oculta, por ejemplo, Focus() no funcionará. Por lo tanto, asegúrese de que su elemento esté visible antes de llamar a Focus().

 4
Author: GRC,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-15 20:18:16

La razón por la que no puede hacer que funcione es porque el evento Load es llamado antes de que el formulario sea dibujado o renderizado.

Es como decirle a una pizzería cómo hacer tu pizza, y luego pedirles que te envíen una foto de cuánto pepperoni hay en tu pizza antes de que la hagan.

using System;
using System.Windows.Forms;

namespace Testing
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();

            Load += TestForm_Load;

            VisibleChanged += TestForm_VisibleChanged;

            Shown += TestForm_Shown;

            Show();

        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            MessageBox.Show("This event is called before the form is rendered.");
        }

        private void TestForm_VisibleChanged(object sender, EventArgs e)
        {
            MessageBox.Show("This event is called before the form is rendered.");
        }

        private void TestForm_Shown(object sender, EventArgs e)
        {
            MessageBox.Show("This event is called after the form is rendered.");
            txtFirstName.Focus();
        }
    }
}
 3
Author: Ben,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-20 14:08:37

Utilice la forma mostrada evento y conjunto

MyTextBox.Focus();
 2
Author: Nitin...,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-09 17:41:52

Resolví mi problema al cambiar la propiedad "TabIndex" de TextBox. Establezco 0 para el cuadro de texto que quiero enfocarlo en el Formulario cuando se inicie el programa.

 2
Author: Mehmet Taha Meral,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-04 19:33:29

Ha funcionado para mí establecer tabindex en 0 este.tu txtbox.TabIndex = 0;

 0
Author: Muhafil Saiyed,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-01 06:47:31

Puede usar textBox1.select(); o el TabIndex en la configuración del cuadro de texto. TabIndex=0 focoso primero.

 0
Author: user9248597,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-22 00:19:53

Establezca Tabstop en True y TabIndex al mínimo para el control en el que necesita centrarse.

Por ejemplo, si tiene 2 cuadros de texto : textBox1 y TextBox2, establezca Tabstop en True para ambos y TabIndex en 0 y 1 respectivamente. Cuando se carga el formulario, el foco estará en textBox1 y al presionar la tecla' Tab', el foco se moverá a TextBox2.

 0
Author: Aparna Ratheesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-18 02:44:34

Establezca el valor de la propiedad Tab Index = 0 y luego en la función de carga del formulario escriba:

YourTextboxName.Focus();

Funcionará.

 0
Author: Shakaib Akhtar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-11 06:57:00

En jquery establecer foco

$(function() {
  $("#txtBox1").focus();
});

O en Javascript puedes hacer

window.onload = function() {
  document.getElementById("txtBox1").focus();
};
 -5
Author: Sudip Bhattacharya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-01 12:38:32