9

I would like to know if there is a way to create GUI program, with main() function (just like in console app), so I'm creating all the objects in main() and I can access/change it from the other functions connected with buttons/textboxes etc. Is it even possible? ;p

Please understand that I'm very beginner with GUI's, things I'm talking about may be funny but still, i want to learn! Thanks :)

Patryk
  • 3,042
  • 11
  • 41
  • 83
  • 8
    **Every** C# program begins with a `Main()` method, even if it uses a GUI. They just don't tell you this. (Not directly anyway.) – BoltClock Jan 24 '12 at 19:30
  • Well, but its not like I have to acces function called "Main("), I'd like to find there a place to work like in "main" with console apps, is that possible? :p – Patryk Jan 24 '12 at 19:32
  • You *do* have access to the main. look at my answer below. – OnResolve Jan 24 '12 at 19:33
  • 1
    Read a book or tutorial on Winforms programming. You are about to get yourself into a lot of trouble. – Hans Passant Jan 24 '12 at 19:42

3 Answers3

21

When you create windows form project ( A Gui one), it has a main loop--In fact it requires one. By default, it's in program.cs and it kicks off your form:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

What you probably want though is the Form constructor. This is in the code behind of the Form (by default Form1.cs) and will look like this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();     
    }          
}
dckuehn
  • 2,427
  • 3
  • 27
  • 37
OnResolve
  • 4,016
  • 3
  • 28
  • 50
  • and how to create string in main so i can read it with text box? :P – Patryk Jan 24 '12 at 19:34
  • This logic *might* go in the Form1() [constructor for the form]. I use might loosely since it belongs in a function outside of the constructor but for demonstration, you can do String myString = txtMyTextBox.Text provided you have a text box on the form and the id is 'txtMyTextBox'. If you have trouble getting there, you need to back up and start from a more beginning stage. – OnResolve Jan 24 '12 at 19:37
  • well, there's a textBox1, there's a String myString = textBox1.Text; (in main) and still, in main i do have this: 'The name 'textBox1' does not exist in the current context' – Patryk Jan 24 '12 at 19:44
  • 1
    again, you don't want Main, you want Form1(). Main doesn't know about textBox1, whereas the form does. – OnResolve Jan 24 '12 at 19:46
  • oh thanks, I almost got it now :P Just dunno after creating string, how to make it change with a button (onclick), thats what i have now but it seem not to have access in there :P private void button1_Click(object sender, EventArgs e) { myString = "xxxxxx"; } – Patryk Jan 24 '12 at 19:50
  • thats what I can use in Form1(), is there a way to make myString = "anything"; from the button function, or from any other item from toolbox? – Patryk Jan 24 '12 at 21:03
  • +1 for including the **WHOLE** `main` (and constructor). That's what I've been looking for! – Bitterblue Feb 21 '14 at 13:35
2

A WinForm application starts from main

static void Main()
{
    Application.Run(new Form1());
}

Whatever you want to do in main you can do it here

dckuehn
  • 2,427
  • 3
  • 27
  • 37
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
0

Main method is in Program class which is located in Program.cs file

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

For what u want i would use constructor of Form1 class:

        public Form1()
        {
            InitializeComponent();
            //your code
        }

or ideally Form load event:

        private void Form1_Load(object sender, EventArgs e)
        {
            //your code
        }
flis00
  • 37
  • 6