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# / January 2008

Tip: Looking for answers? Try searching our database.

pass "out"  parameter to ShowDialog?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
viepia@nospam.com - 30 Jan 2008 17:51 GMT
Hi,
  What is the best way to set a "out" variable in a WinForm passed by
a ShowDialog caller?

   Currently I am setting a public static variable in the ShowDialog
callers' class in the WinForm - I am hoping there is a less ugly way.

Thanks,
Viepia
Harvey - 30 Jan 2008 18:02 GMT
You can easily access a public value once the form is closed that
contains the assignment you make in the form. So let's say you have a
main form:

   public partial class MainForm : Form
   {
       public MainForm()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           WinForm wf = new WinForm();
           if (wf.ShowDialog().Equals(DialogResult.OK))
               MessageBox.Show(wf.SomePublicString);
       }
   }

You can see the wf.SomePublicString is available to you... Here is the
WinForm:

   public partial class WinForm : Form
   {
       public string SomePublicString
       {
           get { return somePublicString; }
           set { somePublicString = value; }
       }
       private string somePublicString = string.Empty;

       public WinForm()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           SomePublicString = "Hello World!";
           DialogResult = DialogResult.OK;
       }
   }

Hope that helps...

On Jan 30, 10:51 am, vie...@nospam.com wrote:
> Hi,
>    What is the best way to set a "out" variable in a WinForm passed by
[quoted text clipped - 5 lines]
> Thanks,
> Viepia
Nicholas Paldino [.NET/C# MVP] - 30 Jan 2008 18:03 GMT
Viepia,

   Why not expose the value through a property on the instance?  Then,
after the call to ShowDialog is made, the caller can access the value
through the property on the instance that ShowDialog was called on.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi,
>   What is the best way to set a "out" variable in a WinForm passed by
[quoted text clipped - 5 lines]
> Thanks,
> Viepia
viepia@nospam.com - 31 Jan 2008 07:13 GMT
Thanks for  helping  my code look clearner.    For some dumb reason I
didn't think that the instance data of a closed Winform was valid.

Viepia

>Viepia,
>
>    Why not expose the value through a property on the instance?  Then,
>after the call to ShowDialog is made, the caller can access the value
>through the property on the instance that ShowDialog was called on.
Ignacio Machin ( .NET/ C# MVP ) - 30 Jan 2008 20:13 GMT
Hi,

A simple public property will do:

MyForm f = new MyForm();
if ( f.ShowDialog() == DialogResult ...)
{
  DoSomething( f.MyProperty);
}

Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

> Hi,
>   What is the best way to set a "out" variable in a WinForm passed by
[quoted text clipped - 5 lines]
> Thanks,
> Viepia

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.