Controlling the properties of a TextBox from a different Form

D

David Hubball

Hi

I hope someone can give me a little push in the right direction.

I'm new c#. My problem is that I've got a Main Form and another form
(named SubForm). I'm trying to enter some details on SubForm which I
would like to update a Text Box control on my Main Form after I click
on an OK button on the Sub Form (which will also clear the Sub Form
out of the way).

I hope I've made sense if someone can give me a pointer - Just when I
thought that I understood about Objects as well -fell at the first
hurdle - typical!!!

Thanks to anyone who can help.

Kind Regards
David
 
J

Joe Cool

Hi

I hope someone can give me a little push in the right direction.

I'm new c#. My problem is that I've got a Main Form and another form
(named SubForm). I'm trying to enter some details on SubForm which I
would like to update a Text Box control on my Main Form after I click
on an OK button on the Sub Form (which will also clear the Sub Form
out of the way).

I hope I've made sense if someone can give me a pointer - Just when I
thought that I understood about Objects as well -fell at the first
hurdle - typical!!!

Thanks to anyone who can help.


There a several ways to do what you want to do.

1. Use an event in the main form that the subform subscribes to and
raises. Use custom event args to pass data to the main form.

2. Use a public method in the main form that the subform invokes. If
the subform is not an MdiChild, it will need a pointer to the parent,
use a property in the subform to pass that.

3. Set up properties in the subform to pass the information back to
the main form.

HTH.
 
D

David Hubball

Thanks so much for all your replies.

Please could anyone expand on how to Set up properties in the subform
to pass the information back to the main form. I'm still a beginner so
if you think I should maybe go away and study a book first - please
could you recommend me one also.

Thanks
David
 
J

Joe Cool

Thanks so much for all your replies.

Please could anyone expand on how to Set up properties in the subform
to pass the information back to the main form. I'm still a beginner so
if you think I should maybe go away and study a book first - please
could you recommend me one also.

Any form is nothing more than a class. Set up the properties in it the
same way you would set up properties in an ordinary class, ie, create
a private class variable for each property and set up a property with
a getter and setter for each of them.
 
J

Jeff Johnson

Please could anyone expand on how to Set up properties in the subform
to pass the information back to the main form. I'm still a beginner so
if you think I should maybe go away and study a book first - please
could you recommend me one also.

They're just properties, except instead of using a private variable as the
backing store, you just use the Text property of the text box. Something
like this:

public string Name
{
get { return nameTextBox.Text; }
set { nameTextBox.Text = value; }
}
 
V

vanderghast

Form1 a WinForm, two controls, a button and a textbox.
Form2, same as Form1.

Button on Form1 open Form2 in dialog mode. The whole code for the button
click event:


-----------------------
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this);
this.textBox1.Text = f2.SomeArg;
}
------------------------


And the code will be 'suspended' just after the line f2.ShowDialog(his);

Button on Form2 will 'appear' to close Form2, but will make it invisible,
instead. The form still exists, though. As side effect, code from Form1,
which was suspended, now continue, and the call to transfer of information:
is with

this.textBox1.Text = f2.SomeArg;


where f2 is still alive and well, and its get-property SomeArg effectively
transfer the captured information to form1. f2 die getting out of scope,
here (or at least, seems to be candidate for garbage collection).


Aded code for Form2:

----------------------
internal String SomeArg
{
get
{
return this.textBox1.Text;
}
}

private void button1_Click(object sender, EventArgs e)
{
// do required form validate, e.cancel=true if fails, otherwise
this.Visible = false;
}
--------------------------

The property is required since the control themselves are private.


Vanderghast, Access MVP
 
D

David Hubball

But how do I pass the properties from the sub form to the main form?
thanks again if you can help a bit more please.
 
J

Joe Cool

But how do I pass the properties from the sub form to the main form?
thanks again if you can help a bit more please.

Peter described that in his reply where he expanded on my first reply.

if (subForm.ShowDialog() == DialogResult.OK)
{
Textbox1.Text = subForm.NewText;
}

Textbox1 is the textbox in the main form. NewText is the property in
the subform that is passing back a value from the subform.
 

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