Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jgram925/e964acac035492673d4f0dddf264525c to your computer and use it in GitHub Desktop.
Save jgram925/e964acac035492673d4f0dddf264525c to your computer and use it in GitHub Desktop.
C# Winform - Calling a form function from another form.md
public partial class MainForm : Form
{        
    public void refreshInventoryDGV()
    {
        // function that will be used in other form
    }
    
    private void addButton_Click(object sender, EventArgs e)
    {
        // need to pass "this" form instance to other form
        ItemForm addItemForm = new ItemForm(this);
        addItemForm.Show();
    }


public partial class ItemForm : Form
{               
    private MainForm fromForm { get; set; }

    public ItemForm(MainForm thatForm)
    {
        InitializeComponent();
        // set the "this" instance of main form
        fromForm = thatForm;            
    }
    
    private void saveButton_Click(object sender, EventArgs e)
    {
        // function from main form
        fromForm.refreshInventoryDGV();
    }
@phillipgibbings
Copy link

just wanted to say thanks, this helped me out in my application

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment