As a new C# programmer I have many issues that need remedying, this one in particular is a question in regards to MouseEventArgs e and its seemingly incompatible interaction with MouseButtons.Left,
I likely have a lack of understanding behind the true capability or usage of MouseEventArgs which is the cause for this issue.
Could somebody explain why my attempt is incorrect, what the correct solution would be, and how I can prevent running into issues like this in the future?
private void BtnSize_MouseDown(object sender, MouseEventArgs e)
{
// Issue exists here, e == MouseButtons.Left does not work
// why? Isn't the purpose of MouseEventArgs e to track the
// mouse input (aka left click?)
if (e == MouseButtons.Left)
{
// clear canvas
Ball.Loading = true;
while (ballsAdded < 25 && ballsRemoved < 1000)
{
Ball newBall = new Ball(ballRadiusSet);
// Contains bool will force balls to spawn apart from one another
if (!soManyBalls.Contains(newBall))
{
soManyBalls.Add(newBall);
ballsAdded ;
}
else
{
ballsRemoved ;
}
}
foreach (Ball listBall in soManyBalls)
{
listBall.AddBall();
}
// render canvas
Ball.Loading = false;
Text = $"Loaded {ballsAdded} distinct balls with {ballsRemoved} discards";
ballsAdded = 0;
ballsRemoved = 0;
}
if (e == MouseButtons.Right)
{
// clear canvas
Ball.Loading = true;
while (ballsAdded < 25 && ballsRemoved < 1000)
{
Ball newBall = new Ball(ballRadiusSet);
// Invert Contains bool so balls form directly connected to one another
if (soManyBalls.Contains(newBall))
{
soManyBalls.Add(newBall);
ballsAdded ;
}
else
{
ballsRemoved ;
}
}
foreach (Ball listBall in soManyBalls)
{
listBall.AddBall();
}
// render canvas
Ball.Loading = false;
Text = $"Loaded {ballsAdded} distinct balls with {ballsRemoved} discards";
ballsAdded = 0;
ballsRemoved = 0;
}
}
CodePudding user response:
Instead of comparing MouseButtons.Left
with the event itself (e == MouseButtons.Lef
), you can use a property of the event that contains the relevant mouse button:
if (e.Button == MouseButtons.Left)
{
// ...
MouseEventArgs
does not only contain the pressed button, but also other information regarding the mouse event, e.g. the location.
Please also note that the Button
property contains information on all the pressed buttons. So if for instance both the left and the right button have been clicked, but you only want to check for the left button, you have to compare the bit patterns, e.g.
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
// ...
Above condition would also evaluate to true, if the left button was clicked together with other buttons.
CodePudding user response:
parameter e
is an object of type MouseEventArgs. MouseButtons.Left
is an enum field of type MouseButtons
. This enum field is never equal to an object of type MouseEventArgs.
private void BtnSize_MouseDown(object sender, MouseEventArgs e)
{
if (e == MouseButtons.Left)
This equation will always return false.
If you examine the definition of MouseEventArgs, you'll find that it has a property MouseEventArgs.Button. This property has type MouseButtons. The following might work:
if (e.Button == MouseButtons.Left)
Warning! MouseButtons is an enum with flags attribute. This means that the value can be a combination of several enum values. If the operator presses both left and right button the value will be: MouseButtons.Left | MouseButtons.Right
. Therefore the statement if (e.Button == MouseButtons.Left)
will return false.
You have to check the requirements how to react if the operator presses both buttons? Ignore the right button and act as if only the left button has been pressed?
if (e.Button & MouseButtons.Left == MouseButtons.Left)
This will return true if only the Left button is pressed, but also if the left button is pressed together with other buttons