简体   繁体   中英

How do I determine the page number for the tab I just clicked on in gtk#?

I have a GTK notebook with multiple tabs. Each tab label is a composite container containing, among other things, a button I want to use to close the tab. The button has a handler for the "clicked" signal.

When the signal is called, I get the button widget and "EventArgs" as a parameter.

I need to determine the page number based on the button widget, but myNotebook.PageNum(buttonWidget) always returns -1. I've even tried buttonWidget.Parent which is the HBox which contains the widget.

Any ideas on what I can do or what I am doing wrong?

One easy work around is to pass the page number to your button's Clicked event as you construct the buttons.

for (int page = 0; page < n; page++){ 
    int the_page = page;
    NotebookPage p = new NotebookPage ();
    ...
    Button b = new Button ("Close page {0}", the_page);
    b.Clicked += delegate { 
        Console.WriteLine ("Page={0}", the_page); 
    };
}

The "the_page" is important, as it is a new variable that will be captured by the delegate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM