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 netbeans has a powerful construction tool where it is possible to drag Swing components to a JFrame
, JDialog
, etc. In the visual-studio 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 eclipse). 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.
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.– Renan Gomes
@Renan and is still a concatenation. It concatenates to a list of subscribers.
– Maniero