Home Ask Login Register

Developers Planet

Your answer is one click away!

Vili Sinervä February 2016

How to create labels once a GUI button is pressed?

I have a GUI button that can detect when it is pressed and a piece of code that I want to have create labels when the button is pressed. I'm currently converting an entry to an int (This is working. Tested by displaying the int in an entry.) and trying to use a for loop. My code is in the source code of my main window. Here is my code:

using System;
using Gtk;

public partial class MainWindow: Gtk.Window
public MainWindow () : base (Gtk.WindowType.Toplevel)
    Build ();

protected void OnDeleteEvent (object sender, DeleteEventArgs a)
    Application.Quit ();
    a.RetVal = true;

protected void generatePlates (object sender, EventArgs e)
    int n;
    short number;
    bool validNumnber;

    if (Int16.TryParse (entry1.Text, out number))
        validNumnber = true;
        validNumnber = false;

    if (validNumnber == true)
        n = Int16.Parse(entry1.Text);
        n = 0;

    for (int i = 0; i < n; i++)
        var lbl = new Label();
        lbl.Name = "lbl"+i;
        lbl.Text = "Plate "+i+":";
        lbl.Allocation = new Gdk.Rectangle (110*i+110,110*i+110,100,100);
        this.Add (lbl);

        entry3.Text = this.ToString();

Any advice is appreciated and feel free to ask for more details. If my code has other flaws in it, please let me know as I am a complete novice. Also entry3 is just an entry I'm using for testing this.

Edit: This code is for a windows desktop app.


DiscipleMichael February 2016

In general, you need to add the new control to the Form/view/whatever's collection of controls. If you are running this in a view than simply use. View.Add (lbl);

If you are using c#.net winforms (which I don't think you are) then use this.Controls.Add(lbl);

Post Status

Asked in February 2016
Viewed 3,396 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life