Setting the Focus () on the UserControl's TextBox

1

I have the following UserControl:

public partial class SliderChrome : UserControl
{
    private int _min;

    public int Min
    {
        get { return _min; }
        set
        {
            txtmin.Text = _min.ToString();
        }
    }
}

I use this UserControl on the Form as follows:

public partial class MacroForm : Form
{
    private void ValorMinEMaxParametros(bool isEnglish, int Min, int Max)
    {
        DialogResult dialogResult;
        if (isEnglish)
        {
            dialogResult = MessageBox.Show("Do you...", "Max ...", MessageBoxButtons.YesNo);
        }
        else
        {
            dialogResult = MessageBox.Show("Deseja...", "Max ...", MessageBoxButtons.YesNo);
        }

        if (dialogResult == DialogResult.Yes)
        {
            sliderChrome1.Min = Min;
            sliderChrome1.Max = Max;                
        }
        else if (dialogResult == DialogResult.No)
        {
            sliderChrome1.Min = 0;
            sliderChrome1.Max = 1;
            sliderChrome1.txtMin.Focus(); //Erro
        }
    }
}

I would like to set the Focus in the TextBox txtmin of the UserControl. How do I?

    
asked by anonymous 20.03.2017 / 16:09

3 answers

0

I had to do this:

I created the method setFocus() in my UserControl which directs the focus on the desired component.

public partial class SliderChrome : UserControl
{
    private int _min;

    public int Min
    {
        get { return _min; }
        set
        {
            txtmin.Text = _min.ToString();
        }
    }

    public void setFocus()
    {
        txtmin.Focus();
    }
}

And in Form seto focus by method setFocus()

public partial class MacroForm : Form
{
    private void ValorMinEMaxParametros(bool isEnglish, int Min, int Max)
    {
        DialogResult dialogResult;
        if (isEnglish)
        {
            dialogResult = MessageBox.Show("Do you...", "Max ...", MessageBoxButtons.YesNo);
        }
        else
        {
            dialogResult = MessageBox.Show("Deseja...", "Max ...", MessageBoxButtons.YesNo);
        }

        if (dialogResult == DialogResult.Yes)
        {
            sliderChrome1.Min = Min;
            sliderChrome1.Max = Max;                
        }
        else if (dialogResult == DialogResult.No)
        {
            sliderChrome1.Min = 0;
            sliderChrome1.Max = 1;
            sliderChrome1.setFocus(); //Setando o focus atráves do método
        }
    }
}
    
20.03.2017 / 19:34
0

Within the MacroForm class, you can add the Form.Activated event and call the Focus() method of the control:

private void MacroForm_Activated(object sender, System.EventArgs e)
{
    txtMin.Focus();
}

Another option that can work is to change the TabIndex of the TextBox txtMin . This property defines the order that the controls will receive the focus, from smallest to largest.

    
20.03.2017 / 19:11
-2

Be more explicit, what do you really intend to do? If that's what I mean, then try to do this:

                 txtMin.Focus(); 
    
20.03.2017 / 16:18