Multiple forms for PocketPC?

G

Guest

This is the problem:Let's say that I have two windows forms(I am making an
mobile device application) and I have a textBox in Form1.In the other Form
(form2) i have a textBox and a button.So I want to implement a code in the
OnClick event of the button that will copy textBox1.text from Form1 into
textBox1.text in Form2.So I did:

Form1 f=new Form1();
textBox1.text=f.textBox1.text;


It is not working... :( .I set the modifiers of text boxes on public but it
is not working.Any ideas?
 
D

Darren Shaffer

Your line of code Form1 f = new Form1() instantiates a NEW Form1,
not the same one that contains the text you want to copy from the textbox.

To pass values between forms, you have the following options:

1. add a constructor to Form2 that takes a string and pass the value
of textBox1 from Form1 into this constructor. this is not useful in
the situation where you have a lot of state information to pass to
another form. Example: Form2 f2 = new Form2(textBox1.text);
f2.Show();

2. add accessors to Form2 that can set local member variables to the
values you want to pass from Form1. So you'd be writing code like
this:
Form2 f2 = new Form2();
f2.ValueYouWantToPassToForm2 = this.textBox1.text;
f2.Show();

3. make your forms singletons so that you always have at most one instance
of them on device and can refer to whatever internal or public state
information
you need, at any time.

4. make a single Globals class that is a singleton that contains accessors
for any values
you want all of your forms to have access to.

-Darren
 
F

Fabien

Hello,
I think there is a mistake in your message. The button is on form2 or form1
?
If it is on form1, use Darren's methods otherwise:
1) in form2
create a delegate
public delegate void TextBoxValueHandler(string newValue);
create an event :
public event TextBoxValueHandler TextBoxValueChanged;
in the Click event of the button add the code
if (TextBoxValueChanged != null)
TextBoxValueChanged(textBox1.Text);
2) in form1
when you create the new instance of Form2, before showing the form, add
the code
f.TextBoxValueChanged += new
TextBoxValueHandler(this.form2_textboxChanged);
in the form2_textboxChanged function add the code
private void form2_textboxChanged(string newValue)
{
textBox1.Text = newValue;
}

Fabien
 

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