Click here to Skip to main content
15,879,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a pretty simple mousemove event which just shows the mouse position X,Y on a picturebox image

**image manipulation class
C#
public void move(MouseEventArgs e)
        {
            double x = e.X - xOffset;
            double y = e.Y - yOffset;
            double worldx, worldy;
            worldx = DPM.xlo + (x-DPM.hll)*(DPM.xhi-DPM.xlo)/(DPM.hur-DPM.hll);
            worldy = DPM.ylo + (y-DPM.vll)*(DPM.yhi-DPM.ylo)/(DPM.vur-DPM.vll);
            f1.labX.Text = worldx.ToString("F");
            f1.labY.Text = worldy.ToString("F");
        }

i'm trying to move all of my image modifying stuff to an image class, but calling the mousemove from the main class doesn't work - i think it's because each time i instantiate the class i refresh the variables (and i use xOffset and yOffset which are the last known position of the cursor). I also have some click based stuff and they work fine, which confirms my suspicions i think.

i've been reading about both singletons and more complex event handling, but my experience with either is null (and i have trouble comprehending the API docs as i'm not a dev), so i really appreciate any hints on how to handle this.

if i keep the code in the main form, everything works fine so i know the underlying stuff is ok

What I have tried:

**in the main form class
C#
private void evImage_MouseMove(object sender, MouseEventArgs e)
        {
            EvImage ev1 = new EvImage(this);
            ev1.move(e);
        }

that instantiates a new class each time, and i think i get why it isn't working

C#
public EvImage ev;
        public Form1(EvImage form)
        {
            this.ev = form;
        }

private void evImage_MouseMove(object sender, MouseEventArgs e)
        {           
            ev.move(e);
        }

i thought this would give me a single instance but it won't compile
Posted
Updated 21-Dec-16 15:23pm
v3

1 solution

Hi Member 12815488, some of your code is confusing. I guess your classname is EvImage where all the image manipulation code resides? Then again evImage_MouseMove() looks like your form name is evImage, but your form name is actually Form1.

Please try the following on the main form. This code is on the guess that your form name is Form1, your class name is EvImage:
C#
EvImage ev1;
private void Form1_Load(object sender, EventArgs e)
{
    ev1 = new EvImage(this);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    ev1.move(e);
}
What is being done here is the EvImage class is instatiated only once.
 
Share this answer
 
v2
Comments
Member 12815488 21-Dec-16 23:09pm    
sorry about that, you are correct about the classes. my "other" problem is, i can't instantiate the EvImage class outside of the method, because that class has a constructor, which takes the Form1 class (this is the only way i know how to instantiate a form to access all the textboxes etc.)
public EvImage(Form1 form)
{
this.f1 = form
}
and because of that constructor, i can't figure out how to instantiate outside of a method using EvImage(this)
Mehedi Shams 21-Dec-16 23:18pm    
Hi Member 12815488,

Please check the updated code. This is almost the same - just creating the instance and passing the form to the constructor in the load event only once.
Member 12815488 22-Dec-16 0:11am    
thanks! i never thought about putting it in form Load(), elegant and works grand.
Mehedi Shams 22-Dec-16 0:13am    
Glad to help. Cheers :)!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900