user3712526
user3712526

Reputation: 11

Windows Form Application won't initialize

I'm trying to write a windows application to do some file reading, manipulation and writing in C# and I'm mostly done except that when I run my code it wont open up my app but instead brings me to a blank terminal screen.

public partial class Form1 : Form
{
    public string fPath;
    public string nfName;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        fPath = textBox1.Text;
        nfName = textBox2.Text;
    }

    public string getPath()
    {
        return fPath;
    }

    public string getFileName()
    {
        return nfName;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            this.textBox1.Text = folderBrowserDialog1.SelectedPath;
        }
    }
}

This is the code for for the app itself. ^^^

static void Main()
{
    Form1 f = new Form1();
    string path = f.getPath();
    string nFileName = f.getFileName();

}

This is the code for the beginning of my main. ^^^

I have these two sections in different classes and in the same namespace. I want for this main to be the entry point and then for it to call my form and initialize it so that I can input my file path and process it but despite creating an instance of the Form1 class, it will not initialize. I have tried f.Form1() but it will not recognize that as a method of my object for whatever reason.

Upvotes: 1

Views: 2481

Answers (3)

John Koerner
John Koerner

Reputation: 38087

As others have stated, you need to call Application.Run(f), but if you are seeing a command window, you may need to change your output type in your project settings:

enter image description here

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 157098

In your main method you should start the UI message loop. Application.Run does that for you. Also, it will show the form for you and keeps the application running meanwhile.

Form1 f = new Form1();
...
Application.Run(f);

Upvotes: 4

TyCobb
TyCobb

Reputation: 9089

You need to call Application.Run

Here's what the standard Main() looks like for WinForms

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

Upvotes: 2

Related Questions