3

Let's say I have a form named Form1 with textBox and button in it.

I want to get the textBox value from another class on button click. I'm trying to do it like this, but it doesn't work:

class Main
{
    public void someMethod()
    {
        Form1 f1 = new Form1();
        string desiredValue = f1.textBox.Text;
    }
}

Forgive me for the dumb question, but I'm pretty new in C# and can't get this thing to work.

2
  • 1
    Please expand on "doesn't work". Unless you've initialised the value of the text box in the constructor for Form1 it will have the default value of string.Empty (i.e. ""). Commented Mar 8, 2012 at 22:44
  • Yes. It returns empty string. Commented Mar 8, 2012 at 22:53

6 Answers 6

13

You need to find the opened Form1 instead of creating another Form1, create the following class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class Class1
    {
        public void someMethod()
        {
            TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
            Debug.WriteLine(t.Text + "what?");
        }
    }
}

Then in your button click method

private void button1_Click(object sender, EventArgs e)
{
    Class1 c = new Class1();
    c.someMethod();
}
Sign up to request clarification or add additional context in comments.

4 Comments

It works! Thanks. This'll be my accepted answer. I just want to know is this easiest(fastest) way to get a textBox value from another class?
not sure if this is the fastest way, it is very easy. If you need to modify textbox value from another class, then consider define it as public. The idea is you need to find the instance of your form1
How can I access it if it's public? Just some example and I'll stop asking, cause I feel awful.
not much different, if it's public, we could write code like this - Form1 form1 = Application.OpenForms["Form1"] as Form1; Debug.WriteLine(form1.textBox1.Text + "what?");
6

your textBox is probably private, although that is as it should be. If you need the Text from the textbox you can expose it with a property

public string TextBoxText{ get { return textBox.Text; } }

4 Comments

I've tried with public and public statis, but again can't access the original textBox.
then you should access the form that has been created for you earlier. look in the program.cs, there you will see the main form that is created and shown. it is (my strong guess) that Form that you need to access. The someMethod should have some reference to that, you should just make sure to pass along the Form1 to someMethod().
So I'm doing it like in the example( Form1 f1 = new Form1(); ). In Program.cs I have: Application.Run(new Form1()); Then no success with f1.textBox.Text or Form1.textBox.Text Sorry for my stupid words again, but I want to get the idea :\
How is the someMethod created? I suppose you create the class in which the someMethod exists somewhere from Form1? If that is the case, you can pass the Form along as an argument to that class (via this). A first step in the right direction could be to add a System.Windows.Form myForm as an argument in the someMethod and go from there.
3

When you say

Form1 f1 = new Form1();

You are creating a whole new object with its own textbox.

If you want the value that is on that textbox of that form, you will need to refer to the same instance of Form1 where the user typed the value.

Comments

2

I think its because you are creating a new instance of form1 so you are actually only getting the textbox text from f1.

Comments

0

Is this pseudo-code, or is this the code you're actually trying to use?

If you're trying to use this code, what you're doing is creating a brand new Form1. Unless the constructor for Form1 puts something into your textbox, it will be empty at this point.

1 Comment

It's just for example, but I'm this is my idea, yes.
0

Form 1

public string pathret()
{
    return textBox.Text;
}

Form 2

class Main
{
    public void someMethod()
    {
        Form1 f1 = new Form1();
        string desiredValue = f1.pathret();
    }
}

1 Comment

This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. Here are some guidelines for How do I write a good answer?. From review.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.