Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

What I have tried:

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


       }
Posted
Updated 5-May-21 18:59pm

1 solution

floors.ShowDialog(); ... what is the point of using 'ShowDialog when you do nothing based on the result ?

Study this: [^]

Put breakpoints in your code and examine the state of the varuables as you single-step through it (using F11).

Is it possible showing whatever a 'Floors is ... a Form ? ... clears the MouseButtonState ? Try capturing the MouseButtonState in a variable before you show the 'floors modally.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900