简体   繁体   中英

How can I change label Value by clicking in a picturebox in C#?

在此处输入图片说明 This is my first post in here and I m trying for 3-4 months now to learn c#. As the title says I have this simple method in a Guy class:

public void UpdateLabels()
{
   MyLabel.Text = (Name + " has placed " + MyBet.Amount + "$ " + "on " );      
}

In image as you can see the label updates by giving value to numericUp button but I want when I click a bear to get the name of the bear in label

Thank you a lot .

You have to add the Click event handler to the PictureBox controls, like shown in the following compact Lambda-style notation sample:

img1.Click+=(s,e)=>{ UpdateLabels();}

where img1 is a PictureBox control name of that one containing "Bear" image. The same technique you can apply to other PictureBox controls.

Note : you may need to modify the UpdateLabels(s,e) to extract sender s information (like: PictureBox _px = s as PictureBox ) and apply the updates to the corresponding Label , thus, the event subscription will look like:

img1.Click+=(s,e)=> UpdateLabels(s,e);
img2.Click+=(s,e)=> UpdateLabels(s,e);
.....

pointing to the same event handler (btw, in case of WPF I would recommend to use TextBlock instead of Label control; correspondingly it could be Image control instead of PictureBox ).

Hope this may help.

Double Click the PictureBoxes to get their Click() event. From there you can set the Label to the correct value:

    public void UpdateLabels()
    {
       MyLabel.Text = (Name + " has placed " + MyBet.Amount + "$ " + "on " + label1.Text);      
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        label1.Text = "Brown Bear";
        UpdateLabels();
    }

    private void pictureBox2_Click(object sender, EventArgs e)
    {
        label1.Text = "Red Bear";
        UpdateLabels();
    }

    // ... etc ...

*Change label1 to the name of your label (which I hope is better named).

那是在我眼前。我试图将图片框视为错误的文本。我可以看到它们,但C#无法看到,因此我制作了文本,使C#可以看到它们并将其应用于图片框选择。谢谢大家您的时间,并帮助我!

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