c# WinForm 如何在坐标上添加按钮
c# WinForm How to add button on Coordinate
我的想法是在我点击表格的坐标上添加一个 button/custom 标记,但我不知道如何实现它。
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);
}
这让我每次单击表单时都能得到一个按钮,但我的想法是单击表单,然后按 addlogo 按钮向表单添加一个按钮。
您需要一个形式为
的变量
bool _placeButton = false;
int _xButton;
int _yButton;
然后
private void MapBrowser_MouseUp(object sender, MouseEventArgs e)
{
textBox1.Text = "X-" + e.X + "Y- " + e.Y;
_xButton = e.X;
_yButton = e.Y;
_placeButton = true;
}
最后
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);
}
}
我的想法是在我点击表格的坐标上添加一个 button/custom 标记,但我不知道如何实现它。
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);
}
这让我每次单击表单时都能得到一个按钮,但我的想法是单击表单,然后按 addlogo 按钮向表单添加一个按钮。
您需要一个形式为
的变量bool _placeButton = false;
int _xButton;
int _yButton;
然后
private void MapBrowser_MouseUp(object sender, MouseEventArgs e)
{
textBox1.Text = "X-" + e.X + "Y- " + e.Y;
_xButton = e.X;
_yButton = e.Y;
_placeButton = true;
}
最后
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);
}
}