-2

I have a method in my Form1.cs called GenerateComboBoxList() and I want to call this in the main method so that the contents of the combo box is generated when the application is started.

public void GenerateComboBoxList()
{
     cmbServerDatabase.Items.Add("1");
     cmbServerDatabase.Items.Add("2");
     cmbServerDatabase.Items.Add("3");
}

However, when I begin to type GenerateComboBoxList() in the main method, intellisense does not pick this up. Can anyone help with where I am going wrong?

Habib
  • 219,104
  • 29
  • 407
  • 436
dmoney
  • 861
  • 2
  • 16
  • 24
  • 2
    Can you post more than just the method? The whole class at least – oppassum Jul 07 '15 at 13:43
  • Sorry, there is nothing else really relevant to this. I assumed I could create a function in my form class that I could then call in my main Program class, just to keep main a bit tidier. – dmoney Jul 07 '15 at 13:45
  • 1
    How would we diagnose this without any code? Tell us what you have done and we will try the same. Else this question will get closed. – Patrick Hofman Jul 07 '15 at 13:46
  • Don't worry @Patrick Hofman, a user below sorted. Thanks for your help either way. – dmoney Jul 07 '15 at 14:10

2 Answers2

9

You need an instance of the Form1 in your Main method, only then you can call your method, but, don't do that. Instead call this method in your Form_Load event or overload OnLoad (See: this).

Your Main method in Program.cs should only be responsible for specifying/loading the start up form. Later in your Form's Load event you can do:

private void Form1_Load(object sender, EventArgs e)
{
   GenerateComboBoxList()
}
Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • Thank you Habib, appreciate the help, that has helped me fix it! – dmoney Jul 07 '15 at 14:09
  • It would be better to override `OnLoad` instead of handling your own form events. – Chris Dunaway Jul 07 '15 at 14:10
  • Hey @Chris Dunaway, let me give that a shot and I will get back to you. Cheers buddy. – dmoney Jul 07 '15 at 14:12
  • 3
    @ChrisDunaway For experienced users, yes. If you know what you are doing. If you are just a starter, I would just use the event handlers. – Patrick Hofman Jul 07 '15 at 14:13
  • Thanks for the heads up @PatrickHofman, I may well stick to the event handlers however its worth a look. – dmoney Jul 07 '15 at 14:16
  • @PatrickHofman - Shouldn't beginners be taught the correct way from the beginning? In OOP, with other classes, you don't handle the base class's events, you override the appropriate methods. Why are forms treated differently (other than the fact the VS automatically puts the stub for the Form_Load event)? In my opinion that was a mistake by Microsoft to do it that way. I realize in this example, it doesn't make much (if any) difference. Maybe I'm just too pedantic! :P – Chris Dunaway Jul 07 '15 at 14:26
  • How many times you have seen coworkers overriding a vital method and forgetting to call `base`? That's why. Better safe than sorry. (I always use *override* though) – Patrick Hofman Jul 07 '15 at 14:28
2

You can do it after InitializeComponents() in a constructor of your form :

public Form1()
{
    InitializeComponents();
    GenerateComboBoxList();
    ... other things that you want to set on startup
}

P.S. other option that is already suggested by people here would be to use an Form_Load event handler.

Fabjan
  • 13,506
  • 4
  • 25
  • 52
  • Thank you Fabjan, this also worked. I will be using Habib's way, but I appreciate your help anyway! – dmoney Jul 07 '15 at 14:11