Access data from another form.

B

Boki

Hi All,

There are two forms, when some click happen, it fires to set form2's
viable as enable.

The form2 is for user to input some text and then the data need to be
collected into form1's textbox.

I have created a function call as public, but I still can't use it in
form2.

Please advice. Thanks!

Best regards,
Boki.
 
B

Boki

Hi All,

There are two forms, when some click happen, it fires to set form2's
viable as enable.

The form2 is for user to input some text and then the data need to be
collected into form1's textbox.

I have created a function call as public, but I still can't use it in
form2.

Please advice. Thanks!

Best regards,
Boki.

I saw a method is to create another copy of form.

ex:
private Form2 form2 = new Form2();

but I think seems not too reasonable, isn't it ?

Boki.
 
J

Jon Skeet [C# MVP]

There are two forms, when some click happen, it fires to set form2's
viable as enable.

The form2 is for user to input some text and then the data need to be
collected into form1's textbox.

I have created a function call as public, but I still can't use it in
form2.

Why not? What happens when you try? You'll need a reference to the
appropriate instance of form2 within form1, of course.

Jon
 
B

Boki

Why not? What happens when you try? You'll need a reference to the
appropriate instance of form2 within form1, of course.

Jon

Thanks, but I am not very understand here.

If we create another form, and then access to this form, why the
modified result also change the original form.... why not only the
copied form..


Boki.
 
P

Peter Bradley

It's not entirely obvious from your post, but I assume you want the text
from Form2 to become visible in Form1 without the user carrying out any
specific action, such as clicking a button that says "Fill Form1", or
something.

In that case, what you want, I guess, is to be able somewhere in code in
Form2 to be able to do something like:

Form1.FillTextBox(SomeString);

This means that you need a reference to Form1 in Form2. The easiest way of
doing this is to provide Form2 with a constructor that passes in a reference
to Form1. In Form1, you provide the FillTextBox() method.

So, in Form1 you can do:

Form Form2 = new Form(this);
Form2.Visible = true;
Form2.Show();

(or whatever)

In the Form2 constructor, you can store the reference to Form1 in an
instance variable:

Form Form1Ref;
....

public Form2(Form formoneref)
{
...
Form1Ref = formoneref;
...
}

Then in Form2 somewhere you can do:

Form1Ref.FillTextBox(MyForm2TextBox.Text);

The code above is off the top of my head, and I don't usually do
Windows.Forms stuff, so the exact syntax might be a bit wayward: but you
should get the idea.

HTH


Peter
 
J

Jon Skeet [C# MVP]

Thanks, but I am not very understand here.

If we create another form, and then access to this form, why the
modified result also change the original form.... why not only the
copied form..

Have you perhaps got some static variables instead of instance
variables?

UI type work the same as "normal" types for the most part (except for
threading). Work out how you'd pass the information around in a non-UI
setting, then apply the same approach.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top