Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Reading values from another form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve - 18 Mar 2008 12:59 GMT
When you pull up a second form in your program,
how do you get the values from the form?

           Form2 dataForm = new Form2();

           dataForm.ShowDialog();
           if (dataForm.DialogResult == DialogResult.OK)
           {
               // I want to get the textBox1 data from the form
           }

Thanks.
Marc Gravell - 18 Mar 2008 13:42 GMT
In this case the best approach would be to expose what you want as public
properties on Form2 - i.e.

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

then you can access dataForm.CustomerName

For info, ShowDialog() [unlike Show()] doesn't Dispose() the form, so it
would be best to be "using" this - i.e.
using(Form2 dataForm = new Form2()) {
 // do everything you want
}
// it is now disposed
Marc
Steve - 18 Mar 2008 14:48 GMT
Thanks Marc.

> In this case the best approach would be to expose what you want as public
> properties on Form2 - i.e.
Cartoper - 18 Mar 2008 14:51 GMT
Marc's solution most definitly works, I like to be a bit more complex
and make a private variable to hold the value and then a public
property to read the private variable.  This way, when the user click
on OK on the second form, the second form can validate and make sure
everything is correct before loading the value into the variable and
exiting.  If the user clicks on cancel, the value is never populated
and the first form, even if it tries to get the value, cannot.
Cor Ligthert[MVP] - 18 Mar 2008 18:33 GMT
Steve,

I use in those cases internal properties.

Cor

> When you pull up a second form in your program,
> how do you get the values from the form?
[quoted text clipped - 8 lines]
>
> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.