Setting Focus() in Usercontrol Textbox

Asked

Viewed 341 times

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 in 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 Focus in Textbox txtmin Usercontrol. How do I ?

  • Instead of sliderChrome1.txtMin.Focus();, if you do sliderChrome1.txtMin.Select(); also doesn’t work?

3 answers

0

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

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

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

0


I had to do it this way:

I created the method setFocus() in my UserControl which arrow 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 the Focus through the 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
        }
    }
}
  • Look at my comment on the question, please

-2

Be that as it may, what do you really intend to do? If that is what I intended, then try to do so :

                 txtMin.Focus(); 
  • 3

    Use the comment option when you don’t want to write a reply!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.