-2

Sorry if this is a simple question, I'm newish to c#. To simplify this, I have one form (this is in WinForms), let's say Form1 that has a textbox (A rich textbox that allows multiple lines). During runtime I need to be able to access that textbox and get everything the user has written and put that into a string. I can do that just fine (within the form), the problem I run into is getting that text from another class. Right now I have it set up so I have this method, within the form:

public int getWords()
        {
            int goals = 15;
            string words = mainTextbox.Text;
            int count = words.Split(' ').Length;
            MessageBox.Show(count.ToString());
            return count;
        }

I tried to call the method from my class, let's call it Words, but the textbox text always came up as empty. It took me (a little too long) to realize that by creating a new instance of the form, I was basically resetting the textbox and that's why it came up as empty even though it wasn't.

I've looked through dozens of other questions like my own on StackOverflow but none of them really worked for me, nor did they explain how they worked. So my question is: How can I get the text from a textbox in Form1, to Class Words, during runtime. The simplest way possible, and the most dumbed-down explanation as possible, please and thank you!

RAZ
  • 11
  • 1
  • 1
    I think you need to do things the other way around. The class should not call the form but the form should use the class object and pass needed data to it. – Chetan Aug 03 '21 at 01:18
  • Does this answer your question? [Interaction between forms - How to change a control of a form from another form?](https://stackoverflow.com/questions/38768737/) • [Passing data between forms](https://stackoverflow.com/questions/4587952/) • [Passing variable between winforms](https://stackoverflow.com/questions/4247807/) • [Passing data between C# forms](https://stackoverflow.com/questions/39188704/) • [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/) –  Aug 03 '21 at 01:19
  • The getwords method of class should have string as a parameter and you should call this method from the form by passing `mainTextbox.Text` as parameter to that method. – Chetan Aug 03 '21 at 01:20
  • Chetan, it's more complicated than I described because it would take too long, but I can't do it the other way around. Olivier Rogier, I'll look into those, but would they help with passing data from a textbox between a form and a class? – RAZ Aug 03 '21 at 01:26
  • @RAZ you have started with incorrect design. You can choose to correct it later but that later might never come or it might get too late/messy to correct. You can use the solution suggested in the below answer to move ahead with you current design. – Chetan Aug 03 '21 at 02:33
  • "I was basically resetting the textbox" - No, you were not. You were creating a second `TextBox`. – Enigmativity Aug 03 '21 at 03:36

1 Answers1

-1

Ok, let's say your mainform class is called MainForm and your other class is called OtherClass.

From MainForm:

OtherClass otherClass = new OtherClass(this); // the keyword this is MainForm

Now in OtherClass:

MainForm m_MainForm; //class level variable
public OtherClass(MainForm mainForm) //constructor
{
    m_MainForm = mainForm; //Now m_MainForm is accessible from OtherClass
}

Now in MainForm, make the textbox public so it can be accessed from OtherClass.

You can call it's properties from OtherClass like:

string sTextBoxText = m_MainForm.MyTextBox.Text;
David.Warwick
  • 620
  • 1
  • 9
  • 28