Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best practices to call methods in the parent form and access gui elements in parent form in c#

Tags:

c#

winforms

I am developing a win form app and I found myself constantly requiring to access methods in my parent form say Form1 from another class be it a form class or just a class. I have some initializers in the constructor of my form 1 and hence I am not able to create an instance of the Form1. So I am not able to access the methods of Form1.

So I feel like this is a bad practice. However, there are certain instances that I don't know what else to do for instance consider this scenario. I have a class called ProcessData in which I have a method which receives a file, reads it line by line and process the data. Now I'm calling this method as a thread from my main form Form1. My requirement is as the data process I want to show the line currently under process in a multiline textbox in the main form Form1.

Previously what I did have I had everything in the same Form1 so I used a delegate, like

delegate void SetTextCallback(string text, Control ctrl);
private void SetText(string text, Control ctrl)
    {
        if (ctrl.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text, ctrl });
        }
        else
        {
            if (ctrl.GetType() == typeof(Label))
            {
                ctrl.Text = text;
            }
            else
            {
                ctrl.Text += Environment.NewLine + text;
            }
        }
    }

and I invoked this like SetText("text",Label1);

but if I am calling this from another class to refer Label1 I will need an instance of Form1 but I will not be able to create it, so what is the best practice to do it?

(I know I can just pass the text to SetText and handle the control there but I am going to use this same thing for a variety of textbox and label controls invoked from different classes)

like image 704
swordfish Avatar asked May 16 '11 05:05

swordfish


1 Answers

The way that I normally do this is to have the child form expose events corresponding to the logical actions and events on that form, for example:

/// <summary>
/// Occurrs when an item is selected in the form
/// </summary>
public event EventHandler<ItemSelectedEventArgs> ItemSelected;

/// <summary>
/// Fires the <see cref="ItemSelected" /> event
/// </summary>
protected void OnItemSelected(MyItem item) 
{
    var handler = this.ItemSelected;
    if (handler != null)
    {
        ItemSelectedEventArgs args = new ItemSelectedEventArgs();
        args.Item = item; // For example
        handler(this, args);
    }
}

The idea is that the logic of your parent form should respond to actions on your child form, rather than actions on your child form driving actions on the parent form - you should try to encapsulate the forms logic as much as possible (aka separation of concerns).

Also as a pattern it should be the parent / calling form that handles marshalling the calls across to the correct thread via InvokeRequired etc... rather than the child form - this will be unneccessary anyway unless you are doing work on background threads.

like image 112
Justin Avatar answered Oct 16 '22 02:10

Justin