Trenton Pottruff
Trenton Pottruff

Reputation: 43

Develop UI software without the forms designer?

I am interested in developing software like Text Editors and so forth.

The only way I currently know how to develop software in C# is to use Visual Studio's Forms designer: https://i.sstatic.net/WVxli.png

In Java it is possible (and I know how to) to do this.

Is it possible to develop software in C# like how it is done in Java (through 100% code).

Upvotes: 1

Views: 2990

Answers (1)

Raheel Khan
Raheel Khan

Reputation: 14787

Yes it is very possible. The forms designer is just a visual wrapper that generates code behind the scenes. You can use WPF that is a declarative approach to UI design. You can do the same with WinForms. Here is a simple form example written by hand. Besides practice though, I don't see why you would want to do this for non-trivial UI applications.

namespace MyTestApp
{
    public static class Program
    {
        [System.STAThread]
        private static void Main ()
        {
            System.Windows.Forms.Application.EnableVisualStyles();
            System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

            System.Windows.Forms.Application.Run(new MyForm());
        }

        public class MyForm: System.Windows.Forms.Form
        {
            private System.Windows.Forms.Button ButtonClose { get; set; }
            private System.Windows.Forms.RichTextBox RichTextBox { get; set; }

            public MyForm ()
            {
                this.ButtonClose = new System.Windows.Forms.Button();
                this.RichTextBox = new System.Windows.Forms.RichTextBox();

                this.ButtonClose.Text = "&Close";
                this.ButtonClose.Click += new System.EventHandler(ButtonClose_Click);

                this.Controls.Add(this.ButtonClose);
                this.Controls.Add(this.RichTextBox);

                this.Load += new System.EventHandler(MyForm_Load);
            }

            private void MyForm_Load (object sender, System.EventArgs e)
            {
                int spacer = 4;

                this.RichTextBox.Location = new System.Drawing.Point(spacer, spacer);
                this.RichTextBox.Size = new System.Drawing.Size(this.ClientSize.Width - this.RichTextBox.Left - spacer, this.ClientSize.Height - this.RichTextBox.Top - spacer - this.ButtonClose.Height - spacer);

                this.ButtonClose.Location = new System.Drawing.Point(this.ClientSize.Width - this.ButtonClose.Width - spacer, this.ClientSize.Height - this.ButtonClose.Height - spacer);
            }

            private void ButtonClose_Click (object sender, System.EventArgs e)
            {
                this.Close();
            }
        }
    }
}

Alternatively, when using the designer, take a look at the FormName.Designer.cs file which contains the same initialization code as above.

Upvotes: 2

Related Questions