Hi,

I'm just starting my way in C# and I have this question/problem:

I've created a "Windows form" project with several forms, where Form1 is the main form and it is also has a statusStrip control. I've managed to change the text in it with a method in Form1, but I can't manage to get access to that method from other forms so I could update it from them. How can I get access to that method from the other forms? Are there other ways to control the statusstrip in form1 from the other forms?


Thanks,
Udi Goldstein

Recommended Answers

All 6 Replies

Have a look here. That should get you started. Let me know if you have any questions.

Hi,

Thanks for the quick answer, but it didn't work. I actually tried this way before - The problem is that it goes to the method in Form1 but it doesn't write the text in it because I've created a new form1 object instead of writing it to the current one...

What are you doing with the old form1 object?

Hi,

I don't do anything with it, that's the problem. I want the statusbar in it to be updated. I get access to the method in it but it just won't write to the statusbar.

Here's an example of what I'm trying to do:

Let's say that I'm creating a new browser. The main form is the browser window, where I can see webpages, and it has a statusbar with information about loading times, etc. I've made another form, called "Internet Options", just like IE. All I want to do is once I finish updating the "Internet options" form and press "OK", I want the statusbar to display a message like "Options were updated successfully".

thanks,
Udi G.

Ok - you can create a public string on form2, and if form2 is a child of form1 you should be able to access the string from form1. Instead of updating the status strip from form2, try waiting until control is passed back to form1 and update it then using the public string.

You have two choices.
You can pass a reference to Form1 into Form2 in its constructor to allow you to pass the data back, or you can add a Result property to Form2 which you read from Form1 in order to update the status.

Solution One Example:

public partial class Form1 : Form
{
    private void ButtonClick(object sender, EventArgs e)
    {
        Form2 child = new Form2(this);
        child.Show();
    }

    //public property to set statusbar
    public string SetStatus
    {
        set { statusBar.Text = Value; }
    }
}

public partial class Form2 : Form
{
    public void Form2(Form1 parent)
    {
        parent.SetStatus = "string from child";
    }
}

I have used a public property as it is bad OO practice to directly alter a member of the parent from a child; ENCAPSULATION is one of the oft forgotten cornerstones of OO design :)

Solution Two Example:

public partial class Form1 : Form
{
    private void ButtonClick(object sender, EventArgs e)
    {
        Form2 child = new Form2();
        child.ShowDialog();
        OptionResult result = child.getResult;
        if(result == OptionResult.Success)
        { 
            this.StatusBar.Text = "Success";
        }
    }
}

public partial class Form2 : Form
{
    public void Form2()
    {
        this.result = OptionResult.Success;
    }

    OptionResult result = OptionResult.Cancelled;
    //public property to get result
    public OptionResult getResult
    {
        get { return result; }
    }
}

public enum OptionResult
{
    Success,
    Failed,
    Cancelled
}

Very simple example that sets the result on form creation. I have shown the options modally so code excecution pauses on Form1 until Form2 closes. If you prefer to have it modeless you can hook an event handler in Form1 to run when Form2 is closed.

Hope this gives you some ideas to work with :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.