Tanjil Khan
Tanjil Khan

Reputation: 61

gtk# - Getting input text from entry, clicking a button and display that text in label

I am using gtk# at monodev and i'm trying to do a simple GUI application that gets weight input and converts it to kg or lbs. Firstly, i'm trying to understand the basics, i wanted to know how to get the entry input after clicking my button and displaying that input text in my label.

This is how my MainWindow.cs looks.

enter image description here

I created a simple signal button that closes the program if i click the Convert button.

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;
    }

    // Convert button
    protected void OnButton3Clicked(object sender, EventArgs e)
    {
        Application.Quit();
    }
}

I wanted to get the text from entry and displaying it after clicking convert button

Upvotes: 0

Views: 28

Answers (1)

Tanjil Khan
Tanjil Khan

Reputation: 61

Ok so i found out how to get entry text, the name of my entry was entry2 as you can see at properties

enter image description here

entry2.Text

My label has label1 as name of property and to display it was just by doing

label1.Text = entry2.Text

Although, to display numbers, i had to convert string of the entry to float

float i;
bool success = float.TryParse(entry2.Text, out i);
if (success)
    {
       float kg_to_lbs = (float)(i * 2.204);
       Console.WriteLine("To pounds: " + kg_to_lbs);
       label1.Text = kg_to_lbs.ToString();
    }

Upvotes: 0

Related Questions