Search code examples
c#openfiledialog

Focus on Control


I want to create UserControl of connection to mdf file so I want to create new property.

I try that but the problem is every 2 second it ask me to chooseFile... How I can do only when he try to change the value Or If I can do the property with "^" button it will be helpful.

edit: i used Enum

look that:

public enum TypeOfData
{
    NotChoseYet=1,
    ChooseDataBase=2,
    ThereAlreadyDataBase=3
}

public partial class Adodc : UserControl
{
    private bool istart;
    private string sql;
    OpenFileDialog DB = new OpenFileDialog();

    private string DbLocation;

    private TypeOfData? DBchose = (TypeOfData)1;

    public TypeOfData? DataBase
    {
        get 
        {
            return this.DBchose; 
        }
        set 
        {
            if ((int)value == 2) 
            { 
                DB.ShowDialog();
                if (DB.FileName.Length != 0)
                {

                    DbLocation = DB.FileName;
                    DBchose = (TypeOfData)(3);

                }
                     if (DesignMode)
                    {
                        this.Focus();
                    }

            } 
        }
    }

    [ReadOnly(true)]
    public  string Location 
    {
        get
        {
            return DbLocation;
        }
    }
}

i have one problem after choose DataBase is LostFocus (in the desinger) from the controller i created to the form it change focus to Form how i can keep Focus on the UserController

i try added it: but not worked

if (DesignMode)
{
    this.Focus();
}

maybe there another way to put it focus back

when i choose option 2, the openDialog Opened and i choose file after i click Ok , the focus set on Form and not back on the controller.


Solution

  • get
    {
        if (dbLocation == null)
        {
            dbDialog.ShowDialog();
            dbLocation = db.FileName;
        }
        return dbLocation;
    }