Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting values in GtkEntry

Tags:

c

gtk

gtk2

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();
like image 298
محمد احسان الحق Avatar asked May 25 '26 09:05

محمد احسان الحق


1 Answers

  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.

like image 92
unwind Avatar answered May 28 '26 02:05

unwind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!