tags:

views:

168

answers:

2

I want my C# program to collect data. Then, when the OK button is clicked, I want this data to be loaded into my program -- such as into variables in Main(), or into data members of a class I have constructed, and I want the form to then go away -- not be hidden, but actually closed. Alas, read as I might in the book I have, the data collected by the form stays in the form, execution proceeds within the form, and the same form is used to display the result of the program's computations. Sheesh. All I want is to capture the form's information, close the form and proceed with my program. I would appreciate some tips on geting data from a form into Main() or into a class's data members.

Thanks, Lucky

A: 

You're writing in WinForms? As far as I know a Windows application has to have a window, even if it's one pixel by one pixel.

Have you seen any other Windows applications that work the way that you want yours to work? Opens a window, the window closes, but the program keeps on running? This is generally considered undesired behavior, similar to viruses and trojans.

You can create a console application or a Windows service with no GUI, of course.

What is the application doing behind the scenes after the data is entered? If it's just doing some calculations and saving to disk, uploading, or printing, leave the window open for that and then exit when it's done. Possibly include a progress bar.

TrueWill
Thank you for your reply. Say I have created a class with data members first name, last name, street address, city, and state. I want my form to collect this information and my class's data members to receive the information for other uses. I am using regular C# to generate a Windows form to collect the information.
Jimmy
@Jimmy: You can use data binding (see http://support.microsoft.com/kb/313482 ) or just set the properties in a button OnClick event. The instance variable could be a field of the form class or could be created on the fly in the OnClick event. This is a complex question; I'd recommend more study to get familiar with event-driven programming.
TrueWill
I am very grateful to JohnSaunders, Oded, Charles M, and TrueWill for taking to time to answer my dumb novice question. I am an old VBA programmer struggling to learn C#. I
Jimmy
I seem to face a choice of either letting the first form I show keep running the show until the program ends or intensely studing data binding. This is a daunting but important choice for me. I'd appreciate any further comments in this regard. I have attempted to create an OpenID account so I can rate replies, but I'm having trouble with that. I apologize. I'll keep trying.
Jimmy
@Jimmy: Data binding isn't going to get you out of this dilemma. You always need **something** running the show. In WinForms that's generally a form.
TrueWill
Thanks again, TrueWill. Your comments are of critical importance to me, and I hang onto every golden word. One more dumb question. When you say WinForms, are you referring to simply using C# to control Windows Forms, or are you referring to a special methodology that can be accessed using C#, or are you referring to an entirely different software? Again, I am just jumping into C and I'm still quite ignorant. I will appreciate any further comments on this. Jimmy
Jimmy
@Jimmy: In Visual Studio, choose File/New/Project. Windows Forms Application (WinForms for short) is one of the options. Other GUI alternatives are Windows Presentation Foundation (WPF) and Silverlight. You can also create Console Applications (command line), web sites, services, etc.
TrueWill
+3  A: 

What you want to do is perfectly acceptable, it just isn't typical.

When you use Visual Studio to generate a WinForms project, it creates one form for you and generates a call to Application.Run(new Form1()). For this version of the Run() method, your application will exit when the "main form" (the one passed to Run(), in this case Form1) closes.

There are three overloads (versions) of Application.Run(). For your purposes, you need to use a different overload:

Application.Run(ApplicationContext)

When you use this overload of Run(), you get to control when the application exits. In a nutshell, here's one way you could do it:

  • Create a class which inherits ApplicationContext.
  • In its constructor:
    • Create your form.
    • Subscribe to its Closing and Closed events.
    • Show your form.
  • In your FormClosing event handler, get the data from the form.
  • In your FormClosed event handler, do whatever you want to do with the data, and then exit the thread (or do something else).

Here's a crude example, but I will leave out the code for the form itself. Assume the form simply has one TextBox which has its Modifiers property set to Public. (This is NOT an elegant way to get data from a form, but that part is up to you).

namespace Me.MyDemo
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            MyApplicationContext ac = new MyApplicationContext();
            Application.Run(ac);
        }

        class MyApplicationContext : ApplicationContext
        {
            string _text = "";

            public MyApplicationContext()
            {
                Form1 f1 = new Form1();
                f1.FormClosing += new FormClosingEventHandler(f1_FormClosing);
                f1.FormClosed += new FormClosedEventHandler(f1_FormClosed);
                Console.WriteLine("I am here. Showing form in 1 second...");
                Thread.Sleep(1000);
                f1.Show();
            }

            void f1_FormClosing(object sender, FormClosingEventArgs e)
            {
                _text = (sender as Form1).textBox1.Text;
            }

            void f1_FormClosed(object sender, FormClosedEventArgs e)
            {
                Console.WriteLine("You wrote: " + _text);
                Console.WriteLine("I will go away in 2 seconds...");
                Thread.Sleep(2000);
                ExitThread();
            }
        }
    }
}

Of course, you don't have to exit the thread. You can leave it running if there are other things for your program to do. It will just run as a windowless process. Just remember that you're responsible for eventually ending it.

For more help, look at the documentation for the System.Windows.Forms.Application class, and the ApplicationContext class.

For getting the data from your form, there are many ways to approach this. The simple way is to just give your form some public properties. A more sophisticated way would be to create a data class and use data-bound controls on your form.

Charles