GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Jul 29, 2014 4:38 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Clearing a GtkEntry
PostPosted: Mon May 04, 2009 8:28 pm 
Offline
GTK+ Geek

Joined: Wed Apr 15, 2009 8:44 pm
Posts: 61
I have a GtkEntry (textbox) that needs to be cleared when a button is clicked, and I cannot seem to find an easy way to do it. couldn't find anything on google or in the search of this site.

anyone have any ideas?

i tried setting the text just to 2 or 3 blank spaces, but GTK then complains when you click on that box again.

any help would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 04, 2009 9:32 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
You should just be able to set the entry text to nothing using gtk_entry_set_text:
http://library.gnome.org/devel/gtk/stab ... y-set-text
Here's an example:
Code:
#include <gtk/gtk.h>

void button_clicked_cb(GtkWidget *button, GtkEntry *entry)
{
   /* Set the entry to "", which is the same as a 'gtk_entry_clear' */
   gtk_entry_set_text(entry, "");
}

gint main(gint argc, gchar **argv)
{
   GtkWidget *win;
   GtkWidget *vbox;
   GtkWidget *entry;
   GtkWidget *button;

   gtk_init(&argc, &argv);

   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect(win, "delete-event", G_CALLBACK(gtk_main_quit), NULL);

   vbox = gtk_vbox_new(TRUE, 2);
   gtk_container_add(GTK_CONTAINER(win), vbox);
   gtk_widget_show(vbox);

   entry = gtk_entry_new();
   gtk_box_pack_start(GTK_BOX(vbox), entry, TRUE, TRUE, 0);
   gtk_widget_show(entry);

   button = gtk_button_new_with_label("Clear entry");
   g_signal_connect(button, "clicked", G_CALLBACK(button_clicked_cb), (gpointer)entry);
   gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);
   gtk_widget_show(button);

   gtk_widget_show(win);

   gtk_main();

   return 0;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 04, 2009 10:04 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello.

You can also replace
Code:
gtk_entry_set_text( entry, "" )
call in dreblen's code with
Code:
gtk_editable_delete_text( GTK_EDITABLE( entry ), 0, -1 )
which is slightly more efficient.

Docs:http://library.gnome.org/devel/gtk/stable/GtkEditable.html#gtk-editable-delete-text


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group