How to create a window without using the GUI builder?

Asked

Viewed 1,334 times

5

I’m starting to study C# and just like in Java, Ides provide resources that make it possible to build Guis easily by dragging components. The has a powerful construction tool where it is possible to drag Swing components to a JFrame, JDialog, etc. In the I felt at home, it’s practically the same scheme.

My question is: In Netbeans, the code generated by GUI Builder is functional, but very difficult to maintain - assuming that anyone who provides maintenance will not use the Netbeans interface builder (e. g a user of ). Depending on the requirements in the graphical interface it is much better to write code "on the nail", making the source code much simpler to read. I don’t know how the window code is generated in C#, but I wonder if it is possible to build a graphical interface in "pure" code, manually.

For example, in Java the following snippet is enough 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();
    }
}

What it would be like to create the same window in C#?


PS: No, I’m not a hater of GUI builders, it is more for curiosity’s sake, I want to understand how it works.

1 answer

5


I agree that it is often better to write the code in hand than to use the generator. It gives more flexibility and control. I am visual Builder hater :P

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

But nothing stops you from writing it all in your hand. What I see happen a lot in experienced programmers who are starting out in C# is generating a class through the IDE to analyze and see how to proceed in future manual implementations.

A slightly more functional Hello World 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!");
    }
}

I put in the Github for future reference.

A most complete tutorial.

  • I found it interesting (and strange) how to add a Listener on the button: b.Click += new EventHandler(Button_Click); is almost a "concatenate" event.

  • 1

    @Renan and is still a concatenation. It concatenates to a list of subscribers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.