C
C#10mo ago
Array

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
Denis
Denis10mo ago
The mouse click event provides two parameters. One of them is sender. Debug you code and see what type is hiding inside it
Array
ArrayOP10mo ago
For reference, here it was happens when I read in the file
No description
Denis
Denis10mo ago
Then you could cast it to whatever is representing the color, I'd assume and select it
Array
ArrayOP10mo ago
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
//todo
//mouse click detects if an array element selected
//set the element selected in Selected array to true





//call this.Refresh() if an element is selected
this.Refresh();
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
//todo
//mouse click detects if an array element selected
//set the element selected in Selected array to true





//call this.Refresh() if an element is selected
this.Refresh();
}
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
Angius
Angius10mo ago
My knowledge of Winforms is minimal
Array
ArrayOP10mo ago
oop no worries
i like chatgpt
i like chatgpt10mo ago
MouseEventArgs has Location property that provides the coordinates of the mouse pointer relative to the control that raised the event.
Array
ArrayOP10mo ago
may be relevant?
No description
i like chatgpt
i like chatgpt10mo ago
What is the contents of the file you must load?
Array
ArrayOP10mo ago
It's a list, that when I load in, I get the elements (colors) of the array elements
Array
ArrayOP10mo ago
No description
Array
ArrayOP10mo ago
No description
Array
ArrayOP10mo ago
When I load it in, I get this
No description
Array
ArrayOP10mo ago
@always use bool? as primary keys I have this image referencing coordinates, but I’m not sure if this is the same?
No description
i like chatgpt
i like chatgpt10mo ago
The first line respresents the number of squares.
Array
ArrayOP10mo ago
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
i like chatgpt
i like chatgpt10mo ago
Yes. The e.X and e.Y are the MouseEventArgs e.
Array
ArrayOP10mo ago
So I should probably try to follow this code for the event @always use bool? as primary keys Ohhh okay yea that makes sense
i like chatgpt
i like chatgpt10mo ago
When will this assignment be due?
Array
ArrayOP10mo ago
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
i like chatgpt
i like chatgpt10mo ago
DrawColourArray(e.Graphics, 50, 100); The tiles start from (50,100).
Array
ArrayOP10mo ago
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
i like chatgpt
i like chatgpt10mo ago
No description
Array
ArrayOP10mo ago
Did you draw this? Or make this
i like chatgpt
i like chatgpt10mo ago
I made this with mspaint.exe, my favourite application.
Array
ArrayOP10mo ago
Omg lol Thank you for even taking the time to do that How did you get 550 and 150?
i like chatgpt
i like chatgpt10mo ago
550 = 50 + 50 x 10 150 = 100 + 50
Array
ArrayOP10mo ago
Yea but why do we add 50 + 50 * 10 I see 10 comes from 10 tiles Right
i like chatgpt
i like chatgpt10mo ago
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.
// mine
if(y>=0 && y<=50 && x>=0 && x<500)
{
// ...
}
// mine
if(y>=0 && y<=50 && x>=0 && x<500)
{
// ...
}
// your teacher
if(y>=0 && y<=50 && x>=0 && x/50<10)
{
// ...
}
// your teacher
if(y>=0 && y<=50 && x>=0 && x/50<10)
{
// ...
}
I think mine is negligibly faster. Yes.
Array
ArrayOP10mo ago
Sorry My dad was talking to me, back reading this
i like chatgpt
i like chatgpt10mo ago
Does your dad know programming?
Array
ArrayOP10mo ago
When you say distance from mouse point, wdym? Oh nah he was talking to me about something completely unrelated Sorry lol 😅
i like chatgpt
i like chatgpt10mo ago
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.
Array
ArrayOP10mo ago
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?
i like chatgpt
i like chatgpt10mo ago
Distance is the easiest way to understand why the teacher wrote
var x = e.X - 50;
var y = e.Y - 100;
var x = e.X - 50;
var y = e.Y - 100;
x and y are about distance in disguise.
Array
ArrayOP10mo ago
Gotcha
i like chatgpt
i like chatgpt10mo ago
No description
Array
ArrayOP10mo ago
What’s that? 😅
i like chatgpt
i like chatgpt10mo ago
finding distance with broken rulers.
Array
ArrayOP10mo ago
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?
i like chatgpt
i like chatgpt10mo ago
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)
Array
ArrayOP10mo ago
Oh okay I gotchu Yea that makes sense, the squares are 50 long I like your method
i like chatgpt
i like chatgpt10mo ago
Introducing x and y is not mandatory actually. But I just follow what the teacher wrote.
Array
ArrayOP10mo ago
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?
i like chatgpt
i like chatgpt10mo ago
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.
Array
ArrayOP10mo ago
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?
i like chatgpt
i like chatgpt10mo ago
- 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.
if (e.X >= 50 && e.X < 550 && e.Y >= 100 && e.Y <= 150)
{
var index = e.X / 50 - 1;
Selected[index] = !Selected[index];
}
if (e.X >= 50 && e.X < 550 && e.Y >= 100 && e.Y <= 150)
{
var index = e.X / 50 - 1;
Selected[index] = !Selected[index];
}
or
var x = e.X - 50;
var y = e.Y - 100;
if (x >= 0 && x / 50 < 10 && y >= 0 && y <= 50)
{
var index = x / 50;
Selected[index] = !Selected[index];
}
var x = e.X - 50;
var y = e.Y - 100;
if (x >= 0 && x / 50 < 10 && y >= 0 && y <= 50)
{
var index = x / 50;
Selected[index] = !Selected[index];
}
or
var x = e.X - 50;
var y = e.Y - 100;
if (x >= 0 && x < 500 && y >= 0 && y <= 50)
{
var index = x / 50;
Selected[index] = !Selected[index];
}
var x = e.X - 50;
var y = e.Y - 100;
if (x >= 0 && x < 500 && y >= 0 && y <= 50)
{
var index = x / 50;
Selected[index] = !Selected[index];
}
Array
ArrayOP10mo ago
Gotcha! Can I ask, I realize when you don’t define x and y you have it set to the 150/550 bounds
i like chatgpt
i like chatgpt10mo ago
yes
Array
ArrayOP10mo ago
Why not?
i like chatgpt
i like chatgpt10mo ago
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
var x = e.X - 50;
var y = e.Y - 100;
var index = x / 50;
if (index >= 0 && index < 10 && y >= 0 && y <= 50)
{
Selected[index] = !Selected[index];
}
var x = e.X - 50;
var y = e.Y - 100;
var index = x / 50;
if (index >= 0 && index < 10 && y >= 0 && y <= 50)
{
Selected[index] = !Selected[index];
}
but the following is correct
var x = e.X - 50;
var y = e.Y - 100;
var index = x / 50;
if (e.X > 0 && index < 10 && y >= 0 && y <= 50)
{
Selected[index] = !Selected[index];
}
var x = e.X - 50;
var y = e.Y - 100;
var index = x / 50;
if (e.X > 0 && index < 10 && y >= 0 && y <= 50)
{
Selected[index] = !Selected[index];
}
Array
ArrayOP10mo ago
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?
i like chatgpt
i like chatgpt10mo ago
Wait in 8 hours. I am preparing to sleep sound now. 🙂 https://tenor.com/view/procrastiner-lsf-usm67-sign-language-gif-15604190
Tenor
Array
ArrayOP10mo ago
Good night!
Array
ArrayOP10mo ago
(for reference), I can only press on all but one
No description
i like chatgpt
i like chatgpt10mo ago
What is your code?
Array
ArrayOP10mo ago
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
//todo
//mouse click detects if an array element selected
//set the element selected in Selected array to true

