473,320 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 16613
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: CHIN | last post by:
Hi all.. here s my problem ( maybe some of you saw me on other groups, but i cant find the solution !! ) I have to upload a file to an external site, so, i made a .vbs file , that logins to...
1
by: Eric | last post by:
Is it possible to create an unbound variable in a continous form and linked with another table variable. If yes then how to declare that variable and how to point another table variable. Thanks,
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.