Hello guys, first of all.. am gonna say that i googled and searched the forum alot.. nothing to be found..

So my question is..

1- Where does my actual manual written code go in C# + GUI (WindowsFormsApplication not the console one.)

Does it go in the Program.cs or Form.cs? and where exactly? Do i make a main method ? very very confusing... please help.

Sorry if I was unclear about a couple of things.

Best Regards to all..

Recommended Answers

All 8 Replies

That is in under form.cs, no need to make any main method for this..
Because program.cs already contain a main method.
I hope it will clear to you.......

Kindly mark thread as solved, if your problem is solved now.......

Dont worry, I will as soon as its solved.. I just need more help

So right now i have two methods inside my form..

one: InitializeComponent() which the compiler is telling me not to modify manually cause it belongs to design or some what...

two: Dispose(bool ....) which from the looks of it I shouldn't change.

These two belongs to the class form1; obviously; and I cant just put my code outside directly in the class cause that wouldnt work; so any help?

This is the code that i have right now with some messed up and incomplete code on it..

using Devart.Data.MySql;

namespace WindowsFormsApplication1
    partial class Form1
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)
            if (disposing && (components != null))

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
            this.mySqlConnection1 = new Devart.Data.MySql.MySqlConnection();
            this.label1 = new System.Windows.Forms.Label();
            // mySqlConnection1
            this.mySqlConnection1.ConnectionString = "User Id=root;Password=test;Host=;Database=test;";
            this.mySqlConnection1.Name = "mySqlConnection1";
            this.mySqlConnection1.Owner = this;
            // label1
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(23, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(68, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Connected...";
            // Form1
            this.ClientSize = new System.Drawing.Size(448, 391);
            this.Name = "Form1";

            //my code..



        private MySqlConnection mySqlConnection1;
        private System.Windows.Forms.Label label1;



I do need more help, am not new to programming, but this is my first day on my C#; been programming in Java for 3 years now. So don't worry about terminology.

Thanks in advance again, your help is much appreciated.

So u don't have any designer.cs file in your application, right?


Form1.cs has the code you write : form's class
Form1.Designer.cs - the code created by Visual Studio for your form and controls(here is written any change you make to your control's properties)

So, in your example, that is the code that VS wrote. It is used in form's constructor. To write your own code double-click on your form (you will be sent in OnLoad method of the form) or from SolutionExplorer, double click on Form1.cs.
in Form1.cs you will initially have :

public partial class Form1: Form
     public Form1() - //constructor
           InitializeComponents(); //here the code you posted is called and the form is created


About the Dispose method, it is the destructor and you have to call form1.close() to call indirectly that method.

Well more than likely your program is going to be doing some event driven stuff (reacting from a button getting clicked, text changed in a textbox).
Just to test out the concept put a button from the Toolbox onto your form (or whatever your gui need is),go over to Solution Explorer and there should be a tab saying Properties on the bottom of that window (else go to View/Properties Window). With the Form1.cs [Design] tab active and the button highlighted click on the lightning bolt in the toolbar. Go to the blank cell to the right of where it says click and double click it. You'll get an event handler button1_click (object sender, EventArgs e) placed into your code for you. Put whatever code you want associated with the click of the button in there(and you can call other methods you've written in class Form1 or in other classes and even trigger other events). So for example with the main window you can use a load event to execute code on startup. You could call other methods at the end of InitializeComponent() but I think it's much more practical to use the events to take advantage of the paradigm.
Just my half nickel but I figured it might help you to get your hands dirty since this ain't your first rodeo.

commented: Nice explanation +6
commented: directly tackled the problem and explained everything. Loved the post. +0

jonsca says it well. A shortcut to this is when in the design window and you put a button on your form, just doubleclick it and you are right in the button click event handler to type in code of what this button should do when clicked. Also notice that the designer.cs file is updated behind your back. The Form1.cs and Form1.designer.cs file are in fact one class wich is divided in two "partial" classes. The Form1.cs part is the one you work with. All this is done to make your life easier, but it can be confusing in the beginning, I had the same confusing when I started C#.

Thank you everyone for your help, I appreciate each and every reply i got from every one of you.

Jonsca and ddanbe; guys you tackled the right problem that i was having, I was just confused with this, because I was programming three years in java without any event driven programming. This is different cause its GUI based and i completely forgot about the events; I was treating the whole matter as if i was working on a console-based application.

Thank you guys for all your help, marking this as solved.
Thumbs up to all of you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.