[英]Radio Buttons and Event check in an If statment c#
我正在尝试在graphicsPanel1_Paint
方法内编写If语句,其中仅当选择了某个单选按钮并单击一个按钮时才进行绘制。
这是我当前的代码:
private void graphicsPanel1_Paint(object sender, PaintEventArgs e)
{
for (int x = 0; x < mapSize.Width; x++)
{
for (int y = 0; y < mapSize.Height; y++)
{
Rectangle destRect = Rectangle.Empty;
destRect.X = x * tileSize.Width;
destRect.Y = y * tileSize.Height;
destRect.Size = tileSize;
destRect.X += graphicsPanel1.AutoScrollPosition.X;
destRect.Y += graphicsPanel1.AutoScrollPosition.Y;
Rectangle srcRect = Rectangle.Empty;
srcRect.X = map[x, y].X * tileSize.Width;
srcRect.Y = map[x, y].Y * tileSize.Height;
srcRect.Size = tileSize;
e.Graphics.DrawImage(Properties.Resources.colors, destRect, srcRect, GraphicsUnit.Pixel);
buttonUse.Click += buttonUse_Click;
if (radioButtonColor.Checked == true)
{
e.Graphics.DrawImage(Properties.Resources.colors, destRect, srcRect, GraphicsUnit.Pixel);
graphicsPanel1.Invalidate();
}
else if (radioButtonDesert.Checked == true)
{
e.Graphics.DrawImage(Properties.Resources.desert9x8x32x32, destRect, srcRect, GraphicsUnit.Pixel);
graphicsPanel1.Invalidate();
}
else if (radioButtonEarth.Checked == true)
{
e.Graphics.DrawImage(Properties.Resources._default, destRect, srcRect, GraphicsUnit.Pixel);
graphicsPanel1.Invalidate();
}
else if (radioButtonMagic.Checked == true)
{
e.Graphics.DrawImage(Properties.Resources.super4x4x64x64, destRect, srcRect, GraphicsUnit.Pixel);
graphicsPanel1.Invalidate();
}
e.Graphics.DrawRectangle(Pens.Black, destRect);
}
}
}
显然,这是行不通的。
您需要添加一个全局变量来检查单击的按钮(或切换状态),然后将事件处理程序添加到buttonUse
和OnPaint
并使用以下代码:
bool btnUsePressed = false; // Declare a variable to track pressed button
buttonUse.Click += new EventHandler(this.buttonUse_Click); // e.g. in the Form_Load()
// and then
private void graphicsPanel1_Paint(object sender, PaintEventArgs e)
{
// ... YOU CODE UP TO
if (radioButtonColor.Checked == true && btnUsePressed)
{
e.Graphics.DrawImage(Properties.Resources.colors, destRect, srcRect,GraphicsUnit.Pixel);
graphicsPanel1.Invalidate();
}
// ... YOU CODE AFTER
}
void buttonUse_Click(Object sender, EventArgs e)
{
if (btnUsePressed)
btnUsePressed = false;
else
{
btnUsePressed = true;
graphicsPanel1.Refresh();
}
}
Click()是一个事件,因此您需要处理该事件,然后检查您的单选按钮。 所以像
buttonUse.Click()+= buttonUse_Click
void buttonUse_Click(object sender, RoutedEventArgs e)
{
if (radioButtonColor.Checked == true)
{
e.Graphics.DrawImage(Properties.Resources.colors, destRect, srcRect,GraphicsUnit.Pixel);
graphicsPanel1.Invalidate();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.