Jos
Jos

Reputation: 1852

Multiple .cs files and access to Form

I'm trying to write my first program in C# without the use of a tutorial. To ensure that I adopt from the start good coding practices, I want to create each class in an different .cs file. However, I'm running into some troubles when trying to access the elements of the program in such an .cs file.

For example, I have an Form1.cs with an Label and a Start button. When clicking on the start button, a text should appear in the Label. So:

In Form1.cs I have:

namespace TestProgram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            WriteToLabel message = new WriteToLabel();
            message.WelcomeMessage();
        }
    }
}

And in my separate WriteToLabel.cs file:

namespace TestProgram
{
    public class WriteToLabel
    {
        public void WelcomeMessage()
        {
            Form1 myForm = new Form1();
            //myForm..  --> myForm doesn't have an 'outputLabel'?
            outputLabel.Text = "Welcome!"; // This returns an error: 'The name outputLabel does not exits in the current context'.
        }
    }
}

'outputLabel' is the (Name) I've given the label, and this is in accordance to the name in Form1.Designer.cs. Both files are using the same components such as 'using System';.

However, from my WriteToLabel.cs file I can't seem to access the Form which holds my program. I did manage to succeed to create different .cs files in an Console Application, which only added to my confusion. So, I have two questions:

First, how can I access the Form from a separate class (i.e. not an partial class) in a separate file? Second, is this the good way to do it, or is it inefficient to create multiple instances of different classes?

Any thoughts or ideas are highly welcome,

Regards,

Upvotes: 0

Views: 5317

Answers (3)

Mark Cidade
Mark Cidade

Reputation: 100007

You need to make sure that Form1.outputLabel has public or internal visibility.

You only need something like a LabelWriter class if the class is going to share a significant amount of state or private methods. If all you have is a bunch of methods that set properties on separate objects, you might as well just keep it as a method on the same object (in this case the Form1 object):

void startButton_Click(object sender, EventArgs e)
    {
        displayWelcomeMessage();
    }

void displayWelcomeMessage()
    {
       this.outputLabel = "Welcome!";
    }

Upvotes: 1

ba__friend
ba__friend

Reputation: 5913

The designer automatically creates controls as private fields, because of that your WriteToLabel class can't access it. You need to change that. Also a good start would be to change the class to something like that:

namespace TestProgram
{
    public class WriteToLabel
    {
        Form1 form;

        public WriteToLabel(Form1 form)
        {
            this.form = form;
        }

        public void WelcomeMessage()
        {
            //Form1 myForm = new Form1();
            //myForm..  --> myForm doesn't have an 'outputLabel'?
            form.outputLabel.Text = "Welcome!";
        }
    }
}

Upvotes: 2

Matthew Abbott
Matthew Abbott

Reputation: 61599

You're actually instantiating a new instance of Form1, whereas you need to pass in a reference to your existing instance:

public void WelcomeMessage(Form1 form)
{
    form.outputLabel.Text = "Welcome";
}

You also need to ensure that outputLabel is a public (or internal) property/field of Form1 so you can set the value accordingly. Then the calling code is slightly different:

private void startButton_Click(object sender, EventArgs e)
{
    WriteToLabel message = new WriteToLabel();
    message.WelcomeMessage(this);
}

Upvotes: 2

Related Questions