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

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

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

问题

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

  1. var UserControlee = new UserControl();
  2. 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的第二个窗体。

  1. public partial class MainForm : Form
  2. {
  3. public MainForm()
  4. {
  5. InitializeComponent();
  6. buttonRefresh.Click += onClickButtonRefresh;
  7. ucHost = new UserControlHostForm(this);
  8. }
  9. UserControlHostForm ucHost;
  10. protected override void OnLoad(EventArgs e)
  11. {
  12. base.OnLoad(e);
  13. ucHost.StartPosition = FormStartPosition.Manual;
  14. ucHost.Location = new Point(Location.X + Width + 10, Location.Y);
  15. ucHost.Show();
  16. }
  17. private void onClickButtonRefresh(object? sender, EventArgs e)
  18. {
  19. RefreshNeeded?.Invoke(this, EventArgs.Empty);
  20. }
  21. public event EventHandler? RefreshNeeded;
  22. }

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

  1. public partial class UserControlHostForm : Form
  2. {
  3. public UserControlHostForm(Form owner)
  4. {
  5. Owner = owner;
  6. InitializeComponent();
  7. // 如果UserControl尚未在设计时添加,请在此处添加。
  8. myUserControl = new MyUserControl
  9. {
  10. BackColor = Color.LightBlue,
  11. Dock = DockStyle.Fill,
  12. };
  13. Controls.Add(myUserControl);
  14. // 订阅主窗体事件
  15. ((MainForm)Owner).RefreshNeeded += onRefreshNeeded;
  16. }
  17. MyUserControl myUserControl;
  18. private void onRefreshNeeded(object? sender, EventArgs e)
  19. {
  20. myUserControl.Refresh();
  21. Visible = true;
  22. }
  23. protected override void OnFormClosing(FormClosingEventArgs e)
  24. {
  25. base.OnFormClosing(e);
  26. if (e.CloseReason.Equals(CloseReason.UserClosing))
  27. {
  28. e.Cancel = true;
  29. Hide();
  30. }
  31. }
  32. }

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

  1. class MyUserControl : UserControl
  2. {
  3. public MyUserControl()
  4. {
  5. Controls.Add(label);
  6. }
  7. public new void Refresh()
  8. {
  9. base.Refresh();
  10. load();
  11. }
  12. Label label = new Label { Location = new Point(10, 100), AutoSize = true, };
  13. int debugCount = 0;
  14. private void load()
  15. {
  16. label.Text = $"Count = {++debugCount}: 你的自定义SQL加载代码在这里";
  17. }
  18. }

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

英文:

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#从另一个窗体刷新用户控件?

  1. public partial class MainForm : Form
  2. {
  3. public MainForm()
  4. {
  5. InitializeComponent();
  6. buttonRefresh.Click += onClickButtonRefresh;
  7. ucHost = new UserControlHostForm(this);
  8. }
  9. UserControlHostForm ucHost;
  10. protected override void OnLoad(EventArgs e)
  11. {
  12. base.OnLoad(e);
  13. ucHost.StartPosition = FormStartPosition.Manual;
  14. ucHost.Location = new Point(Location.X + Width + 10, Location.Y);
  15. ucHost.Show();
  16. }
  17. private void onClickButtonRefresh(object? sender, EventArgs e)
  18. {
  19. RefreshNeeded?.Invoke(this, EventArgs.Empty);
  20. }
  21. public event EventHandler? RefreshNeeded;
  22. }

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

  1. public partial class UserControlHostForm : Form
  2. {
  3. public UserControlHostForm(Form owner)
  4. {
  5. Owner = owner;
  6. InitializeComponent();
  7. // If the UserControl hasn't already been
  8. // added in the Designer, add it here.
  9. myUserControl = new MyUserControl
  10. {
  11. BackColor = Color.LightBlue,
  12. Dock = DockStyle.Fill,
  13. };
  14. Controls.Add(myUserControl);
  15. // Subscribe to the MainForm event
  16. ((MainForm)Owner).RefreshNeeded += onRefreshNeeded;
  17. }
  18. MyUserControl myUserControl;
  19. private void onRefreshNeeded(object? sender, EventArgs e)
  20. {
  21. myUserControl.Refresh();
  22. Visible = true;
  23. }
  24. protected override void OnFormClosing(FormClosingEventArgs e)
  25. {
  26. base.OnFormClosing(e);
  27. if (e.CloseReason.Equals(CloseReason.UserClosing))
  28. {
  29. e.Cancel = true;
  30. Hide();
  31. }
  32. }
  33. }

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.

  1. class MyUserControl : UserControl
  2. {
  3. public MyUserControl()
  4. {
  5. Controls.Add(label);
  6. }
  7. public new void Refresh()
  8. {
  9. base.Refresh();
  10. load();
  11. }
  12. Label label = new Label { Location = new Point(10, 100), AutoSize = true, };
  13. int debugCount = 0;
  14. private void load()
  15. {
  16. label.Text = $"Count = {++debugCount}: Your custom SQL load code goes here";
  17. }
  18. }

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:

确定