Using MouseClick Event
https://paste.mod.gg/zlxoaubtbcom/0
Hey guys, I'm working on a code assignment where I have to use Arrays to Reverse, Swap, Clear, Delete or Insert Array Elements, with each element being a colour read in from a file. My teacher has left some parts of comments to help me organize it but I am confused on the MouseClick event - I haven't ever worked with this event before but he gave instructions on it. Any ideas on how to implement it
BlazeBin - zlxoaubtbcom
A tool for sharing your source code with the world!
67 Replies
The mouse click event provides two parameters. One of them is sender.
Debug you code and see what type is hiding inside it
For reference, here it was happens when I read in the file
Then you could cast it to whatever is representing the color, I'd assume and select it
Hey! Thanks for the reply
Question - What do you mean by two parameters?
"One of them is sender" What do you mean by this?
Sorry if these are bad questions
I am a big beginner
@ZZZZZZZZZZZZZZZZZZZZZZZZZ Any chance you can help me out since you saw me code the reading it part? might make more sense to you
My knowledge of Winforms is minimal
oop no worries
MouseEventArgs
has Location
property that provides the coordinates of the mouse pointer relative to the control that raised the event.may be relevant?
What is the contents of the file you must load?
It's a list, that when I load in, I get the elements (colors) of the array elements
When I load it in, I get this
@always use bool? as primary keys I have this image referencing coordinates, but I’m not sure if this is the same?
The first line respresents the number of squares.
Right
If you look to the right of the picture
My teacher wrote some psuedo code
But I’m not exactly sure if it’s for the mouse click event
It could be
Yes. The
e.X
and e.Y
are the MouseEventArgs e
.So I should probably try to follow this code for the event
@always use bool? as primary keys Ohhh okay
yea that makes sense
When will this assignment be due?
Will try to make sense of it by asking questions , by setting e.x - 50 and y - 100, what does this mean?
He told us try to get it done for tomorrow but if you can't (everyone is still working on it) we can have an extra day
Or if you can explain that I would appreciate that !
this part
DrawColourArray(e.Graphics, 50, 100);
The tiles start from (50,100)
.Oh okay, like the diemsions they're drawn from?
We actually did a lab similar to this so I'll check it out again
for the drawing part
Did you draw this?
Or make this
I made this with
mspaint.exe
, my favourite application.Omg lol
Thank you for even taking the time to do that
How did you get 550 and 150?
550 = 50 + 50 x 10
150 = 100 + 50
Yea but why do we add 50 + 50 * 10
I see 10 comes from 10 tiles
Right
The note on the whiteboard says that
- define two variables
x
and y
where var x = e.X - 50;
and var y = e.Y - 100;
.
- y
represents the distance of the mouse point from the top edge of tiles.
- x
represents the distance of the mouse point from the left edge of the most left tile.
- The mouse point is on any tiles if and only if 0 <= y <= 50
and 0 <= x < 500
.
- To find the index
of a certain tile, we can use var index = e.X / 50;
For example, if x = 40
then index = 40 / 50 = 0
(truncated).
The if
condition can be written as what the teacher wrote or what I said above. Both are correct.
I think mine is negligibly faster.
Yes.Sorry My dad was talking to me, back reading this
Does your dad know programming?
When you say distance from mouse point, wdym?
Oh nah he was talking to me about something completely unrelated
Sorry lol 😅
Distance between a point and a line is defined as the shortest distance from the point to the line. 🙂
Distance between a mouse point to the top edge of a tile means
the shortest distance from the mouse pointer to the top edge of the tile.
Ok I think what’s confusing me is that I’m not exactly sure what is meant by distance - like how is distance relevant here?
Like why do we give it coordinates?
Distance is the easiest way to understand why the teacher wrote
x
and y
are about distance in disguise.Gotcha
What’s that? 😅
finding distance with broken rulers.
Lol
So does the coordinates of 50,100 say like you can click from 50 on the and 100 on the y?
Like it’s the distance/area where you can click?
Yes. you can click tiles if the mouse position
(e.X,e.Y)
in the region from (50,100) to (550,150).
or
the distance (x,y)
in the region/interval (0,0)
to (500,50)
Oh okay
I gotchu
Yea that makes sense, the squares are 50 long
I like your method
Introducing
x
and y
is not mandatory actually. But I just follow what the teacher wrote.For the if statement, if the mouse e.X is greater than 50 and less than 550 and e.Y is less than 150 and greater than 100, then we set index to be o be true
Also I realize my teacher has x/50 < Colors.Length, does that have any relevance?
x/50
represents the index and the index starts from 0 to 9. That is why x/50
must be less than 10 which is the length of the Colors
array.Just curious, why is the index = e.X / 50 ?
I get the general idea thought it makes sense why it is less than the length of colors array
Like you can only go from 0 to 9 (becuse element = 0 is your first element) I'm wondering why it is divided by 50 tho, oes it just represent one X tile maybe?
-
0 <= x < 50
represents tile of index = 0
. Any x
in this interval when divided by 50 produces 0.***
or truncated to 0
.
- 50 <= x < 100
represents tile of index = 1
. Any x
in this interval when divided by 50 produces 1.***
or truncated to 1
.
- ...
- 450 <= x < 500
represents tile of index = 9
. Any x
in this interval when divided by 50 produces 9.***
or truncated to 9
.
So var index = x / 50;
Either one is ok.
or
or
Gotcha!
Can I ask, I realize when you don’t define x and y you have it set to the 150/550 bounds
yes
Why not?
because I use
e.X
and e.Y
as the distance from the mouse pointer to the left edge and top edge of the window repectively.
Just another note:
The following is wrong
but the following is correct
I see
Thank you so much man I am eating dinner but I really appreciate you taking time to help me out
Words can’t describe the feeling when you’re stuck on something n someone clears it up
I want to add some header comments to explain what I did, any suggestions for the if statement?
Wait in 8 hours. I am preparing to sleep sound now. 🙂
https://tenor.com/view/procrastiner-lsf-usm67-sign-language-gif-15604190
Good night!
(for reference), I can only press on all but one
What is your code?
I see.
I noticed earlier my teacher used Colors.Length in the if statement, maybe I could use that instead of 10?
Yes. Using
Colors.Length
is recommended because hardcoding 10
is not flexible.Using e.X seemed to fix it!
This code makes the last element not work though
I think this one with colors.length worked the best
Oh my ghost. I mistyped.
This one works too!
Assuming *50 represents the length of width of each element is 50 pixels right?
See my last edit in the previous code.
SquareSize is new!
That;s actually a smart idea
cause it's a global variable within my code
Also this is just a question but why did my teacher make it =! (Not true) if he wants the selected array to be true lol