matt pruent
matt pruent

Reputation: 35

How do you send data from a form to a class?

I have a Class named Testing and a Form called TitleScreen. In TitleScreen I have a textBox1 who's text I would like to be passed to a Class and then pass it back to my Form into a textBox2.

I know how to do only the basics in C# so if you try and make it simple as possible.

Upvotes: 3

Views: 7321

Answers (3)

tomRedox
tomRedox

Reputation: 30443

I would suggest you want to look at the concept of data binding, whereby you bind the controls on your forms to the properties of the underlying objects (instances of your classes).

Binding removes the need to write code to cross-load the data from the class into the form and back again, instead you can then say "text box 1 is bound to this property of my class". Then, when you update the value of the textbox the data is automatically placed into the chosen property of your class instance. Typically you then have a save button that calls a save method on your class to persist the data to your data store (database or whatever).

It is perfectly reasonable to bind more than one control on your form to the same property on your underlying class, so in your example you can bind both textBox1 and textBox2 to the same property on your class. Then, once you've implemented databinding, when you change the value in textBox1, the value will automatically be reflected in textBox2, either on each keystroke or when the field is validated (typically when you move focus to another control).

This is the microsoft documentation on Winforms binding which covers everything you need: https://msdn.microsoft.com/en-us/library/ef2xyb33(v=vs.110).aspx

Upvotes: 0

Parham.D
Parham.D

Reputation: 1168

In your Class:

public class Class1
    {
        public static string SeparateName(string fullName)
        {
            string[] wordsInText = fullName.Split(' ');
            return wordsInText[0];
        }
    }

In your Form:

private void button1_Click(object sender, System.EventArgs e)
        {
            textBox2.Text = Class1.SeparateName(textBox1.Text);
        }

"I highly recommend that you read a book or tutorial that targets new users, otherwise there will be holes in your understanding of the language and the frameworks."

Upvotes: 2

annonymously
annonymously

Reputation: 4708

It sounds like you want to perform an operation on the textbox's value and then print the result in another textbox.

You can write a method (function) that accepts an argument of type String and perform the operation in that method. The method can then set the Text property of the textbox to the result.

If you're asking how to input code in a winforms project, you can double-click the background of the form to reach its code. (At least in Visual Studio)

If you don't know how to do the above suggestions, I highly recommend that you read a book or tutorial that targets new users, otherwise there will be holes in your understanding of the language and the frameworks.

Upvotes: 0

Related Questions