0

I'm working with a windows application.(.net/c#) on load I want to make first/default control as selected and start dataEntry without using mouse.

Figure1

After I add this.ActiveControl = myTextbox; OR myTextbox .Select();It would work fine looking on UI

figure2

but the same is not active or typable without using mouse initially. I tried the same for Company combobox also and not working expected manner.

I Tried with Focus() as well.

Code:

protected override void OnLoad(EventArgs e) 
{ 
    base.OnLoad(e);
    _context = new CompanyContext();
    companyList = _context.Companies.ToList();
    var companylist = new BindingList<BPS.Data.Entities.Company>(companyList);
    metroComp.DataSource = companylist;
    metroComp.DisplayMember = "CompanyName";
    metroComp.ValueMember = "CompanyName";
    this.ActiveControl = myTextBox;
    //myTextBox.Select();// Tried this line also and working same effect
}

I tried that code in Form_Load also

b_in_U
  • 640
  • 2
  • 7
  • 22

2 Answers2

1

Also add below line of code,

this.Activate();

To your form. It may not be the active parent control. when the form is shown.

I would suggest to activate the form at the end of the Load event. so the form becomes the active form in the application.

Pedram
  • 6,256
  • 10
  • 65
  • 87
bansi
  • 55,591
  • 6
  • 41
  • 52
1

So I think this is working code for your case,

protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _context = new CompanyContext();
            companyList = _context.Companies.ToList();
            var companylist = new BindingList<BPS.Data.Entities.Company>(companyList);
            metroComp.DataSource = companylist;
            metroComp.DisplayMember = "CompanyName";
            metroComp.ValueMember = "CompanyName";
            InitializeComponent();
            myTextBox.Focus();
        }
Pedram
  • 6,256
  • 10
  • 65
  • 87