0

I seem to have some bad luck getting any events beyond formX_load(..) to run. That's what I'm hoping someone here can help me with.

I started an empty project, added a label onto it (label1) and just copied the code off of MSDN's Example. (I added in my own label text).

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.move%28v=vs.80%29.aspx

private void Form1_Move(object sender, System.EventArgs e)
{
this.Text = "Form screen position = " + this.Location.ToString();
label1.Text = "You Moved Me!";
}

Everything compiles and runs, but it doesn't matter how much I move or re-size that form it doesn't change the label text or the Form text.

I've also tried OnMove, OnMouseMove, and LocationChanged examples with the same problem...they never ever seem to be triggered.

What am I missing here? This seems too easy to be able to screw up, but alas...

Thanks for your time.

3 Answers3

2

This works:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.Move +=new EventHandler(Form1_Move);
    }

    private void Form1_Move(object sender, System.EventArgs e)
    {
        this.Text = "Form screen position = " + this.Location.ToString();
    }

}
Samich
  • 29,157
  • 6
  • 68
  • 77
  • Well that works pretty well! I guess a rule of thumb is that for any event you want to make you have to create an event Handler like that at the beginning? I haven't had to use C# in many years so I'm having to relearn everything. Thank you very much! – challengerTA Oct 06 '11 at 19:17
2

Overriding OnMove should have worked and is the preferred method. Did you call the base class OnMove method in your overrided method?

For the _Move event, did you wire up the event handler upon creating the instance of the Form?

Something like:

this.Move += this.Form1_Move;
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
  • I didn't think to do any of that. I was running under the -wrong- assumption that MSDN "examples" are working examples and I didn't need to do anything else to get them to work. Apparently a fatal error. Is what you have there the preferred way of joining the event and handler? Or is Samich's method more proper? Both work and I really appreciate your help. – challengerTA Oct 06 '11 at 19:21
  • Whether to override OnMove or handle the event is a personal choice. I have always felt that overriding the On* methods in *your own class* is preferred. Obviously, if you're using someone else's class you would have to handle the event. – Chris Dunaway Oct 06 '11 at 19:32
  • Here's a link that may help: http://stackoverflow.com/questions/3670806/form-load-event-or-override-onload – Chris Dunaway Oct 06 '11 at 19:38
1

There are 2 answers already explaining how to add the handler programmatically -as I mentioned on my comment-. If you prefer to do it from Visual Studio, simply load the Form Designer, look at the properties (usually located on the bottom right corner), click on the Events tab and double click on the Event for OnMove. Once you double click it VS will add the handler for you automatically and take you to the body of the method so you can put your code in there.

enter image description here

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • You took a screenshot for me too? That's super helpful for future. I remember doing something like that when I was doing VB6. Thanks again for the help. I wish I had more than one up-arrow to give. – challengerTA Oct 06 '11 at 19:31