Hello friends,
Please tell me the solution to give masking to GtkEntry.
I mean to say, How can I allow only letters or only alphabets in a GtkEntry widget.
Thanks in advance.
Hello friends,
Please tell me the solution to give masking to GtkEntry.
I mean to say, How can I allow only letters or only alphabets in a GtkEntry widget.
Thanks in advance.
How about using Google?
It's amazingly useful, within seconds I found this!
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
Ok,
Thanks I understood. But still there's a problem, I want floating numbers:
I replaced the code in the above link with isdigit, worked awesome, but when i am using isfloat its not working.
isfloat function is not there ???
I am unable to give float masking in this code:
Please help.Code:void on_txtSamekitKar_insert_text(GtkEditable *editable, gchar *text, gint length, gpointer position, gpointer data) { int i, count = 0; gchar *result = g_new(gchar, length); for (i = 0; i <= length; i++) { if (!isdigit(text[i])) continue; result[count++] = text[i]; } if (count > 0) { g_signal_handlers_block_by_func (G_OBJECT(editable), G_CALLBACK(on_txtSamekitKar_insert_text), data); gtk_editable_insert_text(editable, result, count, position); g_signal_handlers_unblock_by_func (G_OBJECT(editable), G_CALLBACK(on_txtSamekitKar_insert_text), data); } g_signal_stop_emission_by_name(G_OBJECT (editable), "insert_text"); g_free (result); }
We're heading down the slippery slope where you don't engage your brain and expect me to breadcrumb you to the answer...
There is no isfloat function that works on a char, because a char can't hold a floating point number (you should know this!). Instead you have two options:
1) Check each character whether it's either a digit or the only other character allowed in a floating point number (work this out yourself)
2) Try to convert the entire string to a float (by using a standard library function) and see whether you get a successful result or not
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
Thanks QuantumPete,
I got it to work with this code, I don't know whether it is right logic or not but it is working:
Code:void on_txtSamekitKar_insert_text(GtkEditable *editable, gchar *text, gint length, gpointer position, gpointer data) { int i, count = 0; gchar *result = g_new(gchar, length); for (i = 0; i <= length; i++) { if (!isdigit(text[i]) && text[i]!='.') continue; result[count++] = text[i]; } if (count > 0) { g_signal_handlers_block_by_func (G_OBJECT(editable), G_CALLBACK(on_txtSamekitKar_insert_text), data); gtk_editable_insert_text(editable, result, count, position); g_signal_handlers_unblock_by_func (G_OBJECT(editable), G_CALLBACK(on_txtSamekitKar_insert_text), data); } g_signal_stop_emission_by_name(G_OBJECT (editable), "insert_text"); g_free (result); }
The above code is capable of printing double (.)s but in a float there must be only single (.), how can I implement this. Please help.
Seriously... spoon-feeding...
How would you go about ensuring that an action is carried out only once? Perhaps some state variable that can be checked via an if condition? When you encounter a "." set the state variable and then discard the "." from then on if it is encountered again?
Or, you could try to convert the entire contents in one go via an in-built function, rather than attempting to rewrite it yourself from scratch.
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
Thanks quantumpete for the reply,
That's what I want to say that I am really a newbie to c, will you please tell me that built-in function.
Make a spoon feed once more... please !
I'm going to do the whole Let Me Google That For You thing once more... and you'll find from the results that there are even two methods for doing it.
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
Ok,
Finally I got this to work but still some difficulties:
Code:gboolean on_txt_demandno_focus_out_event (GtkWidget *widget, GdkEvent *event, gpointer data) { gdouble output; gchar *end; const gchar *text; text = gtk_entry_get_text (GTK_ENTRY (Ledger_Fields.txt_demandno)); output = strtod (text, &end); (void) output; if ((end == text) && (strcmp (text, "")!=0)) { display_message (Application.window, "Invalid entry found.", GTK_MESSAGE_ERROR); gtk_widget_grab_focus (Ledger_Fields.txt_demandno); return false; } return true; }
- When I am entering any decimal or float value in txt_demandno field then its ok, the control goes to next field.
- When I am entering 2s or 345s or something the control goes to the next field, why so?
- But, when I am entering s2 or s345 the control is sticking on the same field.
Here, point 1 and 3 are correct but why point no. 2 is not going correctly. Please help. Second thing if I am giving this code, the program is exiting showing error: SEGMENTATION FAULT [CORE DUMPED].
Code:gboolean on_txt_demandno_focus_out_event (GtkWidget *widget, GdkEvent *event, gpointer data) { gdouble output; gchar *end; const gchar *text; text = gtk_entry_get_text (GTK_ENTRY (Ledger_Fields.txt_demandno)); output = strtod (text, &end); (void) output; if (isalpha (text)) { display_message (Application.window, "Invalid entry found.", GTK_MESSAGE_ERROR); gtk_widget_grab_focus (Ledger_Fields.txt_demandno); return false; } else { if ((end == text) && (strcmp (text, "")!=0)) { display_message (Application.window, "Invalid entry found.", GTK_MESSAGE_ERROR); gtk_widget_grab_focus (Ledger_Fields.txt_demandno); return false; } } return true; }
Last edited by Ravi Raj; 05-31-2012 at 12:16 PM.
At last, I don't know the logic is correct or not but this code is working fine:
Code:gchar* convert_value (const gchar *value, gchar type) { gchar *str; gchar buff[strlen(value)]; gchar *fstr = malloc (sizeof buff); gint dvalue; gdouble fvalue; glong lvalue; switch (type) { case 'd': dvalue = strtod (value, &str); sprintf (buff, "%d", dvalue); break; case 'f': fvalue = strtof (value, &str); sprintf (buff, "%.2f", fvalue); break; case 'l': lvalue = strtol (value, &str, 0); sprintf (buff, "%ld", lvalue); break; default: strcpy (buff, value); } strcpy (fstr, buff); return fstr; }