How to create a window without using the GUI builder?

5

I'm starting to study C # and just like in Java, IDEs provide features that make it possible to build GUIs easily by dragging components. The has a powerful build tool where you can drag Swing components for JFrame , JDialog , etc. In the I felt at home, it is pretty much the same scheme.

My question is: In Netbeans, the code generated by the GUI Builder is functional, but very complicated to maintain - assuming that maintenance is not going to use the Netbeans interface builder (eg a ). Depending on the requirements in the graphical interface it is much better to write the code "on the nail", making the source code much simpler to read. I do not know how the window code is generated in C #, but I would like to know if it is possible to build a graphical interface in the "pure" code manually.

For example, in Java, the following excerpt is used to show a window:

import java.awt.event.*;
import javax.swing.*;

public class MinhaJanela extends JFrame {

    public MinhaJanela(){
        super("Minha Janela");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 300);
        setLocationRelativeTo(null);

        JButton btn = new JButton("Click aqui");
        btn.addActionListener((ActionEvent e) -> {
            btnClick();
        });
        getContentPane().add(btn);
        setVisible(true);
    }

    private void btnClick(){
         JOptionPane.showMessageDialog(null, "Clicou!");
    }

    public static void main(String[] args) {
        new MinhaJanela();
    }
}

How would you create the same window in C# ?

PS: No, I'm not a hater of GUI builders, it's more of a curiosity even though I want to understand how it works.     

asked by anonymous 21.12.2014 / 02:29

1 answer

5

I agree that it is often best to write the code on hand rather than using the generator. Gives you more flexibility and control. I am visual builder hater : P

Visual Studio generates easier-to-maintain codes by separating even what you should handle and what only the generator should handle (you can even manipulate this part at the risk of creating generator difficulties). The existence of methods and partial classes help a lot in this.

But nothing prevents you from writing everything down. What I see happen a lot in experienced programmers who are starting in C # is to generate a class through the IDE to analyze and see how to proceed in future manual implementations.

A Hello World a bit more functional than your example:

using System;
using System.Windows.Forms;

public class HelloWorld : Form {
    static public void Main() {
        Application.Run(new HelloWorld());
    }

    public HelloWorld() {
        this.Text = "Minha Janela";
        Button b = new Button();
        b.Text = "Click aqui";
        b.Click += new EventHandler(Button_Click);
        Controls.Add(b);
    }

    private void Button_Click(object sender, EventArgs e) {
        MessageBox.Show("Clicou!");
    }
}

A more complete tutorial .

    
21.12.2014 / 02:59