开发者

How to display a result in a label when I click onto a button?

开发者 https://www.devze.com 2022-12-17 06:04 出处:网络
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 t开发者_运维技巧he label.How do I do that? For windows forms use the

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 t开发者_运维技巧he label. How do I do that?


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


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.


    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.


In winforms or webforms:

label.Text = Enter.Text;


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

精彩评论

暂无评论...
验证码 换一张
取 消