Question Move dynamically created button using cursor at runtime

Ravi

New member
Joined
Jun 4, 2021
Messages
1
Programming Experience
Beginner
Hi all, I'm new to programming and also to this forum.

I'm currently developing a C# WPF application which contains an image. I've written some code to dynamically add a Button onto that image.

C#:
        public void SomeFunction()
        {
            var button = new Button();

            button.Name = "btn" + randomNumber;
            button.Content = "New Button " + randomNumber;
            button.FontSize = 14;

            Grid.SetColumn(button, 8);
            Grid.SetRow(button, 25);
            Grid.SetColumnSpan(button, 25);
            Grid.SetRowSpan(button, 5);
            gridMainWindow.Children.Add(button);
        }

Now, I need to achieve the following-

1. Make the button movable with a mouse cursor all over the image, but not outside of it
2. Make the button rotate 90, 180 degrees

I will be greatful if someone helps me with this.
 
Moving to WPF...
 
You made mention of an image, but there doesn't seem to be any image in that code you presented above.

Unfortunately, you can't easily achieve part 1 due to your adding the button to a Grid. Things in the Grid like to be assigned to specific columns and rows and so you won't have free reign to put a button someplace straddling those borders, or at any arbitrary location within the grid. To get that pixel level position control, you'll need a Canvas instead of a Grid.
 
Back
Top Bottom