Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Feb 3, 2015

Show the Structure of Your GUI in Your Code

I LIKE creating my GUIs programmatically. I suppose I'm a traditionalist (some would say a masochist). But I feel that it gives me more control over the design and keeps together things that should live together.

The downside to this is, lots of objects all declared in the same scope, with no structure to give you a hint as to how they all fit together:

    class View {
      Form frm = new Form();
      FlowLayoutPanel pnl = new FlowLayoutPanel();
      TabControl tbc = new TabControl();
      TabPage tbp1 = new TabPage();
      Label lbl = new Label();
      TextBox txt = new TextBox();
      TabPage tbp2 = new TabPage();
      Button btn = new Button();
      ...
    }
If you think about it though, the controls will all have fixed parent-child relationships with each other. They will effectively form a tree structure during runtime, that will give rise to the final graphical display. In fact, that's what all the XAMLs and JavaFXs and QMLs of the world model. Why not actually show these relationships in the code itself with a little bit of indentation trickery?

    class View {
      Form frm              = new Form();
        FlowLayoutPanel pnl = new FlowLayoutPanel();
          TabControl tbc    = new TabControl();
            TabPage tbp1    = new TabPage();
              Label lbl     = new Label();
              TextBox txt   = new TextBox();
            TabPage tbp2    = new TabPage();
          Button btn        = new Button();
      ...
    }

A-ha! So the Button won't be inside the TabPage; it'll be a sibling of the TabControl itself!

Of course, this is all manual, and won't work with whitespace-sensitive languages, and can get out of date when you change your layout, and all those things. But--when it does work, it's surprising how well it works at organising my thoughts about the layout into a nice 'graphical' overview.

Nov 22, 2006

Finally managed to learn C#

And it wasn't as hard as I thought it would be. I finally dove into the Ecma C# language specification document, for lack of any better reference/tutorial books here in Dhaka, and went through it absorbing everything that had confused me about the language before (like attributes, the override keyword and inheritance, assemblies and DLLs). A little bit about that spec. I downloaded it a long time ago meaning to go through it and learn the language, but kept putting it off. Now that I've done it, I see the spec is really an excellent reference for learning the language -- probably better than any O'Reilly `In a Nutshell' book for the language. If you're not new to programming, but are new to C#, just get it. It's worth it.

Wish there was an equivalent .NET library reference though. The MSDN library is very thorough but also bloated and slow. Kinda typical of Microsoft when you think about it. Heh, heh.

To celebrate, here's the first useful program I wrote (just now) in C#: ToggleScreenSaver. It's a command-line tool that turns your screen saver on or off. Works only on Windows. Usage is:

ToggleScreenSaver [on|off]

on: turns the screen saver on
off: turns it off

Source code:
// ToggleScreenSaver.cs
using System;
using Microsoft.Win32;

public class ToggleScreenSaver {
static void Main(string[] args) {
// args[0] is the first argument passed and so on, not the name of the program
// That's a little brain-dead, but OK, we can roll with it
RegistryKey rk = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
if (args[0] == "on") {
rk.SetValue("ScreenSaveActive", "1");
} else {
rk.SetValue("ScreenSaveActive", "0");
}
rk.Close();
}
}