Home > Software design >  c# WinForm How to add button on Coordinate
c# WinForm How to add button on Coordinate

Time:02-28

My idea is to add a button/custom marker to the coordinate i click on the form but i have no idea how to implement it.

 private void AddLogo_Click_1(object sender, EventArgs e)
    {
        
    } 
private void MapBrowser_MouseUp(object sender, MouseEventArgs e)
    {
        textBox1.Text = "X-"   e.X   "Y- "   e.Y;
        var button1 = new Button { Location = new Point(e.X, e.Y) };
       
        Controls.Add(button1);

    }

This allow me to get a button everytime i click on the form, but my idea is to click on the form and then press the addlogo button to add a button to the form.

CodePudding user response:

you need a variables in the form

bool _placeButton = false;
int _xButton;
int _yButton;

then

private void MapBrowser_MouseUp(object sender, MouseEventArgs e)
{
    textBox1.Text = "X-"   e.X   "Y- "   e.Y;
    _xButton = e.X;
    _yButton = e.Y;
    _placeButton = true;
}

and finally

 private void AddLogo_Click_1(object sender, EventArgs e)
{
   if(_placeButton)
    {
      _placeButton = false;
      var button1 = new Button { Location = new Point(_xButton, _yButton) };
   
      Controls.Add(button1);
   }  
    
} 
  • Related