user1763295
user1763295

Reputation: 1088

Activate tool strip click event from another form

I need to send a click event to refreshToolStripMenuItem from another form. Here is what I have, for some reason it doesn't work. Help please.

Menu item click:

public void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
    noteslist.Items.Clear();
    idlist.Items.Clear();
    setnotes();
}

Code used to send event:

frmnotes notes = new frmnotes();
notes.refreshToolStripMenuItem_Click(this, e);
this.Close();

Upvotes: 0

Views: 1420

Answers (1)

jAC
jAC

Reputation: 5324

Dont call the event itself. It's bad code. Put the create an own protected void updateMyList() Method.

    internal void updateMyList()
    {
         noteslist.Items.Clear();
         idlist.Items.Clear();
         setnotes();
    }

Then call the update-method from within your event.

    private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
    {
        updateMyList();
    }

Then simply call the update-method from your form:

       frmnotes notes = new frmnotes();
       notes.updateMyList();
       this.Close();

Btw.: Set the modifier of your Click events i.e. refreshToolStripMenuItem_Click to private. You never should call them from outside the form. Take a look at the MVC pattern for more info. It really helps.

Upvotes: 1

Related Questions