0

simple question, I guess.. BUt having hard time with it. I want some buttons in my form to be disabled if ToolStripMenuItem.checked is true. Meaning, I have 2 items in my toolstrip menu, If one of them is checked, the button is disabled. If the second is checked, the button is enabled. The button should be disabled from the moment the program is shown, so I cannot put it inside the click button handler. I tried this one:

public Form1()
{
    if (operationalToolStripMenuItem.Checked == true)
        Burn_JED_UES.Enabled = false;
}

But I'm getting an error, saying:

Object reference not set to an instance of an object,

referring to

operationalToolStripMenuItem.Checked == true

Any advice? Thx.

Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
roy.me
  • 351
  • 2
  • 5
  • 17
  • @SonerGönül This is not a duplicate (at least not of the question you marked as a duplicate). Knowing what NRE is and what is its immediate cause does not help here much if you don't know a little about WinForms internals. – Spook Jun 17 '14 at 10:46
  • @Spook Agreed. I reopen it. – Soner Gönül Jun 17 '14 at 10:47

1 Answers1

2

You removed InitializeComponent(). This is the method, which actually creates all controls on the form and set them up, so if you don't call it, operationalToolStripMenuItem won't exist yet and that's why you get the exception.

public Form1()
{
    InitializeComponent(); // <-

    if (operationalToolStripMenuItem.Checked == true)
        Burn_JED_UES.Enabled = false;
}
Spook
  • 22,911
  • 14
  • 79
  • 146
  • Ok, Thanks! This is it. Another question, though. I want the button to be toggled dependent on the checked toolstip. If it is checked - button is disabled. If it not, button enabled. But I want to be happen all the time, not just once at the begning of the SW. From what I've noticed, it is happening only at the begning. Is that so? update - ok. got it. put it inside the click button private. Thx. – roy.me Jun 17 '14 at 10:58