Sample Code 112: How to allow only numbers in GtkEntry - Part 1?
Written by kksou   
Saturday, 09 December 2006
Problem

Suppose you would like to ensure that only numbers, i.e. 0 to 9, are allowed to be entered in a GtkEntry field as shown below:

How to allow only numbers in GtkEntry - Part 1?


Solution

There are many ways to accomplish this. In this example, we will make use of a new function emit_stop_by_name() recently added in by Andrei.

  • Use insert-text to let us know everytime the user enters a text.
  • If it's a number, let the default handler displays the number.
  • If it's not a number, use emit_stop_by_name() to prevent the text from appearing.
Important Note

To be able to use this function, you need to download the latest copy of php-gtk2 from the cvs repository (gtk.php.net/download.php) and do a recompilation. For linux users, this should take less than 10 minutes to recompile. For windows users using Gnope Installer, we have to wait for the php-gtk2 core team to give us an update of the php-gtk2.dll.

By the way, there are some people telling me that they have problem logging on to the cvs server from countries outside US. If you encounter the same problem, you may drop me a note via the feedback, and I can email you a copy of the php-gtk2 source code on the cvs server.


Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Allow only numbers in GtkEntry - Part 1");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);

$vbox->pack_start(new GtkLabel("uses emit_stop_by_name()"), 0, 0);

$item_number = new GtkEntry();
$item_number->connect('insert-text', 'on_insert'); // note 1

$hbox = new GtkHBox();
$hbox->pack_start(new GtkLabel('Item Number: '), 0, 0);
$hbox->pack_start($item_number, 0, 0);
$vbox->pack_start($hbox);
$vbox->pack_start(new GtkLabel("Note: item numbers should contain only numbers 0 to 9"), 0, 0);

$window->show_all();
Gtk::main();
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation
  1. Notify us when user enters any text.
  2. Use emit_stop_by_name() to gobble up any text that are not numbers.

Related Links

User reviews   Average user ratings:    3.0   (from 1 user)
  1. Josh from Switzerland
    July 08, 2008 7:23am

    Interesting function, unfortunately it has not perfect for example to a birthday boxEntry in some form at my sense :( We will must totally block all keyboard entry except numbers...

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved