How to Reload a Form in C# When a Button in Another Form is Clicked

When developing a C# application, you may come across a scenario where you need to reload a form when a button in another form is clicked. This can be achieved by passing a reference of the main form to the secondary form and calling a method on the main form to update its content.

How to Implement the Solution

To better understand the solution, let’s consider an example where we have two forms: frmMain and frmSettings. The frmMain form is the main form of our application, and the frmSettings form is a secondary form that allows the user to add products.

Step 1: Pass a Reference of the Main Form to the Secondary Form

In the frmSettings form, we need to add a constructor that accepts a reference to the main form. This will allow us to access the main form’s methods and properties.

private frmMain main;

public frmSettings(frmMain mainForm)
{
  main = mainForm;
  InitializeComponent();
}

Step 2: Call the Main Form’s Method When the Button is Clicked

In the frmSettings form, when the user clicks the submit button, we can call a method on the main form to add the product. In this example, let’s assume we have a text box (textBox1) where the user enters the product name.

private void button1_Click(object sender, EventArgs e)
{
  main.AddProduct(textBox1.Text);
}

Step 3: Update the Main Form

In the frmMain form, we need to define the AddProduct method that will be called from the frmSettings form. This method will add the product to a list and update the form’s content.

private frmSettings settings;
private List<string> products = new List<string>();

public frmMain()
{
  InitializeComponent();
  // Load products from somewhere
}

public void AddProduct(string product)
{
  products.Add(product);
  UpdateForm();
}

private void UpdateForm()
{
  comboBoxProducts.Items.Clear();
  comboBoxProducts.Items.AddRange(products.ToArray());

  // Other updates
}

Step 4: Show the Secondary Form

In the frmMain form, when the user clicks a button (e.g., “Settings”), we need to show the frmSettings form. We can check if the settings variable is null and create a new instance of the frmSettings form if it is.

private void button1_Click(object sender, EventArgs e)
{
  if (settings == null)
  {
    settings = new frmSettings(this);
  }
  settings.Show();
}

Step 5: Call the Update Method

To reload the form when the button in the frmSettings form is clicked, we can call the UpdateForm method from anywhere in the frmMain form. For example, we can add another button that triggers the update.

private void button2_Click(object sender, EventArgs e)
{
  UpdateForm();
}

Conclusion

By passing a reference of the main form to a secondary form and calling a method on the main form, we can reload the form when a button in another form is clicked. This allows us to update the content of the main form based on user actions in the secondary form.

Categories C#

Related Posts

C# Triple Double Quotes: What are they and how to use them?

In C# programming language, triple double quotes (“””) are a special syntax known as raw string literals. They provide a convenient way to work with strings that contain quotes or embedded language strings like JSON, XML, HTML, SQL, Regex, and others. Raw string literals eliminate the need for escaping characters, making it easier to write ...

Read more

Best Practices in Using a Lock in C#

What is a Lock? A lock in C# is implemented using the lock keyword, which ensures that only one thread can enter a specific section of code at a time. When a thread encounters a lock statement, it attempts to acquire a lock on the specified object. If the lock is already held by another ...

Read more

Usage of ‘&’ versus ‘&&’ in C#

‘&’ Operator The ‘&’ operator in C# is a bitwise AND operator. It operates at the bit level, meaning that it performs the AND operation on each corresponding pair of bits in the operands. This operator is commonly used when working with binary data or performing low-level bit manipulation. For example, consider the following code ...

Read more

How to Add a Badge to a C# WinForms Control

Have you ever wanted to add a badge to a C# WinForms control? Maybe you want to display a notification count on a button or indicate the status of a control. In this article, I will show you how to easily add a badge to a C# WinForms control using a static Adorner class. What ...

Read more

Leave a Comment