Home > Back-end >  How To Refresh User Control From Another Form In WinForms Using C#?
How To Refresh User Control From Another Form In WinForms Using C#?

Time:01-09

I have tried several ways now to achieve this. Amongst other things using this code:

var UserControlee = new UserControl (); UserControlee .load_data();

this however does literally nothing. From what I have read this is because I should not use a new instance of the form. But all the suggested ways like using var UserControlee = new UserControl (this); don't work.

I by the way insert my data through SQL and currently trie to use the method load(), which works and refreshes the DataGridView when used on UserControl.

CodePudding user response:

Your question is How To Refresh User Control From Another Form In WinForms Using C#. The debugging details are missing in your question. Nevertheless, a general answer for one way of doing that is to have the requesting form fire an event when a refresh is needed.

Here is a minimal example of a MainForm that features a [Refresh] button to test fire this event. The main form also creates the second form that hosts the UserControl.

screenshot

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        buttonRefresh.Click  = onClickButtonRefresh;
        ucHost = new UserControlHostForm(this);
    }
    UserControlHostForm ucHost;
    protected override void onl oad(EventArgs e)
    {
        base.OnLoad(e);
        ucHost.StartPosition = FormStartPosition.Manual;
        ucHost.Location = new Point(Location.X   Width   10, Location.Y);
        ucHost.Show();
    }
    private void onClickButtonRefresh(object? sender, EventArgs e)
    {
        RefreshNeeded?.Invoke(this, EventArgs.Empty);
    }
    public event EventHandler? RefreshNeeded;
}

The second form subscribes to the main form event in its constructor method and calls myUserControl.Refresh() in response.

public partial class UserControlHostForm : Form
{
    public UserControlHostForm(Form owner)
    {
        Owner = owner;
        InitializeComponent();

        // If the UserControl hasn't already been
        // added in the Designer, add it here.
        myUserControl = new MyUserControl
        {
            BackColor = Color.LightBlue,
            Dock = DockStyle.Fill,
        };
        Controls.Add(myUserControl);

        // Subscribe to the MainForm event
        ((MainForm)Owner).RefreshNeeded  = onRefreshNeeded;
    }
    MyUserControl myUserControl;
    private void onRefreshNeeded(object? sender, EventArgs e)
    {
        myUserControl.Refresh();
        Visible = true;
    }
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        base.OnFormClosing(e);
        if (e.CloseReason.Equals(CloseReason.UserClosing))
        {
            e.Cancel = true;
            Hide();
        }
    }
}

The custom UserControl implements the Control.Refresh() method in an app-specific manner, for example by calling load(). Here is a mock implementation that demonstrates that it's working.

class MyUserControl : UserControl
{
    public MyUserControl()
    {
        Controls.Add(label);
    }
    public new void Refresh()
    {
        base.Refresh();
        load();
    }
    Label label = new Label { Location = new Point(10, 100), AutoSize = true,  };
    int debugCount = 0;
    private void load()
    {
        label.Text =  $"Count = {  debugCount}: Your custom SQL load code goes here";
    }
}

CodePudding user response:

To refresh the data in a UserControl using the load_data() method, you need to call the method on an instance of the UserControl that is already created and added to the form.

Here is an example of how you can do this: First, create a public method in your UserControl that calls the load_data() method and refreshes the data. This method can be called from the form that contains the UserControl:

public void RefreshData()
{
    load_data();
}

Add the UserControl to the form as a field or property:

public partial class Form1 : Form
{
    // Declare a field for the UserControl
    private UserControl userControl;

    public Form1()
    {
        InitializeComponent();

        // Initialize the UserControl and add it to the form
        userControl = new UserControl();
        this.Controls.Add(userControl);
    }
}

Call the RefreshData() method on the UserControl instance when you want to refresh the data:

userControl.RefreshData();
  • Related