Reputation: 13
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;
}
}
Upvotes: 1
Views: 1340
Reputation: 100
ToolStripMenuItem
(which in this case its name I suppose is: "openFileToolStripMenuItem");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:
Double click it and remove the highlighted row:
Upvotes: 1