int X = e.X - 50;
int Y = e.Y - 100;
int Index = X / 50;
if (Index > 0 && Index < 10 && Y >= 0 && Y <= 50)
{
Selected[Index] = !Selected[Index];

}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
//todo
//mouse click detects if an array element selected
//set the element selected in Selected array to true

int X = e.X - 50;
int Y = e.Y - 100;
int Index = X / 50;
if (Index > 0 && Index < 10 && Y >= 0 && Y <= 50)
{
Selected[Index] = !Selected[Index];

}
i like chatgpt
i like chatgpt10mo ago
I see.
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
//todo
//mouse click detects if an array element selected
//set the element selected in Selected array to true

int X = e.X - 50;
int Y = e.Y - 100;
int Index = X / 50;
if (e.X > 0 && Index < 10 && Y >= 0 && Y <= 50)
{
Selected[Index] = !Selected[Index];
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
//todo
//mouse click detects if an array element selected
//set the element selected in Selected array to true

int X = e.X - 50;
int Y = e.Y - 100;
int Index = X / 50;
if (e.X > 0 && Index < 10 && Y >= 0 && Y <= 50)
{
Selected[Index] = !Selected[Index];
}
}
Array
ArrayOP10mo ago
I noticed earlier my teacher used Colors.Length in the if statement, maybe I could use that instead of 10?
i like chatgpt
i like chatgpt10mo ago
Yes. Using Colors.Length is recommended because hardcoding 10 is not flexible.
Array
ArrayOP10mo ago
Using e.X seemed to fix it!
i like chatgpt
i like chatgpt10mo ago
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
//todo
//mouse click detects if an array element selected
//set the element selected in Selected array to true

int X = e.X - 50;
int Y = e.Y - 100;
int Index = X / 50;
if (e.X > 0 && e.X < Colors.Length * 50 && Y >= 0 && Y <= 50)
{
Selected[Index] = !Selected[Index];
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
//todo
//mouse click detects if an array element selected
//set the element selected in Selected array to true

int X = e.X - 50;
int Y = e.Y - 100;
int Index = X / 50;
if (e.X > 0 && e.X < Colors.Length * 50 && Y >= 0 && Y <= 50)
{
Selected[Index] = !Selected[Index];
}
}
Array
ArrayOP10mo ago
This code makes the last element not work though I think this one with colors.length worked the best
i like chatgpt
i like chatgpt10mo ago
Oh my ghost. I mistyped.
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
//todo
//mouse click detects if an array element selected
//set the element selected in Selected array to true

int X = e.X - 50;
int Y = e.Y - 100;
int Index = X / SquareSize;
if (X > 0 && X < Colors.Length * SquareSize && Y >= 0 && Y <= SquareSize)
{
Selected[Index] = !Selected[Index];
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
//todo
//mouse click detects if an array element selected
//set the element selected in Selected array to true

int X = e.X - 50;
int Y = e.Y - 100;
int Index = X / SquareSize;
if (X > 0 && X < Colors.Length * SquareSize && Y >= 0 && Y <= SquareSize)
{
Selected[Index] = !Selected[Index];
}
}
Array
ArrayOP10mo ago
This one works too! Assuming *50 represents the length of width of each element is 50 pixels right?
i like chatgpt
i like chatgpt10mo ago
See my last edit in the previous code.
Array
ArrayOP10mo ago
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
Want results from more Discord servers?
Add your server