C# , winform, invok method by event

ahmed omar 181 Reputation points
2022-12-04T17:13:59.237+00:00

hello,
I have a windows application C# code.
I have two forms, FormA and FormB .
FormA has a method that gets the count of elements, let's assume the current count is 5 and view this number in label1, and FormA has a button to show FormB.
FormB has a button to reduce the count of the elements in FormA, when I close FormB the lebel1 still has the old count and will show the new number only when I close FormA and load it again.

so please is there any way to refresh the label1 without closing the form,

Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-12-04T17:44:34.68+00:00

    Best way is to have an event in the child form which the parent form subscribes too for updating controls on the parent form from the child form.

    In this case, the main form is MainForm and the child form name is Child Form. The ChildForm has a NumericUpDownControl to control the value from the MainForm named numericUpDown1. I added a label too to show other controls can be used also.

    These are for passing the value back to MainForm

    public delegate void OnPassingNumber(int value);  
    public event OnPassingNumber PassingNumber;  
    

    Complete Child Form

    public partial class ChildForm : Form  
    {  
      
        public delegate void OnPassingNumber(int value);  
        public event OnPassingNumber PassingNumber;  
      
        public ChildForm()  
        {  
            InitializeComponent();  
        }  
        public ChildForm(int value)  
        {  
            InitializeComponent();  
      
            numericUpDown1.Value = value;  
        }  
        private void PassDataButton_Click(object sender, EventArgs e)  
        {  
            PassingNumber?.Invoke((int)numericUpDown1.Value);  
        }  
    }  
    

    MainForm

    public partial class MainForm : Form  
    {  
        public MainForm()  
        {  
            InitializeComponent();  
            numericUpDown1.Value = 10;  
            label1.DataBindings.Add("Text", numericUpDown1, "Value");  
        }  
      
        private void ShowChildButton_Click(object sender, EventArgs e)  
        {  
            ChildForm childForm = new ChildForm((int)numericUpDown1.Value);  
      
            childForm.PassingNumber += ChildFormOnPassingNumber;  
      
            try  
            {  
                childForm.ShowDialog();  
            }  
            finally  
            {  
                childForm.Dispose();  
            }  
      
        }  
      
        private void ChildFormOnPassingNumber(int value)  
        {  
            numericUpDown1.Value = value;  
        }  
      
    }  
    

    266933-share.png


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.