如何在WinForms中使用C#从另一个窗体刷新用户控件?

huangapple go评论75阅读模式
英文:

How To Refresh User Control From Another Form In WinForms Using C#?

问题

我已经尝试了几种方法来实现这个目标。其中一种方法是使用以下代码:

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

然而,这实际上什么都没有做。根据我所了解,这是因为我不应该使用窗体的新实例。但是所有建议的方法,比如使用 var UserControlee = new UserControl(this); 都不起作用。

顺便说一下,我通过SQL插入我的数据,目前尝试使用 load() 方法,当在UserControl上使用时,它可以工作并刷新DataGridView。

英文:

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.

答案1

得分: 0

你的问题是关于如何在WinForms中使用C#从另一个窗体刷新用户控件。在你的问题中缺少调试详细信息。尽管如此,一种通用的做法是在需要刷新时,让请求窗体触发一个事件。

以下是一个示例代码,演示了一个名为MainForm的主窗体,其中包含一个用于触发此事件的[Refresh]按钮。主窗体还创建了承载UserControl的第二个窗体。

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        buttonRefresh.Click += onClickButtonRefresh;
        ucHost = new UserControlHostForm(this);
    }
    UserControlHostForm ucHost;
    protected override void OnLoad(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;
}

第二个窗体在其构造方法中订阅了主窗体的事件,并在响应事件时调用了myUserControl.Refresh()

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

        // 如果UserControl尚未在设计时添加,请在此处添加。
        myUserControl = new MyUserControl
        {
            BackColor = Color.LightBlue,
            Dock = DockStyle.Fill,
        };
        Controls.Add(myUserControl);

        // 订阅主窗体事件
        ((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();
        }
    }
}

自定义的UserControl以应用程序特定的方式实现了Control.Refresh()方法,例如通过调用load()。以下是一个模拟实现,演示了它是如何工作的。

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}: 你的自定义SQL加载代码在这里";
    }
}

这些代码组成了一个简单的示例,演示了如何从一个窗体刷新另一个窗体中的用户控件。

英文:

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.

如何在WinForms中使用C#从另一个窗体刷新用户控件?

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        buttonRefresh.Click += onClickButtonRefresh;
        ucHost = new UserControlHostForm(this);
    }
    UserControlHostForm ucHost;
    protected override void OnLoad(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";
    }
}

huangapple
  • 本文由 发表于 2023年1月8日 22:42:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定