1

I want to be ale to add an image to a form in runtime using a click from the left mouse button. When I tried the code below, nothing happens. I don't get an error message and the picture doesn't appear where I click. I'm not sure if I'm on the right track or not. I don't know how to start if this isn't how I go about doing this operations.

private void button7_Click(object sender, EventArgs e)
{
       Floors floors = new Floors();
       floors.ShowDialog();

       if(Mouse.LeftButton == MouseButtonState.Pressed)
       {
           var picture = new PictureBox
           {
               Name = "pictureBox",
               Size = new Size(16, 16),
               Location = new Point(100, 100),
               Image = Final_Project_1_.Properties.Resources.fire_icon,
           };

           this.Controls.Add(picture);
       }
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 1
    Can you explain the `Floors` dialog you're showing before handling the click? I assume the user has released the mouse button when the `if` is reached. – C.Evenhuis May 06 '21 at 06:06
  • Does this answer your question? [Add PictureBox to form at runtime](https://stackoverflow.com/questions/31879906/add-picturebox-to-form-at-runtime) –  May 06 '21 at 06:29
  • Does this answer your question? [Load image from resources area of project in C#](https://stackoverflow.com/questions/1192054/load-image-from-resources-area-of-project-in-c-sharp) –  May 06 '21 at 06:31
  • Does this answer your question? [Load image from resources](https://stackoverflow.com/questions/13592150/load-image-from-resources) –  May 06 '21 at 06:31
  • `button7` tells us there are many controls on the form. Adding a new control with `this.Controls.Add()` may simply put it under other controls, so you will not see it. I suggest you to use dedicated container on the form (e.g. some `Panel`) and add to it. Ideally invisible `PictureBox` should already be there and you just change its visibility and `Image` to *show* image. – Sinatr May 06 '21 at 06:50
  • Or maybe you just mean to use `floors.Controls.Add(picture)` to add `PictureBox` to a new form? – Sinatr May 06 '21 at 06:59
  • When the Click event happens the left button is no longer pressed. - The test is not necessary anyway, as only the left button can evoke a click. – TaW May 06 '21 at 08:24

1 Answers1

0

So I tried the following code:

 private void button1_Click(object sender, EventArgs e)
    {
        var picture = new PictureBox
        {
            Name = "pictureBox",
            Size = new Size(16, 16),
            Location = new Point(100, 100),
            Image = WinFormsTestApp.Properties.Resources.Copy,
            SizeMode = PictureBoxSizeMode.StretchImage
        };
        this.Controls.Add(picture);
    }

and this worked perfectly fine. If this doesn't work on your machine, your image link may be corrupted or something else is overlapping the image.

However, if this works, I guess it's currently not working because of the if statement with the mouse button. It could be, that the click event will only be raised, if the mouse button is up again so the if statement can never be true. If you want to achieve, that the image is created the moment the mouse button goes down, you should be able to do so with the MouseDown Event.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Patrick
  • 387
  • 3
  • 15