469,356 Members | 1,873 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Is it possible to access a control in another form?

Hi,

In a MDI environment I am in one form (main or child). How can access a
value from a text box placed in another form? An example please.
Thanks,
Doru
Feb 2 '06 #1
10 16302
Doru,

You would have to have a reference to the form. If you have a
reference, then you have to expose a method/property/field which will expose
the TextBox publically. Once you do that, you can just call the
method/property/field and then do what you need to do with it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Doru Roman" <do*******@rogers.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Hi,

In a MDI environment I am in one form (main or child). How can access a
value from a text box placed in another form? An example please.
Thanks,
Doru

Feb 2 '06 #2
Thanks for the reply.
I have created a reference of the form, but I don't know what to do next.
What do you mean by: "to expose a property"?
What I tried was to transfer the value of the text box into the form's Text
property. But I would like to get the textbox's text property directly.
Feb 2 '06 #3
In the form that contains the Textbox, you want to do something like
this:

public string CustomerName
{
get { return this.customerNameTextBox.Text; }
}

then from the other form you can say:

form1.CustomerName

to get the value.

(Of course, change the name of the property, etc., to something more
meaningful for your program.)

BTW, I disagree with Nicholas's suggestion. You most likely _don't_
want to expose the entire Textbox via a property when all you want is
the contents, and those contents likely have some semantic meaning (as
in they're a customer name, an invoice number, an address, or something
like that). Exposing the entire Textbox reveals _how_ your form is
displaying / receiving information. All you want is the information.

Feb 2 '06 #4
Thanks Bruce,
It makes sense. I tried and fore some reason it does not return the value,
but an empty string.
Feb 2 '06 #5
My bad, I forgot SET.
Feb 3 '06 #6
Even with set it loses the value. I do not know what is wrong.
Here is the code:

In Child form:
public class Form2 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button button1;

private System.Windows.Forms.TextBox txtContent;

private string content;

......

public string Content

{

get {return content;}

set {content = value;}

}

}

private void button1_Click(object sender, System.EventArgs e)

{

textBox1.Text = txtContent.Text;

this.Content = textBox1.Text;

}

=========

In Main Form:

private void menuItem1_Click(object sender, System.EventArgs e)

{

Form2 ChildForm = new Form2();

ChildForm.MdiParent = this;

ChildForm.Show();

}

private void btnRetrieveValue_Click(object sender, System.EventArgs e)

{

txtDestination.Text = ChildForm.Content ;

}
Feb 3 '06 #7
Checkout the following FAQ - it contains an example of what you need. You will need to search for the example though, but I would bookmark this website.

http://www.syncfusion.com/faq/windowsforms/Default.aspx

Sonu Kapoor [MVP]
---
Posted via www.DotNetSlackers.com
Feb 3 '06 #8
Some fine tuning and it is working now.
Thanks to everybody.
Feb 3 '06 #9
Is there a particular reason why you need a "content" string field,
rather than just having the Content property read directly from the
textbox1.Text, like this:

public string Content
{
get { return textBox1.Text; }
}

Feb 3 '06 #10
No reason. I tried this way first and it did not work, then I tried
different ways to wok around.
Feb 3 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.