3

On my GUI (Graphical User Interface), I have a button named Enter and a label.
When I click Enter, I want my result to be shown in the label. How do I do that?

Sean Reilly
  • 21,526
  • 4
  • 48
  • 62
Abid
  • 57
  • 1
  • 1
  • 2

5 Answers5

4

For windows forms use the .Text property on the label:

 private void btnEnter_Click(object sender, EventArgs e)
    {
        int themeaningoflifeuniverseandeverything = 420 / 10;
        lblResult.Text = themeaningoflifeuniverseandeverything.ToString();
    }

See exampe: ButtonEvent.zip

Anders
  • 647
  • 2
  • 8
  • 16
  • 1
    Sooo heavy variable name!! :D Also pleased to know that `themeaningoflifeuniverseandeverything ` is of type `int` ! cheers. – Pratik Deoghare Jan 25 '10 at 10:35
  • Sorry, had to put in a reference to hitchhiker's guide to the galaxy :) Please try googles calculator: http://www.google.se/search?hl=sv&rlz=1C1GGLS_enSE348SE348&q=the+answer+to+life+the+universe+and+everything&btnG=S%C3%B6k&meta=&aq=1&oq=the+answer – Anders Jan 25 '10 at 10:43
  • 1
    Your reputation + No of badges = themeaningoflifeuniverseandeverything :D cool best of luck man!! (+1) – Pratik Deoghare Jan 25 '10 at 11:07
2

Double click on the button in the designer. This should create you a handler function for the buttons click event, something like this:

private void Button_Click(object sender, EventArgs e)
        {

        }

then in the function add the code to set the text of the lable:

lable.Text = myResult;

you should end up with something like this:

private void Button_Click(object sender, EventArgs e)
    {

    lable.Text = myResult;
    }

As you said you have an int which is a percentage, and you have the value 0, I suspect that you are having problems getting the percentage not writing the value to the lable. so you might want to look at this question as I suspect that you have something like:

int valuea;
int valueb;
int result= valuea/valueb*100;

in this example if valuea=45 and valueb=100 the value of result will be 0, not 45.

Community
  • 1
  • 1
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • we need some more information, like what is the type of your result? What do you expect it to put in the lable? – Sam Holder Jan 25 '10 at 10:39
  • and what is the value of the percentage? If you tooltip it what is the value in the tooltip? You could also try doing myResult.ToString("P") to display as a percentage; – Sam Holder Jan 25 '10 at 10:45
1
    int result = 0; // declare a private variable to hold the result

    // Event handler for Click of Enter button
    private void Enter_Click(object sender, EventArgs e)
    {
        result = Add(10,20);  // set result to result of some function like Add

        label.Text = result.ToString();
    }

    private int Add(int a, int b)
    {
        return a + b;
    }

NOTE: I assume you are a beginner working with Winforms.

Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146
0

Double click on button to generate event and write following code

private void Button_Click(object sender, EventArgs e)
{
    lable1.Text = ur result; //result must be in string format otherwise convert it to string
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
0

In winforms or webforms:

label.Text = Enter.Text;
Oded
  • 489,969
  • 99
  • 883
  • 1,009