C# If语句c中的单选按钮和事件检查#
我试图在C# If语句c中的单选按钮和事件检查#,c#,C#,我试图在graphicsPanel1\u Paint方法中编写一个If语句,在该方法中,只有在选中某个单选按钮并单击某个按钮时,才会进行绘制 这是我目前的代码: private void graphicsPanel1_Paint(object sender, PaintEventArgs e) { for (int x = 0; x < mapSize.Width; x++) { for (int y = 0; y <
graphicsPanel1\u 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);
}
}
}
private void graphicsPanel1_Paint(对象发送器,PaintEventArgs e)
{
对于(int x=0;x
显然,这不起作用。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();
}
}
您需要添加一个全局变量来检查单击的按钮(或切换的状态),然后向
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()
事件检查如果(radioButtonColor.checked)
并编写代码…您在哪里编写了此代码?如果按钮应该具有切换行为,要么将其写入按钮的事件处理程序,要么将其转换为一个复选框,该复选框具有一个已选中的属性??或者是一个检查者(就像其他被问到的评论一样?)??表单必须做什么或如何反应还不完全清楚。EventArgs
没有公开图形属性。@FrédéricHamidi:系统.Windows.Forms.PaintEventArgs
,现在可以了吗?不可以。单击处理程序使用EventArgs
参数调用,因此代码不会编译。要使此解决方案有意义,您必须处理这两个事件。@FrédéricHamidi:问题是OP没有提供更多上下文:(是的,这就是问题所在。不回答像这样的问题可能不是一个坏主意。有人能告诉我为什么我被否决了,这样我可以在下次解决它吗?可能与我对另一个答案的评论中的原因相同——代码无法编译。请检查语法和RoutedEventArgs
的属性。