0

as title, i have a button that load an image in a picturebox, then when i click on it i want it to display the coordinates in two textbox, only proble...it display the coordinates when i click outside of the picturebox and not when i click in. i already tried the solution in this site but it doesn't seems to work so far.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    protected override void OnMouseClick(MouseEventArgs e)
    {
        base.OnMouseClick(e);
        textBox1.Text = e.X.ToString();
        textBox2.Text = e.Y.ToString();
    }

    private void BtnBrowse_Click(object sender, EventArgs e)
    {
        openFileDialog1.Filter = "All jpg files (*.jpg)|*.jpg";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.ImageLocation = openFileDialog1.FileName;
        }
    }

   }
}
Matteo Sala
  • 43
  • 10
  • You need to code the MouseClick event __not of the Form but of the PictureBox__ ! - Btw: If the pbox has any other SizeMode than Normal you may also want to scale the mouse coordinates to the image coordinates.. – TaW Jul 12 '19 at 13:59
  • ok so i create a new partial class for picturebox, but then it said texBox 1 and 2 do not exist in the current context, sorry if i sound stupid but i am not a developper and i'm just trying to do some ikeacoding – Matteo Sala Jul 13 '19 at 10:29
  • While it often is fine to create subclasses especially for larger projects, I suggest you don't do that here and now. Instead do this: Select pictureBox1. Go to the properties window and there to the event pane. Now find the MouseClick event and doubleclick it. Two things have happened now: 1) the event code has been added to the form code and 2) the event has been hooked up. (The latter can be seen when you browse the Form1 _Designer.cs file, where you will find something like pictureBox1.MouseClick += pictureBox1_MouseClick;) - Now put the code in that event stub.. – TaW Jul 13 '19 at 12:31

0 Answers0