Can i check is MouseClick inside certain area? - c#

I have PictureBox in my Windows Form.When I click in some part of the picture, some label text needs to be changed.
Is there any way to know is it clicked in some part of the image?
I didn't gave any code because i think you can understand my problem whitout it.

I suppose you have rect as your Rectangle with some initialization, its coordinates are relative to your PictureBox:
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if(rect.Contains(e.Location)){
//your code here
}
}

You should create mapping for your image, something like this:
Rectangle rect1 = new Rectangle(/*coordinates of part of image*/);
OnClick(object sender, MouseEventArgs e)
{
if (rect1.Contains(e.Location))
{
//handler for this part
}
}

Create List of Rectangle containing areas you want to be interactive.
let say:
private static List<Rectangle> rects;
populate it with desired coordinates in some order.
then in OnClick event
OnClick(object sender, MouseEventArgs e)
{
for(int i=0; i<rects.Count; i++)
if (r.Contains(e.Location))
ActionForArea(i);
}
also
private static void ActionForArea(int number)
{
//do sth
}

Related

How can I make images drag and drop-able to reorder them?

I'm building an application in which I'd like a user to be able to reorder pictures in a form in two columns. I've got a flowLayoutPanel of a set width, and pictures are added via the OpenFileDialog and scaled to half the width (minus an allowance for a scroll bar) of the flow layout panel.
This is where I'm stuck - I've tried adding the images as Labels, Buttons, and now PictureBoxes and I can't work out how to actually move them around. I gave up on labels because CanSelect is false - although I didn't know if that would have made a difference - and I moved on from buttons because I realised picture boxes existed. I'm open to switching out which controls I use but the images will always need to be in two columns.
Here's the code I currently have for the DragEnter and DragDrop events:
private void flowLayoutPanel_6_Cards_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void flowLayoutPanel_6_Cards_DragDrop(object sender, DragEventArgs e)
{
MessageBox.Show("dropped");
}
How can I implement this? What controls should I use and what properties should I be looking at to make this possible?
So thanks to #TaW's comment I now know that you have to add a DoDragDrop call to the MouseDown event on whatever you're dragging. My now-working code is below (thanks mostly to this tutorial):
private void flowLayoutPanel_6_Cards_DragDrop(object sender, DragEventArgs e)
{
PictureBox picture = (PictureBox)e.Data.GetData(typeof(PictureBox));
FlowLayoutPanel _source = (FlowLayoutPanel)picture.Parent;
FlowLayoutPanel _destination = (FlowLayoutPanel)sender;
if (_source != _destination)
{
//where did you even get this from?
}
else
{
Point p = _destination.PointToClient(new Point(e.X, e.Y));
var item = _destination.GetChildAtPoint(p);
int index = _destination.Controls.GetChildIndex(item, false);
_destination.Controls.SetChildIndex(picture, index);
_destination.Invalidate();
}
}
private void flowLayoutPanel_6_Cards_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
void p_MouseDown(object sender, MouseEventArgs e)
{
PictureBox p = (PictureBox)sender;
p.DoDragDrop(p, DragDropEffects.All);
}

Getting Scrollbar position from Mouse wheel event

I have a panel in winform. I want to capture both scroll and mouse wheel event for the panel. For both scenario I want to check the scroll bar position.
When scroll bar is at bottom (at the end of scrolling...) the control should fire the event.
I have done this for Panel.Scroll like this:
private void Panel1_Scroll(object sender, ScrollEventArgs e)
{
if (e.NewValue == Panel1.VerticalScroll.Maximum - Panel1.VerticalScroll.LargeChange+1)
{
//do some operation
}
}
But for MouseEventArgs there is no value (e.newvalue) to indicate scrollbar position.
How can I get the Scrollbar position from mouse wheel event ?
Also as per my requirement both event call have same logic implementation, so I want to write the logic once.
How can I achieve this ?
Well upon further analysis I checked out that panel1.VerticalScroll.Value
is equivalent to e.NewValue of ScrollEventArgs.
So for code re-usability below can be used:
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
panel1_scrollcheck(e.NewValue);
}
private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
panel1_scrollcheck(panel1.VerticalScroll.Value);
}
private void panel1_scrollcheck(int currPos)
{
if (currPos == panel1.VerticalScroll.Maximum - panel1.VerticalScroll.LargeChange+1)
{
//Put the logic here
}
}
Try to you VerticalScroll property of Panel.
void MouseWheel(object sender, MouseEventArgs e)
{
if (_panel.VerticalScroll.Value > _panel.VerticalScroll.Maximum - _panel.VerticalScroll.LargeChange)
MessageBox.Show("Bottom");
}

