C Realization of winform Method of Refreshing Parent Form with Child Form and Changing Parent Form Control Value with Child Form

  • 2021-07-22 11:06:09
  • OfStack

This article describes the example of C # implementation of winform with child form to refresh the parent form and child form to change the value of the parent form control method. Share it for your reference. The details are as follows:

Method 1:

With delegates, Form2 and Form3 are the same group

Form2


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
namespace TestMouseMove 
{ 
  public delegate void SetVisiableHandler(); 
  public partial class Form2 : Form 
  { 
    public Form2() 
    { 
      InitializeComponent(); 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
      Form3 frm = new Form3(new SetVisiableHandler(SetVisiable)); 
      frm.Show(); 
    } 
    private void SetVisiable() 
    { 
      SetVisiable(this.label1, !this.label1.Visible); 
    } 
    private void SetVisiable(Control control, bool visiable) 
    { 
      if (this.Controls.Contains(control)) 
      { 
        control.Visible = visiable; 
      } 
    } 
  } 
} 

Form3


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
namespace TestMouseMove 
{ 
  public partial class Form3 : Form 
  { 
    private SetVisiableHandler m_setVisible; 
    public Form3(SetVisiableHandler setvisible) 
    { 
      InitializeComponent(); 
      this.m_setVisible = setvisible; 
    } 
    private void btnVisible_Click(object sender, EventArgs e) 
    { 
      if (this.m_setVisible != null) 
      { 
        this.m_setVisible(); 
      } 
    } 
  } 
} 

Method 2:

With variables, Form4 and Form5 are the same group

Form4


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
namespace TestMouseMove 
{ 
  public partial class Form4 : Form 
  { 
    public Form4() 
    { 
      InitializeComponent(); 
    } 
    #region  The child window refreshes the value of the parent window  
    private string strLabel1 = ""; 
    public string StrLabel1 
    { 
      get 
      { 
        return strLabel1; 
      } 
      set 
      { 
        strLabel1 = value; 
        this.label1.Text = strLabel1; 
      } 
    } 
    #endregion 
    private void button1_Click(object sender, EventArgs e) 
    { 
      Form5 form5 = new Form5(this);// Pay attention to pass a message here this 
      form5.Show(); 
    } 
  } 
} 

Form5


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
namespace TestMouseMove 
{ 
  public partial class Form5 : Form 
  { 
    Form4 form4 = new Form4(); 
    public Form5(Form4 formFrm)// This constructor has parameters in it  
    { 
      form4 = formFrm; // This must have  
      InitializeComponent(); 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
      form4.StrLabel1 = this.textBox1.Text; 
    } 
  } 
}

I hope this article is helpful to everyone's C # programming.


Related articles: