3

How do I limit the gtkEntry only to numbers and also how to store the value entered by the user for further calculation.

entry1 = gtk_entry_new();
ptomato
  • 56,175
  • 13
  • 112
  • 165

1 Answers1

3
  1. You can attach a function to handle the key-press-event, and in that function you can filter the keys. This way you can block any keypresses that you don't want to affect the content of the GtkEntry.
  2. You can use gtk_entry_get_text() to get the text, then of course for an integer you need to convert using e.g. strtol() or some other regular string-to-integer function:

    const char *text = gtk_entry_get_text(entry1); const long value = strtol(text, NULL, 10); printf("the value is %ld\n", value);

    The above isn't 100% rock-solid, you can use the middle argument to strtol() to make it better but I omitted it for brevity and topicality.

unwind
  • 391,730
  • 64
  • 469
  • 606