0050 – MVC Part III – ComboBoxText - Adding and Removing Items
Last time we got stuck into
ComboBoxText widgets and this time we carry on by adding and deleting items that show up in the drop-down list.
In this first example, we’ll look at…
Adding an Item to a ComboBoxText
For this functionality, we need to put an extra
Button in our UI and, of course, that’s done in the
I’ll draw your attention to these lines:
The only unusual thing is passing in the
dayComboBoxText object so the Add
Button has access to its data. No worries, right? We’ve done this kind of thing a few times by now.
writeln() to echo a couple of lines to the terminal so you know what’s being demonstrated.
Now let’s look at…
The Add Button Class
Which looks like this:
Because we need to grab the text from the
Entry, as mentioned before, we need access and we get that in the constructor by assigning
_entry. The first assignment (
_comboBoxText) isn’t strictly necessary, but it lends clarity to the
cast() call. And maybe we’ll think of some other reason we need this sometime down the road.
doSomething() function, we take up the task of grabbing the text from the
appendText() adds it to the list.
A quick note: There are three ways we can add items to the list:
appendText()sticks it at the end of the list,
prependText()sticks it at the beginning, and
insertText()takes a second argument so you can put it wherever you want.
Remove an Item
This example looks pretty much the same as the last except it’s got a Remove
Button that looks like this:
Removing, as can be seen in the
if() statement inside the
doSomthing() function, is a two-stage process:
- check to make sure the item is actually on the list, and
remove()to remove it.
There is a third line of code that’s just as important, though:
Why? Because if we don’t reset the active item, the just-deleted text is still sitting in the
Entry. Depending on circumstances, you may want to comment this line out so the text is left there to serve as a quick-n-dirty ‘undo’ function. All the user would have to do is hit the Add
Button and the item goes right back on the list.
Next time, we’ll take a look at the
ComboBox and look at populating it with various data types starting with text, then moving on to things like numbers, images, or whatever else fits into a
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