Move a window with custom borders using the mouse

I'm working on a C# project using WPF. In my MainWindow I just got rid of the default windows border and made an own border instead with a png file. I just set this as the background of the MainWindow.
Now I made some kinda tricky way to make my window move:
private void Window_MouseDown(object sender, MouseButtonEventArgs e) {
mouseDown = true;
lastX = (int)e.GetPosition(this).X;
lastY = (int)e.GetPosition(this).Y;
}
private void Window_MouseUp(object sender, MouseButtonEventArgs e) {
mouseDown = false;
}
private void Window_MouseMove(object sender, MouseEventArgs e) {
if (mouseDown) {
int xDiff = (int)e.GetPosition(this).X - lastX;
int yDiff = (int)e.GetPosition(this).Y - lastY;
this.Left += xDiff;
this.Top += yDiff;
}
}
private void Window_MouseLeave(object sender, MouseEventArgs e) {
mouseDown = false;
}
Now this doesn't really work properly. This way the user can move the window at all clear spaces, even on Labels and TextBlocks. Is there a way to give the background or a border these kind of events? Or is there a better way to integrate borders?
Thanks in advance!
As you said, behavior is erratic when done manually but there's a fix for it.
This is the method in the framework that is specially for doing that :
http://msdn.microsoft.com/en-us/library/system.windows.window.dragmove(v=vs.110).aspx

Which pictureBox was selected? C#

I am working on a piano in C#. I have encountered a small problem.
I have a piano keyboard which, when pressed, displays the relevant note on the staff.
The notes created are stored in an array of type PictureBox, called picBox. I have constructed the following event handler, however it is not working.
private void pictureBox_Click(object sender, MouseEventArgs e)
{
picBox[0].MouseDown += new MouseEventHandler(pic_Click); //testing for first location
}
private void pic_Click(object sender, MouseEventArgs e)
{
ClickedTextBox.Text = "I was clicked";
}
I am just testing to see if the first note was clicked. Why is this not working?
Here is the method which adds the picturebox (containing the note) to the staff (panel3).
public void addPictureBox(int x, int y, Image image)
{
picBox[cnt] = new PictureBox();
picBox[cnt].Image = image;
picBox[cnt].Location = new Point(x, y);
picBox[cnt].BackColor = Color.Transparent;
panel3.Controls.Add(picBox[cnt]);
picBox[cnt].BringToFront();
cnt++;
}
What is wrong with my event handler please? Also, what can I do to identify the location in the array of the picturebox clicked? Thank you
As said in the first comment, you subscribe to the event in a wrong location.
Also use the sender parameter of your event handler to know which picturebox is clicked (it'll contain an instance of the picturebox).

Moveable Custom Control

I have a custom control and I want the user to be able to drag it. So I put in the following code in the custom control:
void MoveableStackPanel_MouseMove(object sender, MouseEventArgs e)
{
if (IsMoving)
{
Point newLoc = e.GetPosition(null);
MainWindow.Instance.Title = newLoc.ToString(); // Debug
Margin = new Thickness(newLoc.X - 48, newLoc.Y - 48, 0, 0);
}
}
Note the "-48" in the code.
When the mouse is moved up or left then the mouse is not in the controls area anymore and thus does no longer trigger the MouseMove event. So I added the -48 twice to work around that. But when the user moves the mouse faster than the framework can update then the mouse will get outside the controls area and the control also won't move anymore.
I was thinking about assigning an IMovableInterface and keep lists of controls that are moving in the main form and such but that is all such a hassle and such... What is the proper solution?
P.S.: the controls are generated dynamically so I need the solution in C# code and not in XML.
Try using the CaptureMouse Method.
See if something like this works for you.:
void moveableStackPanel1_MouseUp(object sender, MouseButtonEventArgs e)
{
ReleaseMouseCapture();
}
void moveableStackPanel1_MouseDown(object sender, MouseButtonEventArgs e)
{
if (IsEnabled && IsVisible)
CaptureMouse();
}
void moveableStackPanel1_MouseMove(object sender, MouseEventArgs e)
{
if (IsMouseCaptured)
{
Point newLoc = e.GetPosition(null);
Margin = new Thickness(newLoc.X, newLoc.Y, 0, 0);
}
}

Categories