S
S
Stas Korostelev2015-10-09 22:20:34
C++ / C#
Stas Korostelev, 2015-10-09 22:20:34

How to access a form element from a class?

Good day.
I am using WinForms C#. There is label1 on the form, there is a separate class, suppose it is called Test.cs. How can I access Form1's label1.Text from this class (Test)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Makarov, 2015-10-09
@ZorG761

have label1 set the access modifier public (for example) .... and contact.

S
sivabur, 2015-10-09
@sivabur

It is best to make a public method (or property) in the form that returns a reference to textBox1.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
.....................
        public TextBox getTextBox()
        {
            return textBox1;
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question