In the entry control, how do I set that only numbers can be entered?

Source: Internet
Author: User
Tags gtk

I haven't written any articles for a long time, because I started to take charge of a module. I am so busy that I am not a module, but I am just taking charge of a module. I have no experience in many things, the scalability and robustness of the Code, as well as some script problems and system interfaces, have puzzled me a lot of time and I have crossed the river by feeling the stone. However, after a while, I was satisfied with the results. Today, when I was modifying a bug, some testers mentioned that I had to enter only numbers in the entry and asked the old programmers. This is also a programming experience and skill, take a note here.
In GTK, entry or editable does not directly provide interfaces that only allow numbers to be input, but it hides a signal. By registering this signal, it is okay to filter out characters that are not numbers.

  1. # Include <GTK/GTK. h>
  2. Static void on_entry_insert_text (gtkwidget * entry, gcahr * new_text, int new_text_length, int * position)
  3. {
  4. If (new_text [0]> '9' | new_text [0] <'0 ')
  5. {
  6. New_text [0] = '/0 ';
  7. }
  8. Return;
  9. }
  10. Int main (INT argc, char ** argv)
  11. {
  12. G_type_init ();
  13. Gtk_init (& argc, & argv );
  14. Gtkwidget * window = NULL;
  15. Gtkwidget * vbox = NULL;
  16. Gtkwidget * entry = NULL;
  17. Window = gtk_window_new (gtk_window_toplevel );
  18. Gtk_window_set_title (gtk_window (window), "entry_test ");

  19. Vbox = gtk_vbox_new (false, 0 );
  20. Gtk_container_add (gtk_container (window), vbox );
  21. Entry = gtk_entry_new ();
  22. Gtk_vbox_pack_start (gtk_box (vbox), entry, false, false, 10 );
  23. G_signal_connect (g_object (entry), "insert-text", g_callback (on_entry_insert_text), null );
  24. Gtk_widget_show_all (winodw );
  25. Gtk_main ();
  26. Return 0;
  27. }

Filter strings by using an "insert-text" signal, so that you can only enter numbers.

~~ End ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.