5

To load my ComboBox at the beginning I used the mine method LoadDataFromDB() in the InitializeComponent() method, but there is an warning says:

The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified

So where can I load my ComboBox?

Shmwel
  • 1,697
  • 5
  • 26
  • 43
Sal-laS
  • 11,016
  • 25
  • 99
  • 169

3 Answers3

5

Use form's constructor

public Form1()
{
    InitializeComponent();
    LoadDataFromDB();
}

Or (sometimes better) Form.Load event handler (it will be added automatically when you double-click form in designer):

private void Form1_Load(object sender, EventArgs e)
{
    LoadDataFromDB();
}

Another option is overriding OnLoad method of form.

NOTE: You see this warning, because InitializeComponent is generated by designer, and it will be completely re-generated when you'll change something in designer (add some control, move or resize some control, change color etc). Thus all your changes to this method will gone.

Community
  • 1
  • 1
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
3

Directly after InitializeComponent is called, or as long as its after InitializeComponent().

public FormName()
{
   InitializeComponent();
   LoadDataFromDB();
}

Note: You may not see these controls in the Visual studio designer view

Sayse
  • 42,633
  • 14
  • 77
  • 146
1

You don't need to load it in InitializeComponent method. Just after it you can call your method LoadDataFormDB().

InitializeComponent();
LoadDataFormDB();

You can read also, the comments above the method which says:

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer. 
'Do not modify it using the code editor.
Shmwel
  • 1,697
  • 5
  • 26
  • 43