0018 - Variations on a Text Entry Widget
The Simple Entry
Let’s move away from buttons for the moment, both mouse and GUI, and look at the
Entry widget… yeah, the one used for gathering a small bit of text from the user.
This is pretty much the easiest widget to use and the code amounts to this:
And to bring the entered text into your program is dead simple:
Assign it to a variable, stick it in a
writeln() function, whatever’s your poison.
Moving right along…
Sometimes, you may want the user to enter text, but once it’s entered, you don’t want it changed. In this example, I’ve set up a
CheckButton to control editability:
Rather than sub-classing the
CheckButton, I used a sub-class of
Box as a way to track the data and hand it around as needed. In effect, the
Box acts as a parent handing out the goodies.
Note that both the
CheckButton states are set to
false which means if you compile and run this example, you won’t be able to type into the
Entry until you check the
Now let’s think about what else text
Entry widgets are used for. One thing that comes to mind is collecting login information. In our third example, we’ll mimic a login with a
Username field left in the clear (readable, in other words) and an obscured
Password field. The
LoginBox class serves as the parent handing out candy, so we need an extra
Entry widget with
Visibility set to
false, which means our constructor now looks like this:
false for the
Entry and then in the callback function
passwordVisibility(), we make allowances for finding either
And just before the callback finishes up, we cobble together a message for the user about computer security. I could tell you what the messages say, but I think it’ll be more educational if you compile the code for the obfuscated Entry example and run it yourself.
Next time, we’ll continue with two more variations on the
Entry widget. Until then, happy D-coding and may the
Widgets find room in your top pocket.
If you'd like to leave a comment...
Until I get time to research and install a commenting system, I ask that you try one of these options:
- come on over to the D Language Forum and look for one of the gtkDcoding announcement posts,
- drop by the gtkD Forum,
- follow the link below to email me, or
- go to the gtkDcoding Facebook page.
You can also subscribe via RSS so you won't miss anything.
© Copyright 2019 Ron Tarrant