Axilleas DevAxe
Axilleas DevAxe

Reputation: 13

ToolStripMenuItem click event not working

I'm trying to open a file dialog window by pressing the "Open" button (ToolStripMenuItem) but it's not working for some reason (no errors, just not working).

It's working if i'll write exactly the same code in the method:

toolStripDropDownButton1_Click(object sender, EventArgs e) {...}

but it doesn't work for methods of menu items. Any idea?

    private void openFileToolStripMenuItem_Click(Object sender, EventArgs e)
    {
            OpenFileDialog openFileDialog1 = new OpenFileDialog
            {
                InitialDirectory = @"C:\",
                Title = "Find the .assets file to backup",

                CheckFileExists = true,
                CheckPathExists = true,

                DefaultExt = ".assets",
                Filter = "assets files (*.assets)|*.assets",
                FilterIndex = 2,
                RestoreDirectory = true,

                ReadOnlyChecked = true,
                ShowReadOnly = true
            };

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Visible = true;
                textBox2.Text = openFileDialog1.FileName;
            }
    }

example of my program

Upvotes: 1

Views: 1340

Answers (1)

ewhoambra
ewhoambra

Reputation: 100

  • Click on your ToolStripMenuItem (which in this case its name I suppose is: "openFileToolStripMenuItem");
  • Goto Properties (Right-click on item > Properties or press F4 Key);
  • List item Click "Events";
  • List item Look for "Click" entry and on the right column type: openFileToolStripMenuItem_Click (which is your EventHandler name).

Your problem was that your component wasn't associated with the EventHandler you were coding.

To avoid this kind of issue with any type of event, add your EventHandler method name in the properties just like I explained to you above.


WARNING: after associating a EventHandler method, deleting it from the .cs file without removing from the Properties or the Designer.cs file, will cause CS1061 compile error. In this case, just remove the line that VS will mark as error.

For instance:

enter image description here

Double click it and remove the highlighted row:

enter image description here

Upvotes: 1

Related Questions