djgharphalia07
djgharphalia07

Reputation: 227

Update or Change the button label in C

For my GUI having some buttons. If I were to change or update the label of any random button I select from the list what should I do?

The initial name of the button is written in button properties. My GUI is designed in Glade.

And now I will enter the new name in entry-text in my GUI.

I have created an update button for this. How to do it in Gtk ofcourse.

The related codes are as follows:

Creation of button in the window and find it.

 UpdateButton = GTK_WIDGET( gtk_builder_get_object( builder, "UpdateButton" ) );
 gtk_signal_connect (GTK_OBJECT (UpdateButton), "clicked", GTK_SIGNAL_FUNC (Update_Data), NULL);

On update button clicked.

 void Update_Data( GtkWidget *widget, gpointer data)
    {
                    const gchar *entry_text1;
                    const gchar *entry_text2;
                    const gchar *entry_text3;

    g_print ("You have clicked Update... - %s was pressed\n", (char *) data);

             entry_text1 = gtk_entry_get_text (GTK_ENTRY (entry1));
             entry_text2 = gtk_entry_get_text (GTK_ENTRY (entry2));
             entry_text3 = gtk_entry_get_text (GTK_ENTRY (entry3));

    char sql[300];
    sprintf(sql, "UPDATE DEVICES set NAME='%s ',\nUSERNAME='%s ',\nPASSWORD='%s '\nwhere ID=%s;"
               , entry_text1, entry_text2, entry_text3, updateid); 
    //updateid is the ID taken from the array when a button is clicked

       inserDatabase("myDatabase.db", sql);  
       getlastEntry(); //for taking the last entered info
       updateData(sql); //for updating in database
    }

If more information is required I will get you. Please do ask!

Upvotes: 0

Views: 1960

Answers (1)

Coffee'd Up Hacker
Coffee'd Up Hacker

Reputation: 1446

Your question is unclear, but if I understand you correctly...

You get the buttons...

GtkButton *click_button; // Button to click
GtkButton *change_button; // Button that changes label

click_button = GTK_BUTTON (gtk_builder_get_object (builder, "click_button"));
change_button = GTK_BUTTON (gtk_builder_get_object (builder, "change_button"));

Define a function for the click event to set the label...

static void
change_button_label (GtkWidget *click_button,
                     gpointer   user_data)
{
    GtkButton *change_button = (GtkButton *) user_data;
    gtk_button_set_label (change_button, "New Label");
}

Connect the click signal function to the button, and pass it the change button...

g_signal_connect (click_button, "clicked", G_CALLBACK (change_button_label), change_button);

Upvotes: 3

Related Questions